Redjinator
10-05-2009, 08:57 PM
Ok, so I'm trying to build a type of mini game where you walk around like those old school double dragon games. However the background is stationary, and not scrolling (yet) because I'm in a learning process, and even though I won't use this in my game, i wanna know how to get the player to move in all directions smoothly, and also have an animation for the direction he's walking in.
So I have my Hero Movieclip, instance named player. The Hero MC has 40 frames. With Keyframes on 1, 11, 21, 31.
Hero MC Frames 1-10 are standing still, facing left.
Hero MC Frames 11-20 are with one leg outwards facing left.
Hero MC Frames 21-30 are standing still, facing right.
Hero MC Frames 31-40 are with one leg outwards facing right.
2 points before I continue. Yes, I know I could have done this in 4 frames, and I'm going to change it to that once I get my animations to work correctly. And second, I know 2 frames for walking is kinda bad, but I am trying to emulate an old 8bit game, so it works perfect for what I'm doing.
Now my problem isn't getting the player to move in different directions, I know how to do this. But if he's facing right or left already, and I make him walk in the direction he is pointing, it works out fine. But if he's facing right lets say, and I make him walk to the left, he gracefully does the magic genie, and float backwards across the screen.
As cool as magic genies are, this is not my desired result.
As far as I can tell in my code, I'm telling my MC that if I press the left arrow key, to check and see if my MC is already facing to the left (checking to see if the MC is on frame 1) and if it is, to play the animation from 1 to 20. And if it's NOT on frame 1 (for instance if the MC is facing to the right) than the MC should go to and stop on frame 1, and since I'm still holding the key down, I assume that it will run thought that if statment again, and realize it's on frame 1 now, and this time play the clip from 1-20.
I hope that all makes sense. Anyway, here is my code.
var myBG:BG = new BG();
myBG.x = 0;
myBG.y = 0;
addChild(myBG);
var player:Hero = new Hero();
player.x = 250;
player.y = 250;
addChild(player);
var heroSpeed:Number = 5;
player.gotoAndStop(1);
var left:Boolean = false;
var right:Boolean = false;
var up:Boolean = false;
var down:Boolean = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, startHeroMovement);
stage.addEventListener(KeyboardEvent.KEY_UP, stopHeroMovement);
stage.addEventListener(Event.ENTER_FRAME, moveHero);
function startHeroMovement(evt:KeyboardEvent):void {
if (evt.keyCode == 37) { left = true; }
else if (evt.keyCode == 38) { up = true; player.play() }
else if (evt.keyCode == 39) { right = true; }
else if (evt.keyCode == 40) { down = true; player.play()}
}
function doNothing():void { }
function stopHeroMovement(evt:KeyboardEvent):void {
if (evt.keyCode == 37) { left = false; player.gotoAndStop(1); }
else if (evt.keyCode == 38) { up = false; player.stop(); }
else if (evt.keyCode == 39) { right = false; player.gotoAndStop(21); }
else if (evt.keyCode == 40) { down = false; player.stop(); }
}
function moveHero(evt:Event):void {
if (left) { player.x -= heroSpeed;if (player.currentFrame == 1) { player.play()} else { gotoAndStop(1)}}
else if (right) { player.x += heroSpeed; if (player.currentFrame == 21) { player.play()} else {gotoAndStop(21)}}
else if (up) { player.y -= heroSpeed }
else if (down) { player.y += heroSpeed }}
thanks for any info, I'm really stumpted
So I have my Hero Movieclip, instance named player. The Hero MC has 40 frames. With Keyframes on 1, 11, 21, 31.
Hero MC Frames 1-10 are standing still, facing left.
Hero MC Frames 11-20 are with one leg outwards facing left.
Hero MC Frames 21-30 are standing still, facing right.
Hero MC Frames 31-40 are with one leg outwards facing right.
2 points before I continue. Yes, I know I could have done this in 4 frames, and I'm going to change it to that once I get my animations to work correctly. And second, I know 2 frames for walking is kinda bad, but I am trying to emulate an old 8bit game, so it works perfect for what I'm doing.
Now my problem isn't getting the player to move in different directions, I know how to do this. But if he's facing right or left already, and I make him walk in the direction he is pointing, it works out fine. But if he's facing right lets say, and I make him walk to the left, he gracefully does the magic genie, and float backwards across the screen.
As cool as magic genies are, this is not my desired result.
As far as I can tell in my code, I'm telling my MC that if I press the left arrow key, to check and see if my MC is already facing to the left (checking to see if the MC is on frame 1) and if it is, to play the animation from 1 to 20. And if it's NOT on frame 1 (for instance if the MC is facing to the right) than the MC should go to and stop on frame 1, and since I'm still holding the key down, I assume that it will run thought that if statment again, and realize it's on frame 1 now, and this time play the clip from 1-20.
I hope that all makes sense. Anyway, here is my code.
var myBG:BG = new BG();
myBG.x = 0;
myBG.y = 0;
addChild(myBG);
var player:Hero = new Hero();
player.x = 250;
player.y = 250;
addChild(player);
var heroSpeed:Number = 5;
player.gotoAndStop(1);
var left:Boolean = false;
var right:Boolean = false;
var up:Boolean = false;
var down:Boolean = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, startHeroMovement);
stage.addEventListener(KeyboardEvent.KEY_UP, stopHeroMovement);
stage.addEventListener(Event.ENTER_FRAME, moveHero);
function startHeroMovement(evt:KeyboardEvent):void {
if (evt.keyCode == 37) { left = true; }
else if (evt.keyCode == 38) { up = true; player.play() }
else if (evt.keyCode == 39) { right = true; }
else if (evt.keyCode == 40) { down = true; player.play()}
}
function doNothing():void { }
function stopHeroMovement(evt:KeyboardEvent):void {
if (evt.keyCode == 37) { left = false; player.gotoAndStop(1); }
else if (evt.keyCode == 38) { up = false; player.stop(); }
else if (evt.keyCode == 39) { right = false; player.gotoAndStop(21); }
else if (evt.keyCode == 40) { down = false; player.stop(); }
}
function moveHero(evt:Event):void {
if (left) { player.x -= heroSpeed;if (player.currentFrame == 1) { player.play()} else { gotoAndStop(1)}}
else if (right) { player.x += heroSpeed; if (player.currentFrame == 21) { player.play()} else {gotoAndStop(21)}}
else if (up) { player.y -= heroSpeed }
else if (down) { player.y += heroSpeed }}
thanks for any info, I'm really stumpted