PDA

View Full Version : [AS3] Frames seem to ignore what I'm telling them


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

Redjinator
10-06-2009, 05:25 AM
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 }}



PS: I tried posting this in the beginner section, but due to the lack of response out of the amount of people that read the thread, I assume nobody knew the answer, so I have resorted to looking for answers to this obviously advanced problem here with you experts! =D

lol, anyway, thanks for any help guys, it will be much appriciated!

Redjinator
10-08-2009, 05:53 AM
Don't know why I'm bothering to update my thread since only the main Actionscript 3.0 section gets any attention here (good moderation would suggest almagamation of some sections to increase efficiency, and public learning since their questions outside of that main section would actually get answered)

But here is my current solution to the problem I posted here. Not the BEST solution, but currently works well enough that I can continue and come back to perfect it later.

================================================== ====

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;

var animateTimer:Timer = new Timer(1000);
animateTimer.start();

stage.addEventListener(KeyboardEvent.KEY_DOWN, startHeroMovement);
stage.addEventListener(KeyboardEvent.KEY_UP, stopHeroMovement);
stage.addEventListener(Event.ENTER_FRAME, moveHero);
stage.addEventListener(KeyboardEvent.KEY_DOWN, animateHero);

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 stopHeroMovement(evt:KeyboardEvent):void {
if (evt.keyCode == 37) { left = false; player.gotoAndStop(1); stage.addEventListener(KeyboardEvent.KEY_DOWN, animateHero) }
else if (evt.keyCode == 38) { up = false; player.stop() }
else if (evt.keyCode == 39) { right = false; player.gotoAndStop(21); stage.addEventListener(KeyboardEvent.KEY_DOWN, animateHero) }
else if (evt.keyCode == 40) { down = false; player.stop() }
}


function moveHero(evt:Event):void {
if (left) { player.x -= heroSpeed }
else if (right) { player.x += heroSpeed }
else if (up) { player.y -= heroSpeed }
else if (down) { player.y += heroSpeed }
}

function faceDirection(val:String):void {
if (val == 'left') { player.gotoAndPlay(1); stage.removeEventListener(KeyboardEvent.KEY_DOWN, animateHero) }
else if (val == 'right') { player.gotoAndPlay(21); stage.removeEventListener(KeyboardEvent.KEY_DOWN, animateHero) }
}

function animateHero(evt:KeyboardEvent):void {
if (evt.keyCode == 37) { faceDirection('left') }
else if (evt.keyCode == 39) { faceDirection('right') }
}

================================================== ==

rrh
10-08-2009, 04:44 PM
Interesting choice to make a new function triggered by the KEY_DOWN event and then checks the value of keyCode, rather than fold this functionality into your existing function startHeroMovement.

When posting your code, it helps to put it inside [ as ] or [ code ] tags, so it retains the indentation.