(Controls are Arrow Keys, plus spacebar to attack)
Hey guys I’ve been trying to program a little zelda type game, just to see how far I can get and I’ve ran into a problem. The problem is when you press say the right arrow when the down arrow is being held down, a glitch occurs and the animation stays on the down animation but the right code is executed. (Confusing but play with the swf and roll onto different arrow keys and you’ll see what I mean).
Can someone take a look and offer some advice on what to modify? Thanks
SWF: http://www.mighty.ca/clients/test/_matt/linkGame.swf
FLA: http://www.mighty.ca/clients/test/_matt/linkGame.fla
Code:
import KeyPoll;
var RIGHT:Number = 37;
var LEFT:Number = 39;
var SPACE:Number = 32;
var UP:Number = 38;
var DOWN:Number = 40;
var keyPressedRight:Boolean;
var keyPressedLeft:Boolean;
var keyPressedUp:Boolean;
var keyPressedDown:Boolean;
var keyPressedSpace:Boolean;
var doUpDown:Boolean;
var doRightLeft:Boolean;
var keyLeftDown:Boolean = false;
var keyRightDown:Boolean = false;
var keyUpDown:Boolean = false;
var keyDownDown:Boolean = false;
var keySpaceDown:Boolean = false;
var currentHit:Boolean = false;
var dir:String = "right";
var stopLeft:Boolean = false;
var stopRight:Boolean = false;
var speedIncrement:int=0;
var speed:Number = 5;
var nextx:Number = link_mc.x;
var nexty:Number = link_mc.y;
var key:KeyPoll;
key = new KeyPoll( this.stage );
addEventListener(Event.ENTER_FRAME, run);
//stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownListener);
//stage.addEventListener(KeyboardEvent.KEY_UP,keyUpListener);
function run(e:Event):void {
getKeys();
moveObjectsInMemory();
render();
}
function getKeys() {
speedIncrement=0;
if( key.isDown( Keyboard.RIGHT ) ) {
if (keyRightDown == false){
keyRightDown = true;
dir = "right";
trace("Current direction is: "+dir);
link_mc.gotoAndStop("run");
link_mc.scaleX = 1;
}
//speed += 1;
speedIncrement=speed;
} else if( key.isDown( Keyboard.LEFT ) ){
if (keyLeftDown == false){
keyLeftDown = true;
dir = "left";
trace("Current direction is: "+dir);
link_mc.gotoAndStop("run");
link_mc.scaleX = -1;
}
//speed -= -1;
speedIncrement=-speed;
} else if( key.isDown( Keyboard.UP ) ) {
if (keyUpDown == false){
keyUpDown = true;
dir = "up";
trace("Current direction is: "+dir);
link_mc.gotoAndStop("runUp");
link_mc.scaleX = 1;
}
speedIncrement=-speed;
} else if( key.isDown( Keyboard.DOWN ) ) {
if (keyDownDown == false){
keyDownDown = true;
dir = "down";
trace("Current direction is: "+dir);
link_mc.gotoAndStop("runDown");
link_mc.scaleX = 1;
}
speedIncrement=speed;
} else if( key.isDown( Keyboard.SPACE ) ) {
if (keySpaceDown == false){
keySpaceDown = true;
trace("attack!");
link_mc.gotoAndStop("attack");
}
} else {
keyLeftDown = false;
keyRightDown = false;
keyUpDown = false;
keyDownDown = false;
keySpaceDown = false;
if ((dir == "left") || (dir == "right")){
link_mc.gotoAndStop("stand");
} else if (dir == "up"){
link_mc.gotoAndStop("standUp");
} else if (dir == "down"){
link_mc.gotoAndStop("standDown");
}
}
}
function moveObjectsInMemory() {
if (dir == "right" || dir=="left"){
doUpDown = false;
doRightLeft = true;
nextx = link_mc.x + speedIncrement;
} else if (dir == "up" || dir=="down"){
doUpDown = true;
doRightLeft = false;
nexty = link_mc.y + speedIncrement;
}
if (nextx < 0) {
nextx = 550;
} else if (nextx > 550) {
nextx = 0;
}
if (nexty < 0) {
nexty = 400;
} else if (nexty > 400) {
nexty = 0;
}
}
function render() {
//if (currentHit == false){
// if (link_mc.hitTestObject(cabin_mc) == true){
// if (dir == "left"){
// currentHit = true;
// trace("stuck going left");
// stopLeft = true;
// stopRight = false;
// } else if (dir == "right"){
// currentHit = true;
// trace("stuck going right");
// stopLeft = false;
// stopRight = true;
// }
// speed = 0;
// }
// }
if (doRightLeft){
link_mc.x = nextx;
} else if (doUpDown){
link_mc.y = nexty;
}
}
ninjateacup