PDA

View Full Version : [AS3] Character Movement


Zend
03-02-2009, 03:10 AM
I'm very confused.. I've reviewed and reviewed and reviewed my code and I can't find any problems. But for some reason my guy will only move 5 pixels away and stop, but when I release the arrow key he will return to his original position.

I know it's long and probably looks very intimidating, but if someone can check out my code and see if they know whats wrong I would be forever grateful. There is a downloadable preview of my project attached to this thread.

:)

runawayprisoner
03-02-2009, 03:22 AM
Well, here's the culprit:


function levelCheck(event:Event):void{
if (currentFrame < 5){
currentLevel = currentFrame;
prevLevel = currentFrame - 1;
oneTime();
}
}

function oneTime():void{
if (currentLevel > prevLevel){
positionReset();
prevLevel += 1
}
}


Since the currenFrame is always less than 5, it always does the currentLevel = currentFrame and then prevLevel = currentFrame - 1 operations.

Then goes on to check oneTime.

Guess what? prevLevel would ALWAYS be less than currentLevel because of the above 2 operations, so it just resets your character's position every single time infinitely.

I'm not too sure what you'd like to do here, so I'd leave the fixing to you. But the game works fine if you take out the listener and those two functions, then proceed as usual, reach the goal, and the win screen comes up.

Also, try to use multiple booleans so you can recognize multiple key presses at the same time instead of just one keyCode at a time (1 key press at a time).

...I think I might well write a tutorial on that because there has been a fair amount of people asking about how to recognize more than 2 key presses at the same time.

Zend
03-02-2009, 03:31 AM
Oh, so basically you saying that because currentFrame is always less then 5 it will always be making prevLevel 1 less then currentLevel even though I have prevLevel += 1; in the function oneTime? So it infinitely resets his position?

Well what I'm trying to do is make it so the function still works in the upcoming levels. That way it will reset his position in the next level as well. I already tried taking that function and just inserting it into the next frame since I don't plan on adding any levels after that but it would do the function winLose first and continue to the Congratulations frame before you got a chance to even play the second level.

Sorry if that didn't make a whole lot of sense, this stuff is hard to explain :\

But thanks so much for the help :) I'll see what I can do to fix it.

[Edit]
Fixed it, thanks again for the help :)