PDA

View Full Version : [AS2] Ball Stop on Next KeyDown


Wesley008
04-14-2008, 12:18 AM
_global.bspeed = 25;
Player.onEnterFrame = function() {
var speed = 3;
var gravity = .5;
if (Key.isDown(Key.UP)) {
Player._y -= speed;
}
if (Key.isDown(Key.DOWN)) {
Player._y += speed;
}
if (Key.isDown(Key.LEFT)) {
Player._x -= speed;
}
if (Key.isDown(Key.RIGHT)) {
Player._x += speed;
}
Bullet._y -= bspeed;
Player._y += gravity;
};

keyListen = new Object();
keyListen.onKeyDown = function() {
if (Key.getCode() == Key.SPACE) {
var bulletx = Player._x;
var bullety = Player._y-Player._height+10;
_global.Bullet = attachMovie("mcBullet", "mcBullet", _root.getNextHighestDepth(), {_x:bulletx, _y:bullety});
}
};
Key.addListener(keyListen);

Heres my code for the game. Whats happening in this is when I press spacebar it generates the Bullet Movie and positions it at the tip of the Player, Then at the same time it Moves forward for the Bullet Effect but now my problem accors, instead of creating a new bullet to follow the bullet before it, it stops the one in motion and the new one that just created with the Spacebar goes in motion, and so on so forth with each spacebar I press.

ASWC
04-14-2008, 04:18 AM
var numberOfBullet:Number = 0;
_global.bspeed = 25;
Player.onEnterFrame = function() {
var speed = 3;
var gravity = .5;
if (Key.isDown(Key.UP)) {
Player._y -= speed;
}
if (Key.isDown(Key.DOWN)) {
Player._y += speed;
}
if (Key.isDown(Key.LEFT)) {
Player._x -= speed;
}
if (Key.isDown(Key.RIGHT)) {
Player._x += speed;
}
Bullet._y -= bspeed;
Player._y += gravity;
};

keyListen = new Object();
keyListen.onKeyDown = function() {
if (Key.getCode() == Key.SPACE) {
numberOfBullet+=1;
var bulletx = Player._x;
var bullety = Player._y-Player._height+10;
_global.Bullet = attachMovie("mcBullet"+numberOfBullet, "mcBullet", _root.getNextHighestDepth(), {_x:bulletx, _y:bullety});
if(numberOfBullet>30)//set the max number of bullet
{numberOfBullet=0;}
}
};
Key.addListener(keyListen);