PDA

View Full Version : firing in defender game


mrsharm
05-01-2003, 12:08 PM
hello,
i'm new to actionscript and i've got this problem.
i am having trouble working out a way to limit the fire on my spaceship so that it only fires a certain amount of shots before stopping for a moment. What i really need is a way of getting Flash to check whether the control key is up instead of down.
there are two movie clips. One is the spaceship,one is the laserfire. This code is from Flashkit by the way.
heres the code

onClipEvent(load)
{
movespeed=7;
_root.laser._visible=false;
laserCounter=1;
fireburst=1
}
onClipEvent(enterFrame)
{

if (Key.isDown(Key.UP))
{
this._y-=movespeed;
}
else if(Key.isDown(Key.DOWN))
{
this._y+=movespeed;
}
if (Key.isDown(Key.LEFT))
{

this._x-=movespeed;
}
else if(Key.isDown(Key.RIGHT))
{
this._x+=movespeed;
}
if (Key.isDown(Key.CONTROL))
{ laserCounter++;
_root.laser.duplicateMovieClip( "laser"+laserCounter, laserCounter );
_root["laser"+laserCounter]._visible=true;

}

}


}




The problem is that i am currently just getting a stream of bullets. I need to be able to put a break in the the shots fired so that a number of shots can be fired while the button is pushed down but then the bullets should stop, at which point the button would need to be de-pressed again in order to activate more fire. Im sure anyone familiar with defender-type games knows what i'm getting at!

mrsharm
05-01-2003, 12:09 PM
by the way thanks in advance

Ricod
05-01-2003, 12:57 PM
Hi ! Welcome to the board !

The code is from Flashkit ? Oh, then sorry, we can't help you.



...



j/k ! btw, if you want to add something to your post, you can always press the 'edit' button under it. No need to post a reply for a single line of comment. ;)

Also, if you post as code, plase it between as-tags [ as ] and [ /as ] without spaces ofcourse.

You're already checking if the control key is down :
if (Key.isDown(Key.CONTROL))
{ laserCounter++;
if (laserCount < 5) {
_root.laser.duplicateMovieClip( "laser"+laserCounter, laserCounter );
_root["laser"+laserCounter]._visible=true;
}else if (laserCounter >= 10){
laserCounter = 0;
}
}
If I'm correct, you will now fire 5 shots, then wait 6 frames and then shoot again when the control key is held down. Play with the values until it 'feels' right. :)

mrsharm
05-02-2003, 02:08 AM
Thx Ricod, I really should have been able to work that one out. All working fine now.
thx again

Ricod
05-02-2003, 12:43 PM
You're welcome !

Yes. Probably, but sometimes you've just been staring at the same piece of code for too long to spot those little things. :)

mrsharm
05-03-2003, 02:26 AM
i still have one small problem
here goes



onClipEvent(load)
{
movespeed=7;
_root.laser._visible=false;
laserCounter=1;
fireburst=1
}
onClipEvent(enterFrame)
{

if (Key.isDown(Key.UP))
{
this._y-=movespeed;
}
else if(Key.isDown(Key.DOWN))
{
this._y+=movespeed;
}
if (Key.isDown(Key.LEFT))
{

this._x-=movespeed;
}
else if(Key.isDown(Key.RIGHT))
{
this._x+=movespeed;
}
if (Key.isDown(Key.CONTROL))
{ laserCounter++;
fireburst++;
if (fireburst < 5) {
_root.laser.duplicateMovieClip( "laser"+laserCounter, laserCounter );
_root["laser"+laserCounter]._visible=true;
}else if (fireburst >12){
fireburst = 1;

}

}



}


i had to add the variable fireburst because the laserCounter one was losing its visible state when the else condition kicked in.
The problem is that when the fireburst value reaches a number between 5 and 12 and the user lets go of the CTRL button then when they de-press the button it takes a moment to kick in again, i.e its not instantanious.
What I really need is a way of Flash recognising the up state of the CTRL button so that it can then reset the value of fireburst ( or laserCounter in the old script)
thx again
sharm

Ricod
05-03-2003, 02:38 PM
IF you have Flash MX, there's the listener method. I've never done anything with it yet, but from the helpfile it seems to do just what you want.

Method; registers an object to receive onKeyDown and onKeyUp notification. When a key is pressed or released, regardless of the input focus, all listening objects registered with addListener have either their onKeyDown method or onKeyUp method invoked. Multiple objects can listen for keyboard notifications. If the listener newListener is already registered, no change occurs.


myListener = new Object();
myListener.onKeyDown = function () {
trace ("You pressed a key.");
}
myListener.onKeyUp = function () {
trace ("You released a key.");
}
Key.addListener(myListener);

mrsharm
05-03-2003, 09:22 PM
hello again Ricod
I have tried desperately to understand how this listener thing works but have had no luck getting it going. Any ideas much appreciated.

All I need is a way of telling Flash that if the user unpresses the CONTROL key then reset the value of the variable fireburst. Its so frustrating!
but thx

Ricod
05-03-2003, 11:29 PM
Have you tried the following ? (remember, I never used those listeners either) : onClipEvent(enterFrame)
{
if (Key.isDown(Key.UP))
{
this._y-=movespeed;
}
else if(Key.isDown(Key.DOWN))
{
this._y+=movespeed;
}
if (Key.isDown(Key.LEFT))
{

this._x-=movespeed;
}
else if(Key.isDown(Key.RIGHT))
{
this._x+=movespeed;
}
if (Key.isDown(Key.CONTROL))
{ laserCounter++;
fireburst++;
if (fireburst < 5) {
_root.laser.duplicateMovieClip( "laser"+laserCounter, laserCounter );
_root["laser"+laserCounter]._visible=true;
}else if (fireburst >12){
fireburst = 1;
}
}
// the listener
Key.addListener(this);
this.onKeyUp = function() {
if (Key.CONTROL) {
laserCounter = 1;
fireburst=1;
}
}