PDA

View Full Version : Prevent Flooding



tophat
February 8th, 2007, 05:49 PM
I'm making a mario-like platform game, and when you press spacebar, it shoots fireballs. The only problem is, if you hold down spacebar, they just endlessly come out until you stop pressing it. Is there any way to control this?

Hansol
February 8th, 2007, 06:18 PM
I'm making a mario-like platform game, and when you press spacebar, it shoots fireballs. The only problem is, if you hold down spacebar, they just endlessly come out until you stop pressing it. Is there any way to control this?

im kinda newb at flash so i cant give you the exact code cause i need references, but i know you can but a interval so it sets a time and when the times up it removes the bullet, during the time a bullet shouldnt not be able to be created. I can think it up just no sure the exact thing to type. : \

bombsledder
February 8th, 2007, 10:04 PM
var keyhandler:Object= {}
keyhandler.onKeyDown=function(){
if(Key.isDown(Key.SPACE)){
// Firing code here
}
}
Key.addListener(keyhandler)


should do the trick

SacrificialLamb
February 9th, 2007, 12:37 AM
i would use some thing like this. it's like interval but i don't like coding them

t=0
onEnterFrame = function() {
if(Key.isDown(Key.SPACE) && t == 0){
t=12 // @ 24fps this is every half second
// Firing code here
} else {
t--
}
}

bombsledder
February 9th, 2007, 07:17 AM
this is how i would write it with intervals



var keyhandler:Object = {};
var canShoot:Boolean = true;
var interval:Number = null;
var time:Number = 1400;
keyhandler.onKeyDown = function() {
if (Key.isDown(Key.SPACE)) {
if (canShoot) {
trace("shoot here");
canShoot = false;
_root.interval = setInterval(function () {
canShoot = true;
clearInterval(_root.interval);
}, time);
}
}
};
Key.addListener(keyhandler);

tophat
February 11th, 2007, 09:12 PM
thanks for the help but none of these codes came close to working. in fact, they made it worse :/

bombsledder
February 11th, 2007, 10:45 PM
both codes should work fine, post your code

NiñoScript
February 11th, 2007, 10:54 PM
why aren't they working?, they should... (at least SacrificialLamb's way, which is the only one i read)

try posting your code

tophat
February 12th, 2007, 06:10 PM
sorry about that. should've put that from the start :)


on (keyPress "<Space>") {

ran = random(1000);
_root.ran = ran;

duplicateMovieClip(_root.pill, "pill"+ran, +ran);
_root["pill"+ran]._x=_root.mario._x;
_root["pill"+ran]._y=_root.mario._y-20; // 10
_root["pill"+pc]._alpha=random(100); // Just for different alphas

sound = new Sound(this);
sound.attachSound("fireball");
sound.start(0,0);

}
The only reason I used random(1000) was because using increasing increments clashed with another part of my AS. Random works just as fine.

Oh, and by the way, when I used the codes you supplied, I put them in onClipEvent(enterFrame) not on(keyPress "<Space>") ;)

bombsledder
February 12th, 2007, 09:45 PM
var keyhandler:Object = {};
var canShoot:Boolean = true;
var interval:Number = null;
var time:Number = 1400;

function shoot(){
ran = random(1000);
_root.ran = ran;
var mc = _root.duplicateMovieClip(_root.pill, "pill"+ran, +ran);
mc._x=_root.mario._x;
mc._y=_root.mario._y-20; // 10
mc._alpha=random(100); // Just for different alphas

sound = new Sound(this);
sound.attachSound("fireball");
sound.start(0,0);

}
keyhandler.onKeyDown = function() {
if (Key.isDown(Key.SPACE)) {
if (canShoot) {
_root.shoot()
canShoot = false;
_root.interval = setInterval(function () {
canShoot = true;
clearInterval(_root.interval);
}, time);
}
}
};
Key.addListener(keyhandler);



shouldnt have any problems if your code you had before any others worked aswell