Minor difficulties in AS3 (solved-thanks moonkey and 8bityoda!)

Subscribe to Minor difficulties in AS3 (solved-thanks moonkey and 8bityoda!) 6 posts

Sign in to reply


 
avatar for Kreg Kreg 85 posts
Flag Post

Hey everyone! First off, thanks so much for being so helpful, I come here whenever I can’t figure out what the heck is going on with my code, and I always get timely and extremely helpful information from these forums. You all rock!

I’m having a couple of minor problems right now… I have my game set to run off of a timer, instead of keyframes, and I have the timer set to go off every 25 milliseconds. The scoring of the game is going to be in seconds, so in the timer function, I set up this function:

secondsLasted+=25;

then, I pass the value like so:

.scoretext.text=String(Math.floor(secondsLasted/1000));

now, this does work, but it goes WAY slower than it should. In order for it to count at about a second, I have to divide secondsLasted by about 650. Any ideas on why that would be? Does my computer just go too slow to accurately run through the functions once every 25 milliseconds?

Other trouble I’m having:
I’ve got the lifebar working, and I’m using a scaleX function to get it to reduce. The problem is that I’m using a high quality lifebar that isn’t a perfect rectangle. (it’s slanted on the edges). Also, it switches colors from red, to yellow, to green. With the scaleX property, it doesn’t just chop off the right side of the bar, it squishes everything together. The end effect, is that the left slanted side becomes more “boxlike” as health is lost, and all three colors just squish together, heading for the left side. Is there another flash function that anyone has found works better than scaleX to work with lifebars like that?

As always, thanks so much in advance! This really is a great community!

-Kreg

 
avatar for Moonkey Moonkey 1007 posts
Flag Post

The timer thing does seem weird. I haven’t seen them be so inaccurate before. If you want a more precise way, you’d be better off using getTimer()
That’ll give you the number of milliseconds since the swf started. If you record the time at the start of the game/level you can just subtract that from the current time to get how long you’ve lasted.

As for the health bar, it sounds like a mask would be best. Make another Sprite the same shape as your health bar (or just use another instance of your health bar symbol), and set that to your healthbar’s .mask property. Then move the mask left to reduce the health, for example

healthBar.mask = healthMask;

...

healthMask.x = healthBar.x - healthMask.width * (1 - health / maxHealth);
 
avatar for the8bitYoda the8bitYoda 79 posts
Flag Post

use flash.utils.getTimer() to get the number of ms since the flash virtual machine was started. so instead of


secondsLasted+=25;

//Use
secondsLasted=getTimer()-startTime;


Just save the startTime variable when the user clicks the ‘Play Game’ button or whatever. The Timer class isn’t guaranteed to execute at the rate you specify, it’s tied to the frame rate of the game and can only be fired 10 times per frame, so it can change if your frame rate drops due to lag or whatever. It might not be your problem, but the Timer is not the way to reliably get seconds elapsed.

 
avatar for Kreg Kreg 85 posts
Flag Post

Man, thanks so much! as always, these forums continue to be probably my most helpful resource when I can’t find the answer anywhere else!

 
avatar for UnknownGuardian UnknownGuardian 6220 posts
Flag Post

I heard somewhere its faster/better to use an Enter_Frame rather than a Timer. Might want to check that out to see if your lag decreases just a bit.

 
avatar for I_wear_shoes I_wear_shoes 44 posts
Flag Post

I always heard a timer based loop was better, although I don’t really see what the difference is between them.

Sign in to reply