Be a Supporter!

SavedObject?

  • 148 Views
  • 9 Replies
New Topic Respond to this Topic
Jawnduss
Jawnduss
  • Member since: Jul. 18, 2012
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
SavedObject? 2014-02-21 14:54:38 Reply

So, here's my situation.
I'm working on a little test game just to get used to AS3, so far what i have is: You click an enemy, and when you do so it deals 1 damage, and gives you points equal to the damage dealt.
I added to point system for a currency to use in an upgrade shop on a separate frame (frame 2), when you go to the shop on the separate frame, the points remain are shown, but when you back to the battle, the points reset.
I was wondering how i would go about making it so that the points don't reset. I thought i need some sort of saving method but i'm not sure how to do that. Any help would be appreciated.

Jawnduss
Jawnduss
  • Member since: Jul. 18, 2012
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to SavedObject? 2014-02-21 15:05:12 Reply

This is the first frame:

stop();
import flash.events.MouseEvent;
import flash.net.SharedObject;

var enemyHealth:Number = 1000;
var damage:Number = 1;
var damageDealt:Number = 0;
var points:Number = 0 ;
var level:Number = 1;

enemy.addEventListener(MouseEvent.CLICK, dealDamage);
function dealDamage(ev:MouseEvent)
{
enemyHealth -= damage;
damageDealt += damage;
EnemyHealth.gotoAndStop(damageDealt);
trace(enemyHealth);
}

enemy.addEventListener(MouseEvent.CLICK, addPoint);
function addPoint(ev:MouseEvent)
{
points += damage;
trace(points);
Points.text = String(points);

and then the second is:

import flash.events.MouseEvent;
import flash.net.SharedObject;

UpDam.addEventListener(MouseEvent.CLICK, Upgrade);
function Upgrade(ev:MouseEvent)
{
if (points >= 10)
{
damagetext.text = String(level);
damage += 1;
points -= 10;
Points.text = String(points);
level++;
}
}
back.addEventListener(MouseEvent.CLICK, goBack);
function goBack(ev:MouseEvent)
{
gotoAndStop(1);
}

Points.text = String(points);

bumblefish
bumblefish
  • Member since: Feb. 26, 2013
  • Offline.
Forum Stats
Member
Level 05
Game Developer
Response to SavedObject? 2014-02-21 15:12:02 Reply

At 2/21/14 03:05 PM, Jawnduss wrote: This is the first frame:

stop();
import flash.events.MouseEvent;
import flash.net.SharedObject;

var enemyHealth:Number = 1000;
var damage:Number = 1;
var damageDealt:Number = 0;
var points:Number = 0 ;
var level:Number = 1;

enemy.addEventListener(MouseEvent.CLICK, dealDamage);
function dealDamage(ev:MouseEvent)
{
enemyHealth -= damage;
damageDealt += damage;
EnemyHealth.gotoAndStop(damageDealt);
trace(enemyHealth);
}

enemy.addEventListener(MouseEvent.CLICK, addPoint);
function addPoint(ev:MouseEvent)
{
points += damage;
trace(points);
Points.text = String(points);

and then the second is:

import flash.events.MouseEvent;
import flash.net.SharedObject;

UpDam.addEventListener(MouseEvent.CLICK, Upgrade);
function Upgrade(ev:MouseEvent)
{
if (points >= 10)
{
damagetext.text = String(level);
damage += 1;
points -= 10;
Points.text = String(points);
level++;
}
}
back.addEventListener(MouseEvent.CLICK, goBack);
function goBack(ev:MouseEvent)
{
gotoAndStop(1);
}

Points.text = String(points);

You are gonna need to make points as "public static var points:number, then in your next class, you are gonna wanna call the first class by name like this: say your class name is classone, call classone.points.

bumblefish
bumblefish
  • Member since: Feb. 26, 2013
  • Offline.
Forum Stats
Member
Level 05
Game Developer
Response to SavedObject? 2014-02-21 15:42:03 Reply

Sorry, I just scanned through it the first time. I am unfamiliar with FlashCS, just using the language in an IDE. I use a class inside of a package rather than frames.

milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to SavedObject? 2014-02-22 19:15:20 Reply

At 2/21/14 02:54 PM, Jawnduss wrote: I was wondering how i would go about making it so that the points don't reset.

Don't use frames. The code on a frame is executed when you go to that frame, that's why your variables reset.

Instead of having several frames, each for one state of your game, create a Sprite/MovieClip for each state and add those to the display list.

At 2/21/14 03:12 PM, bumblefish wrote: You are gonna need to make points as "public static var points:number, then in your next class, you are gonna wanna call the first class by name like this: say your class name is classone, call classone.points.

That's a bad idea because it creates unwanted dependencies.

bumblefish
bumblefish
  • Member since: Feb. 26, 2013
  • Offline.
Forum Stats
Member
Level 05
Game Developer
Response to SavedObject? 2014-02-23 04:02:11 Reply

At 2/21/14 03:12 PM, bumblefish wrote: You are gonna need to make points as "public static var points:number, then in your next class, you are gonna wanna call the first class by name like this: say your class name is classone, call classone.points.
That's a bad idea because it creates unwanted dependencies.

If you are using several classes, using static variables is a must to access a member from another class.

milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to SavedObject? 2014-02-23 07:27:05 Reply

At 2/23/14 04:02 AM, bumblefish wrote: If you are using several classes, using static variables is a must to access a member from another class.

That's just wrong.
Not all members of a class are/can be static. With that simple fact one can disprove that static member access is "a must" to get to the member of another class.

Events are a better way to wire an application up.

bumblefish
bumblefish
  • Member since: Feb. 26, 2013
  • Offline.
Forum Stats
Member
Level 05
Game Developer
Response to SavedObject? 2014-02-23 09:27:44 Reply

At 2/23/14 07:27 AM, milchreis wrote:
At 2/23/14 04:02 AM, bumblefish wrote: If you are using several classes, using static variables is a must to access a member from another class.
That's just wrong.
Not all members of a class are/can be static. With that simple fact one can disprove that static member access is "a must" to get to the member of another class.

Events are a better way to wire an application up.

Not "all" members. Only the members you wanna access in other classes.

Sam
Sam
  • Member since: Oct. 1, 2005
  • Offline.
Forum Stats
Moderator
Level 19
Programmer
Response to SavedObject? 2014-02-23 09:51:58 Reply

At 2/23/14 09:27 AM, bumblefish wrote: Not "all" members. Only the members you wanna access in other classes.

That's a bad way to look at the static keyword and will most likely lead to lazy and therefore bad designs for your games and applications. A better way to look at it would be "Do you really need to access this method/property without having to construct an object of this class?". If you have objects already made and simply throw in the static keyword so you can access it from anywhere, you're completely missing the point. If another class or object needs to know about the object(s) in question then you need to design your application so that they are passed in some way or another.

That's not to say static methods and properties don't have there place, but 90% of the time I see people using it as an easy get fix to a bad design.

milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to SavedObject? 2014-02-23 09:52:45 Reply

At 2/23/14 09:27 AM, bumblefish wrote: Not "all" members. Only the members you wanna access in other classes.

Not all members that should be accessed by other classes can be static.
As I pointed out, even if some can be, they shouldn't. Use Events.