A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: bullet rate[simple]

  1. #1
    Flash Incompetent ChaseNYC's Avatar
    Join Date
    Jun 2002
    Location
    new york city
    Posts
    693

    [RESOLVED] bullet rate

    well, I presume this is a simple question, I have scanned the forums a bit and cannot seem to come up with any conclusions to my problem. I am making space invaders style game and I am using one of the tutorials from flashkit which uses the following code for shooting:

    Code:
    onClipEvent(load){
     _root.laser._visible=false;
     laserCounter=1;
    }
    
    if (Key.isDown(Key.SPACE)) {
    		
    		laserCounter++;
    		_root.laser.duplicateMovieClip( "laser"+laserCounter, laserCounter );
    		_root["laser"+laserCounter]._visible=true;
    		trace(laserCounter);
    	} 
    
    //this is in the bullet/laser action
    
    onClipEvent (load) {
    
      laserMoveSpeed=20;
      this._y=_root.spaceship._y - ((_root.spaceship._height / 2) + (this._height)); 
      this._x=_root.spaceship._x;
    
    }
    
    onClipEvent (enterFrame) {
    
      this._y-=laserMoveSpeed;
      if (this._y>600){
        this.removeMovieClip();
      }
    
    }
    now, the code works fine for what it is supposed to do; however, my problem lies in the fact that I do not want it to be the case that if the user holds down the spacebar it will shoot the bullet at the maximum speed, I would like to figure out some way that(these times are not exact but just examples) if the spacebar is held down it will shoot a bullet every 2 seconds; and if the user taps the spacebar they can shoot up to one bullet every second. Hopefully this makes sense, I apologize greatly if this question is explained somewhere else but I did look around and could not find any examples.

    Thanks in advance,

    ChaseNYC
    Last edited by ChaseNYC; 06-26-2006 at 08:03 AM.
    mmm signature

  2. #2
    crossconscious
    Join Date
    Sep 2005
    Location
    Belgium
    Posts
    1,188
    Just use a variable, each time you shoot you set it to 40 (or more or less). Each frame, if it's higher than 0 and fire is pressed, extract 1 from it. If fire is not pressed, and it's higher than 20 (or whatever), set it to 20 to allow more rapid firing when tapping. Only allow users to shoot if it's equal to 0. Something like that should work.

  3. #3
    Flash Incompetent ChaseNYC's Avatar
    Join Date
    Jun 2002
    Location
    new york city
    Posts
    693
    Quote Originally Posted by Fall_X
    Just use a variable, each time you shoot you set it to 40 (or more or less). Each frame, if it's higher than 0 and fire is pressed, extract 1 from it. If fire is not pressed, and it's higher than 20 (or whatever), set it to 20 to allow more rapid firing when tapping. Only allow users to shoot if it's equal to 0. Something like that should work.
    i'm a little confused about your suggestion I kinda have an idea so that there would be a variable... bare with me this is not actual AS but rather just in my own programming language code
    Code:
    readytoFire = 20
    currentFire = 0
      if key.space.keydown = true and currentFire >= readytoFire then
         call firebullet
      end if
    
      if key.space.keydown and currentFire < readytoFire then
         currentFire = currentFire + 1
      End if
    
      if key.space.keyup and currentFire < readytoFire then
         currentFire = currentFire + 2
      End if
    this way you get to fire quicker is you aren't holding down the spacebar... however i don't understand how this would work seeing as the whole game is on one frame... im not sure what would call the if statements and what wouldnt make it add to 20 instantly because it just keeps running... it's really hard to explain what i'm confused about but exactly what is calling these if statements and why wouldnt it instantly call these if functions over and over and get to 20 instantly? I guess the main point is that as of now the whole game is in one frame therefore adding to the counter each frame wouldnt work...
    mmm signature

  4. #4
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    See if this helps.

    In load event set the time variable:

    Code:
    onClipEvent (load) {
    	_root.readytoFire = getTimer();
    	_root.laser._visible = false;
    	laserCounter = 1;
    }
    Change the code checking for SPACE key:

    Code:
    if (Key.isDown(Key.SPACE)) {
    	//get current time in ms
    	var thisTime = getTimer();
    	//check if enough time has passed from last shot
    	if (thisTime<_root.readytoFire) {
    		//shoot after 2 seconds
    		_root.readytoFire = thisTime+2000;
    		//save the last time laser was shot
    		_root.lastTime = thisTime;
    		//creating laser
    		laserCounter++;
    		_root.laser.duplicateMovieClip("laser"+laserCounter, laserCounter);
    		_root["laser"+laserCounter]._visible = true;
    		trace(laserCounter);
    	}
    } else {
    	//space key is not down
    	//reduce time until next shot if it was set from key down event
    	if (_root.readytoFire == _root.lastTime+2000) {
    		//shoot after 1 second from last shot
    		_root.readytoFire = _root.lastTime+1000;
    	}
    }

  5. #5
    Flash Incompetent ChaseNYC's Avatar
    Join Date
    Jun 2002
    Location
    new york city
    Posts
    693
    got really excited... but it doesnt work... i havent processed the code in terms of understanding all that is going on yet so i'm not sure what to trace however hitting spacebar does not lead to any bullets/laser coming out...

    edit: so i've added two traces in part of the code:
    Code:
    var thisTime = getTimer();
    	//check if enough time has passed from last shot
    	trace (thisTime + " : " + _root.readytoFire);
    	if (thisTime<_root.readytoFire) {
    		trace("ready!");
    		//shoot after 2 seconds
    		_root.readytoFire = thisTime+200;
    so it returns that thisTime is continually growing and _root.readytoFire stays at a consistant 25 however; the second trace never comes up making me believe something is wrong with that if statement because... err just needs to be > instead of <... time to play around and see if i can get it to work to my liking... thank you oh tonypa guru!
    Last edited by ChaseNYC; 06-26-2006 at 07:58 AM.
    mmm signature

  6. #6
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Uh, sorry, use > of course.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center