PDA

View Full Version : Undoubtedly Simple Fix Help Please



BansheeIndian
March 1st, 2009, 01:37 PM
Hey guys

I'm working on a 2D sidescrolling platform game for my programming class and I've pretty much just started, I've been working on the character movement, right now I've got left and right movement to work easy peasy, the problem I'm having right now is that obviously i want it to play a move clip when I move left and right so that it looks like he's running, but whenever I hold down the left or right arrows it plays the movie clip once and then stops at the end, so essentially he takes two steps and then slides until you let go of the button.

I've really been pulling my hair out over this one because I'm sure it's an easy fix, if you have any ideas I'm willing to try anything, I've literally exhausted myself trying to figure this out.

Here's my code


var moveRight:Boolean = false;
var moveLeft:Boolean = false;
var speed:Number = 10;

frank.scaleX=.2;
frank.scaleY=.2;

stage.addEventListener(KeyboardEvent.KEY_DOWN, moveFunction);
stage.addEventListener(KeyboardEvent.KEY_UP, stopFunction);
stage.addEventListener(Event.ENTER_FRAME, Start);

function moveFunction(evt:KeyboardEvent):void{
if(evt.keyCode==Keyboard.RIGHT){
moveRight=true;
frank.gotoAndStop(2);
}
if(evt.keyCode==Keyboard.LEFT){
moveLeft=true;
frank.gotoAndStop(2);
}
}

function stopFunction(evt:KeyboardEvent):void{
if(evt.keyCode==Keyboard.RIGHT){
moveRight=false;
frank.gotoAndStop(1);
}
if(evt.keyCode==Keyboard.LEFT){
moveLeft=false;
frank.gotoAndStop(1);
}
}

function Start(evt:Event):void{
if(moveRight){
frank.x+=speed;
frank.scaleX=.2;
}
if(moveLeft){
frank.x+=-speed;
frank.scaleX=-.2;
}
}It's really basic right now and that's whats getting under my skin becuase I'm sure it's just some easy fix I overlooked.

Thanks for reading.

~Cody Sims

MurtenSaerbi
March 1st, 2009, 05:26 PM
Well, right now your "moveFunction" gets called once, namely the time when the user presses the left or right arrow key. Then it goes to frame 2 with your gotoAndStop action and since it's already on frame 2 nothing happens anymore.

You should make a clip that is a loopable (maybe "frank" is already such a clip) and just do gotoAndPlay(1) or even play(). When you want it to stop just use "frank.stop();".

If you have a stopframe (like legs just standing there you could use a more complex structure.
Inside "frank" I would define a walking cyclus (say frame 2 ---> frame 6) and a stop frame (frame 1). Frame 6 holds an actionscript that SKIPS frame 1 (the stopframe) and plays 2 until 6 again. Once you let go of the key it triggers the "gotoAndStop(1)" and your character will stop.

Fidodo
March 1st, 2009, 06:06 PM
Here's what I do in my games, and it works really well. Instead of having a different animation on each frame (which gets really confusing later when you have more animations) create a separate movie clip for each animation and export it for actionscript. Create a movie clip called animation for the character class and when you want to start a new animation swap it for another one. Here's what I do:



function setAnimation(clip:MovieClip):void {
if (animation == null || animation.constructor != clip.constructor) {
if (animation != null && contains(animation)) {
removeChild(animation);
}
animation = clip;
addChild(animation);
}
}


This also has the added functionality of not swapping an animation if it's the same one, so if you call setAnimation(new WalkAnimation()) multiple times in a row it wont interrupt it.

BansheeIndian
March 1st, 2009, 08:39 PM
Hey guys thanks for the responses you're both life savers I'm gonna mess around with this some more tonight if my back ever quits killing me (can sleeping weird debilitate you for an entire day?? getting older sucks...)

Thanks again

~Cody Sims