PDA

View Full Version : [AS2] actionscript movement in flash game


natronp
02-25-2009, 10:45 PM
got a general question about the best way to do this:

I've got a flash game where you need to help a little man climb up a slippery slope. The idea is that you move him up by hitting certain keyboard keys as fast as you can.. the whole while he continues to slide back down.

Right now I've got a setInterval running which moves the man down unless you enter the right key sequence which then sets a boolean value and the set interval moves the man up .

My question is really if setInterval is the best way to control the movement of the man/check the boolean or if there's a better way to have the flash player run smoothly (not tax it) like .onEnterFrame or something.

Right now i'm using Fuse to tween the man down a number of pixels each time the setInterval fires. Is that a good route? It seems to be slightly smoother than just incrementing the ._y value of the man but still kind of jerky... anyone have a better way to smoothly move it down a set amount every couple of milliseconds?

thanks for any input people!

bluemagica
02-26-2009, 05:17 AM
you can use tween, but i myself prefer using coordinates!

Now on to the main question, Using a timer is definately the right way to go, since it gives more control, but i don't understand the use of boolean, since i think this should work the same way as we do friction for jumping..... every step we subtract speed no matter whether player enters combo or not, but if we get a correct combo, add a higher positive speed!

for example, every step we "subtract" 0.5, and on each correct entry we "add" 2, thus the person will move up as soon as you do a correct entry, otherwise he will keep moving down!

kkbbcute
02-26-2009, 10:57 AM
I don't get why changing the man's coords will cause it to look laggy, as a tween does the exact same thing (Change the coords to make it look like its moving). Anyway, coordinates give you much more control, so I recommend it.

natronp
02-26-2009, 12:33 PM
i set the boolean in the Key listener function i have which makes sure the correct key sequence was hit. If it was, then the boolean gets temporarily set to true and in the function that has an interval set, we tween/move up vs. down.

If you have a better idea, please let me know! (see code):

keyListener = new Object(); // establish listener (generic) object

keyListener.onKeyUp = function () { // when keyboard key pressed

// display code of last key pressed
trace(Key.getCode());

checkOrder(Key.getCode());
}



function checkOrder(key)
{

switch(count)
{
case 0:

count++;
if(key == 37)
{

stall=false;
}

break;
case 1:
if(key == 39)
{

stall=true;
count=0;

}

break;

}
}


Key.addListener(keyListener); // register object to receive onKeyUp

bluemagica
02-26-2009, 01:17 PM
umm in your code i don't exactly see how you are planning on checking a "sequence"! moreover, as per your code, that counter and boolean seem useless! If you want to check a sequence then use an array......

natronp
02-26-2009, 01:23 PM
each time you press a key, the check order key code increments the counter var (so we know if the key is the first key pressed or the second). If the first key is correct and the second key is correct, then i set the boolean to true.

(the project requires that the keys be hit in a certain order... left arow THEN right arrow in that order, not just alternated or hit sporadically)

elsewhere in my code that fires using setInterval, i use the boolean to either move up or move down.

It seems to work fine as far as hitting the keys in the right order.

My questions are really about how to optimize movement, and whether to use setInterval or onEnterFrame, or what to maintain motion, check vars...

kkbbcute
02-26-2009, 01:29 PM
The ironic thing is that I was about to recommend onEnterFrame, when I saw bluemagica recommending setInterval instead, the best part, our reasons are the same, it offers more control! For me, working with frames just somehow feels a whole lot more natural and allows me to be more precise because I'm sort of used to it.

natronp
02-26-2009, 01:33 PM
Yeah, i used onEnterFrame and setInterval both pretty regularly, but not in a game scenario where you've got constant input. I think bluemagica actually said 'timer' instead of 'setInterval' I'm unclear on how the Timer class would be better though. Thanks for the input!

kkbbcute
02-26-2009, 01:36 PM
Sorry, I misintepreted the timer class as a setInterval.
Anyway the best option is to experiment to see which one you are most comfortable with, there is no point programming in a way you don't find natural.

natronp
02-26-2009, 01:50 PM
There is no point programming in a way you don't find natural.

well, maybe the sake of performance.

kkbbcute
02-26-2009, 02:00 PM
well, maybe the sake of performance.

How do I put it.
Okay, here's a lame one. You can eat veggies or can choose to not eat veggies. If you eat veggies you live a longer life, and if you don't eat veggies you live a shorter life. If you are willing to trade not eating veggies for living a shorter life, because you don't like eating veggies, then no one is stopping you.

The same way, sure, you might get a performance increase, but if you don't like it and just can't stand programming with it, and you are willing to sacrifice that bit of performance, which might not be that important to you anyway, then just go ahead and use whatever you are most comfortable with. Of course that doesn't mean that you do redundant things such as executing functions which do nothing just because you feel like it.:p

bluemagica
02-26-2009, 02:09 PM
in these type of games, timer(advanced as3 version of setInterval) has a bit of advantage over onEnterFrame! if the game is graphically rich, then the FPS may not be the same everywhere, and enterFrame function will give different results cause it depends on fps rather than actual time, for example, on one pc man will slide down in say 30sec, and in another 1min. Also say he wants the player to type faster depending on level of progress and this is best done through timer.


This is almost the same thing as enterframe

var timer:Timer = new Timer(1000/30) //1000=1sec, 30 = fps
timer.addEventListener(TimerEvent.TIMER,eframe,fal se,0,true);
timer.start();

function eframe(e:TimerEvent):void{
//your code
}


Actually this thing wont be any help to you cause you are using as2(I overlooked that earlier), but if you ever move to as3, this is generally much beneficial over enterFrame( and since as3 is more programmer oriented, its best to think a bit outside the concept of frames )

kkbbcute
02-26-2009, 02:18 PM
In the first place if your game has such a high fps difference to double the time taken to do some moves, the game needs to undergo some major optimization work. Anyway most of the time when your game lags that much, I think that the time taken to do a move would be the least of your worries, and cap your fps to something like 30 so it doesn't fluctuate too much.

bluemagica
02-26-2009, 02:23 PM
during calculations we preffer a steady state, but fps do vary from machine to machine, even on a simple tetris game....use "flash debug player", and you will see!
Also its generally best to code the best way from start, rather than coding sloppyly then going back once the problems start!

kkbbcute
02-26-2009, 02:27 PM
It's not sloppy, its just that its less accurate due to the fps difference amongst different PCs. But most of the time, such fps differences as far as I'm concerned has a negligible impact on a gamer's experience, no one is going to memorize that your jump takes exactly 1.00 seconds to completely, you know what I'm saying. The moment the game starts lagging and the difference is observable, then its an indication that you need some serious code optimizations in other areas.