PDA

View Full Version : [AS3] Mario game help


Michael8818
04-16-2009, 08:42 PM
Im in the begining stages of making a mario game for a midterm in my class. I have begun my character movement and now have him moveing left to right, I also have a main mario mc and inside that all his positions including his walk cycle and when the left key is pressed I want him to move left and go through his walk left cycle. The problem is that when the left key is pressed he does not go to the second part of his walk cycle (it has two parts). I know why it is doing this but dont know how to fix it. here is my code and my fla file.

var leftKeyDown:Boolean=false;
var rightKeyDown:Boolean=false;
var marioSpeed:Number=4;

Mario.addEventListener(Event.ENTER_FRAME, moveMario);
function moveMario(event:Event):void {
if (leftKeyDown) {
Mario.gotoAndPlay("walk left");
Mario.x-=marioSpeed;
}
if (rightKeyDown) {
Mario.gotoAndPlay("walk right");
Mario.x+=marioSpeed;
}
}

stage.addEventListener(KeyboardEvent.KEY_DOWN,chec kKeyDown);
function checkKeyDown(event:KeyboardEvent):void {
if (event.keyCode == 37 || event.keyCode == 65) {
leftKeyDown = true;
}
if (event.keyCode == 39 || event.keyCode == 68) {
rightKeyDown = true;
}
}

stage.addEventListener(KeyboardEvent.KEY_UP, checkKeyUp);
function checkKeyUp(event:KeyboardEvent):void {
if (event.keyCode == 37 || event.keyCode == 65) {
Mario.gotoAndPlay("still left");
leftKeyDown = false;
}
if (event.keyCode == 39 || event.keyCode == 68) {
Mario.gotoAndPlay("still right");
rightKeyDown = false;
}
}

Michael8818
04-16-2009, 08:57 PM
Im in the begining stages of making a mario game for a midterm in my class. I have begun my character movement and now have him moveing left to right, I also have a main mario mc and inside that all his positions including his walk cycle and when the left key is pressed I want him to move left and go through his walk left cycle. The problem is that when the left key is pressed he does not go to the second part of his walk cycle (it has two parts). I know why it is doing this but dont know how to fix it. here is my code and my fla file.

var leftKeyDown:Boolean=false;
var rightKeyDown:Boolean=false;
var marioSpeed:Number=4;

Mario.addEventListener(Event.ENTER_FRAME, moveMario);
function moveMario(event:Event):void {
if (leftKeyDown) {
Mario.gotoAndPlay("walk left");
Mario.x-=marioSpeed;
}
if (rightKeyDown) {
Mario.gotoAndPlay("walk right");
Mario.x+=marioSpeed;
}
}

stage.addEventListener(KeyboardEvent.KEY_DOWN,chec kKeyDown);
function checkKeyDown(event:KeyboardEvent):void {
if (event.keyCode == 37 || event.keyCode == 65) {
leftKeyDown = true;
}
if (event.keyCode == 39 || event.keyCode == 68) {
rightKeyDown = true;
}
}

stage.addEventListener(KeyboardEvent.KEY_UP, checkKeyUp);
function checkKeyUp(event:KeyboardEvent):void {
if (event.keyCode == 37 || event.keyCode == 65) {
Mario.gotoAndPlay("still left");
leftKeyDown = false;
}
if (event.keyCode == 39 || event.keyCode == 68) {
Mario.gotoAndPlay("still right");
rightKeyDown = false;
}
}

Sorry for post outside of game development section I wasn't getting an answer.

jsimpson
04-16-2009, 09:06 PM
What do you mean by second part of his walk cycle?

Michael8818
04-16-2009, 09:32 PM
I mean his walk cycle is the first five frames of the walk cycle is him front foot up and from frame 5 to 10 it is him with his back foot up. look at my source file if the way I explained it is a bit confusing.

EightySeven
04-16-2009, 11:08 PM
You had animation all confused. you shouldn't do animations based on key presses.

I changed you code to use animation states which makes it easy to do and cancel.

You also might wan to work on your structure, you shouldn't use keyframes for your code

Michael8818
04-17-2009, 12:15 AM
Thank you eighty seven your code works just like I wanted it to, ill be sure to work on what you suggested one question though should that effect my crouch stance or my jumping stance?