PDA

View Full Version : [AS3] Variables + Text Fields


Sythius
07-11-2009, 05:54 PM
Right, I need some help people! :D
I'm making an RTS flash game. It was working out in AS2 alright.
But when I asked here about if I could do something, I was told it's easier in AS3.
So I switched.
But since then I've been trying to wrap my mind around AS3. With little luck.
This (http://www.megaupload.com/?d=1YZQOVT0)is a link for a .fla, Flash 8 Pro, AS2. It's the template of my resource coding.
In the proper game, there's going to be several symbols effecting several different variables. As the game progresses, more things will affect the variables. - and + to them.

Plus, can someone help me with my other problem in my game?
It was the reason I switched over.

Okay. So. I want my workers to have an effect. Each time 1 is created. I want it to minus away from a variable. I want the minuses to stack. These minuses will be timed. So each one minuses say every 30 seconds.

Thanks in advance.

runawayprisoner
07-11-2009, 07:25 PM
Ho... a 1-second interval?

Try a timer. I think you'll want something like this, though:


public class Worker {
public static const SPRITE:MovieClip = /*-- Reference to the embedded or loaded MovieClip here --*/;
public static const DEFAULT_HP_MAX:uint = 40;
public static const DEFAULT_TP_MAX:uint = 20;
public static const DEFAULT_ATTACK:uint = 1;
public static const DEFAULT_SHIELD:uint = 0;
public static const DEFAULT_STAMINA:uint = 50;

public static var COUNT:uint; // To keep track of how many we have created

public var hp:uint = DEFAULT_HP_MAX;
public var tp:uint = DEFAULT_TP_MAX;
public var at:uint = DEFAULT_ATTACK;
public var sh:uint = DEFAULT_SHIELD;
public var st:uint = DEFAULT_STAMINA; // This is the variable from which we will subtract 1 every second
public var upgrades:Array; // Well, I'm out of ideas

private var timer:Timer;

public function Worker ()
{
COUNT++;
timer = new Timer(1000); // Create a timer object
timer.addEventListener("timer", timerHandler); // Attach the handler to the tim
timer.start();
}

public function defeated () // Call this when the worker's hp reaches 0
{
if (hp == 0)
{
if (timer != null) timer.stop();
}
}

private function timerHandler ()
{
st--; // Minus 1 from st every second
}
}


Then... hmm... I think that's all you'll need. Embedding MovieClip... you should know that from working with Flash 8. Loading is a different story, and you might want to consult more articles. Alternatively, create a separate class file similar to that one, take out the SPRITE constant variable, then embed the class file into the MovieClip of the Worker, and then load it into the main game as needed.

If all of that just slipped right through, then... well, welcome to AS 3.0 and pure object-oriented programming.

If you want to reconsider it, AS 2.0 can use the setInterval method to start a countdown. If the difference between a few milliseconds is not significant to you, use a global timer and decrement the variable of each individual worker using that instead as it'll be much less intensive than doing it this way, even if it's less accurate.

Sythius
07-11-2009, 08:16 PM
Whoa..
That's a lot of stuff. Haha.
Thank you for your help.
I was in quite a rush when I wrote the thread.
My problem is simpler than the coding you've given me there, I think...

Let me explain myself now that I'm not rushed.
Okay, this game is just an economical RTS. Not a combat one.You got to manage several resources.
Current Workers.
Available Workers. (Each time you build something, etc, it'll require an amount of workers until it's done, plus a certain amount of workers will be needed to work the farms, factories, etc.)
Food. (Used to purchase more workers, each worker will minus an amount from this every so often, if they can't. A worker will "die".)
Wood. (Used to construct buildings, etc.)
Metal. (Same as wood, but for better buildings, upgrading buildings, etc, both commonly will be needed on buildings.)
Power. (The buildings such as factories will need to be powered. Generators and Plants shall do this.)
Pollution. (Some buildings increase pollution. Others decrease. If it gets to 100%. Everyone on the planet has been killed due to lack of oxygen.)
Valcronz. (Currency. Also used to buy workers, buildings, etc. The most needed resource, as buildings need to be maintained, workers need to be paid.)

Now this is quite easy to do, or will be once I can get variables. And those variables are increased or decreased due to the above said things. The variables will be displayed upon Dynamic Text Fields. My second issue was that. When a worker is created. All it will do is increase the Maximum Worker variable, and the Available worker variable. That is it. But I also want it to do the said minuses to the gold (valcronz) and Food variables on an endless timer, always minus-ing some every now and then.

I was planning to originally use a movieclip. that would be empty till a certain keyframe with AS coding, which will pretty much have If statements, checking if there is enough of the gold (valcronz) and food. If either weren't. Then I was going to have it minus 1 from both available and maximum workers. My issue being I couldn't have the same named instance of the movieclip several times.