Problem with Key events

OK folks, I have a problem with that game I’m doing for the battle (don’t laugh). I have this function in the main timeline:

function listenForKeys(){
	if (Key.isDown(Key.RIGHT)){
		hero.xmov+=hero.moveIncrement;
	}
	if (Key.isDown(Key.LEFT)){
		hero.xmov-=hero.moveIncrement;
	}
	if (Key.isDown(Key.UP)){
		hero.ymov-=hero.moveIncrement;
	}
	if (Key.isDown(Key.DOWN)){
		hero.ymov+=hero.moveIncrement;
	}
	if (Key.isDown(Key.SPACE)){
		missileLaunch();
	}
}

and I call it onEnterFrame, so that when I press and hold space, a new missile is launched. Which is not very realistic. What I’d like to do is set a maximum frequency for the missiles.

I thought about setInterval, but I would need to detect the moment I release the space key to clear the interval.

Does anybody see an easy way of doing that?

pom :trout:


Key.addListener(this);
this.onKeyUp = function(){
	if(!Key.isDown(Key.SPACE)) clearInterval(missle_interval)
}

?

Well, the problem is that I have the space bar and the 4 arrows. So each time I release the arrow keys, that will clear the interval, no?

no it wont clear the interval unless space is not pressed [if(!Key.isDown(Key.SPACE))]. If you release an arrow key, yes, the onKeyUp will be called, but the interval will no be cleared unless the SPACE is not being pressed… and if its not the key being released, it wont matter because the interval wont be set anyway.

:trout: <-- me
Dammit you’re right. Now why did I think I couldn’t do it that way? :hangover: :hangover:

Thanks Sen :slight_smile:

np :slight_smile:

glad you got it all figured out