A Flash Developer Resource Site

Results 1 to 15 of 15

Thread: Shoot in direction

  1. #1
    Junior Member
    Join Date
    Mar 2006
    Posts
    6

    Shoot in direction

    Hello,

    I'm trying to make this little flashgame.
    You can take a look at it here: http://cmdstud.khlim.be/~nkolb/etc/game.swf

    The basic idea is to shoot a bullet to the bomb in the top of the screen. When the bullet hits the bomb it falls down on top of the 'alien'.

    I'm stuck at the following point, I have no idea on how to direct the bullet so it shoots in the same direction as the canon.

    With the up/down key you can control the canon, with the spacebar you shoot a bullet.

    The .fla file can be found here: http://cmdstud.khlim.be/~nkolb/etc/game.fla
    I'm using Flash 8.

    If you can give me some instructions on how to do this, thanks. This will be a great learning curve!

  2. #2
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,876
    Cant open your file as using flash mx 2004...heres the general idea ...basically first what you need to know is the rotation value of the cannon

    e.g

    var angle:Number = cannon_mc._rotation; //this gives answer in degrees
    var radians:Number = Math.PI/180 * angle //this is the angle in radians

    //now lets place the bullet at the base of the cannon
    bullet_mc._x = cannon_mc._x;
    bullet_mc._y = cannon_mc._y;

    //now we need to define a speed that the bullet will travel at
    var bulletSpeed:Number = 50;

    //now we need to fire the bullet in the right direction lets define the x and y velocity components

    var vx:Number = bulletSpeed*Math.cos(radians);
    var vy:Number = bulletSpeed*Math.sin(radians);

    //now the rest is simple just put the stuff in an onEnterFrame or setInterval //timer so that the bullet actually moves so a fireBullet function may look something like this(after putting most of the above code in it)

    Code:
    function fireBullet():Void{
    	var angle:Number = cannon_mc._rotation;
    	var radians:Number  = Math.PI/180 * angle;
    	var vx:Number = bulletSpeed*Math.cos(radians);
             var vy:Number = bulletSpeed*Math.sin(radians);
    	
    	_root.onEnterFrame = function(){
    		bullet_mc._x +=vx;
    		bullet_mc._y +=vy;
    		
    		}
    	}
    ive knocked up a very quick fla for you to show you this in action ( although i must say as its so rushed, dont think of it as the best technique etc, as i havent even deleted the onEnterFrame function etc..but it should put you in the right direction, no pun intended lol) Note : Space bar to fire, top/bottom cursor keys to rotate the cannon..at present it will only fire once..
    Attached Files Attached Files
    Last edited by silentweed; 03-06-2006 at 08:23 PM.
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

  3. #3
    Junior Member
    Join Date
    Mar 2006
    Posts
    6
    Thanks Silentweed, I resaved the file to be mx2004 compatible.

    I'll analyse your code, thanks very much in advance.

  4. #4
    Junior Member
    Join Date
    Mar 2006
    Posts
    6
    Allright, I seem to understand ever line, but I'm stuck at the refiring option.
    I continue on your code.

    I need to duplicate the bullet_mc in order to re-use this mc I need to insert this line somewhere, I guess?

    Code:
    _root.bullet_mc.duplicateMovieClip("bullet_mc");
    Actually, the more I think about it, the more I think I need to find another solution then just duplicating it.

    Thanks for the clarification and cheers

  5. #5
    Junior Member
    Join Date
    Mar 2006
    Posts
    6
    Someone else maybe? Thanks.

  6. #6
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,876
    here mate .. this should do the trick ... heres the modified code and the fla if you dont understand anything give me a shout.

    Code:
    var vr:Number = 5;
    var vBullet:Number = 50;
    cannon_mc._rotation = -45;
    var bulletArray:Array = new Array();
    var bulletNumber:Number = 0;
    
    
    
    _root.onEnterFrame = function():Void{	
    	if(Key.isDown(Key.UP)){
    		
    		cannon_mc._rotation -= vr;
    	}
    	
    	if(Key.isDown(Key.DOWN)){
    		
    		cannon_mc._rotation += vr;
    	}
    }
    
    var keyListener:Object = new Object();
    Key.addListener(keyListener);
    
    keyListener.onKeyDown = function():Void{
    	
    	if(Key.getCode() == 32){
    		
    		fireBullet();
    	}
    }
    
    function fireBullet():Void{
    	var bullet_mc:MovieClip = _root.attachMovie("bullet_mc", "bullet"+bulletNumber, _root.getNextHighestDepth());
    	var angle:Number = cannon_mc._rotation;
    	var radians:Number  = Math.PI/180 * angle;
    	
    	bullet_mc.vx = vBullet*Math.cos(radians);
    	bullet_mc.vy = vBullet*Math.sin(radians);
    	bulletArray.push(bullet_mc);
    	
    	bulletNumber++;
            bullet_mc._x = cannon_mc._x;
            bullet_mc._y = cannon_mc._y;
    }
    
    var nMover:Number = setInterval(mover, 1000/24);
    
    function mover():Void{
    		for(var i:Number=0; i<bulletArray.length; i++){
    		_root["bullet"+i]._x +=_root["bullet"+i].vx;
    		_root["bullet"+i]._y +=_root["bullet"+i].vy;
    		}
    }
    P.s you may want to make it more efficient for example by deleting movieclips when they leave the stage etc...
    Attached Files Attached Files
    Last edited by silentweed; 03-07-2006 at 06:43 PM.
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

  7. #7
    Junior Member
    Join Date
    Mar 2006
    Posts
    6
    Wauw, thanks.

    What exactly does this do:

    Code:
    var nMover:Number = setInterval(mover, 1000/24);
    
    function mover():Void{
    		for(var i:Number=0; i<bulletArray.length; i++){
    		_root["bullet"+i]._x +=_root["bullet"+i].vx;
    		_root["bullet"+i]._y +=_root["bullet"+i].vy;
    		}
    }

  8. #8
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,876
    Basically this is a timer ( which can be used instead of onEnterFrame ) I prefer using timers because their timing is not timed to the frame rate.

    Every 1000/24 milliseconds i.e every 1/24 sec the timer runs the function mover() which then runs the For Loop. What this loop does is control the movement of each of these bullets. It loops through the bullet array and moves each bullet to a new position according to what its vx and vy velocity components are

    Hope that makes sense
    Last edited by silentweed; 03-07-2006 at 08:17 PM.
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

  9. #9
    Qwaizang:syntax_entity_ Q__Hybrid's Avatar
    Join Date
    Aug 2005
    Posts
    270
    James,

    The variable "nMover" is an interval ID. Flash stores all intervals created using the setInterval() global function in an array. This array can only be accessed through the setInterval() and clearInterval() global functions. Think of setInterval() as an equivelant of the Array.push() method -- it inserts a new interval at the end of the interval array. Think of clearInterval() as an equivelant of the Array.pop() method -- it removes the last interval from the interval array.
    Interval IDs store the array index of their respective intervals (all indexes are positive integers greater than zero) rather than the object reference, such that supplying the interval ID to the clearInterval() global method effectively removes the interval that corresponds with that interval ID.

    Intervals are used to invoke functions at a rate that is seperate from the framerate specified in the document settings and is not reliant upon other forms of loops, such as "for," "for ... in," or "while."

    The function "mover" is being invoked at a rate of 24 frames per second, because one second contains 1,000 milliseconds and dividing this by 24 returns approximately 42 milliseconds, or 24 fps.

    Ideally, setInterval() would be passed its own interval ID so that the function being invoked could cancel the interval calling it. As in:
    Code:
    var nMover:Number = setInterval(mover, (1000/24), nMover);
    
    function mover(stopCallingMe:Number):Void {
      // pesudo-code:
      movement code goes here...
    
      if((bullet has hit enemy) || (bullet can no longer be seen)) {
        remove the bullet from the array
      }
    
      if(there are no more bullets) {
        clearInterval(stopCallingMe);
      }
    };
    Hope that makes more sense,
    +Q__
    Qwai•zang \kwî-'zan\ n [origin unknown] 1 : abstract designer, esp. of complex real-time experiments, c. 21st century

  10. #10
    Junior Member
    Join Date
    Mar 2006
    Posts
    6
    Thanks to both, has been a great help.

  11. #11
    Flash Game Maker Mr_Welfare's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    155
    Whenever I try to use this script in a version of Flash other than Flash 8, I get the following errors.

    Code:
    **Error** Scene=Scene 1, layer=a, frame=5:Line 12: '{' expected
         _root.onEnterFrame = function():Void{	
    
    **Error** Scene=Scene 1, layer=a, frame=5:Line 22: Unexpected '}' encountered
         }
    
    Total ActionScript Errors: 2 	 Reported Errors: 2
    In my "Publish Settings" I am running Flash Player 6 as that is what my original movie was made in. Can anyone help me convert this script into a format that will be accepted?

    Thanks,

    Mr_Welfare

  12. #12
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,876
    In publish settings use AS2 and flash player 7 or 8 ..if u wanna use 6 then take out all the strict data typing
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

  13. #13
    Flash Game Maker Mr_Welfare's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    155
    Ok that now works. Now I am running into a problem where if I try to make the bullet_mc's _x and _y = a symbol that is inside of a symbol, the bullet just shoots across the top of the screen from the left corner to the right. Here is the script that I am using.

    Code:
    var vr:Number = 5;
    var vBullet:Number = 50;
    cannon_mc._rotation = -45;
    var bulletArray:Array = new Array();
    var bulletNumber:Number = 0;
    
    var keyListener:Object = new Object();
    Key.addListener(keyListener);
    
    keyListener.onKeyDown = function():Void{
    	
    	if(Key.getCode() == 32){
    		
    		fireBullet();
    	}
    }
    
    function fireBullet():Void{
    	var bullet_mc:MovieClip = _root.attachMovie("bullet_mc", "bullet"+bulletNumber, _root.getNextHighestDepth());
    	var angle:Number = _root.character.shootable._rotation;
    	var radians:Number  = Math.PI/180 * angle;
    	
    	bullet_mc.vx = vBullet*Math.cos(radians);
    	bullet_mc.vy = vBullet*Math.sin(radians);
    	bulletArray.push(bullet_mc);
    	
    	bulletNumber++;
        bullet_mc._x = _root.character.shootable._x;
        bullet_mc._y = _root.character.shootable._y;
    }
    
    var nMover:Number = setInterval(mover, 1000/24);
    
    function mover():Void{
    		for(var i:Number=0; i<bulletArray.length; i++){
    		_root["bullet"+i]._x +=_root["bullet"+i].vx;
    		_root["bullet"+i]._y +=_root["bullet"+i].vy;
    		}
    }
    EDIT: Also, how could I make the bullet fire when a button is pressed? I tried copying the firebullet(); function into the on (release) statement, but nothing happened.

    Sorry for the questions,

    Mr_Welfare
    Last edited by Mr_Welfare; 07-21-2006 at 11:47 AM.

  14. #14
    Flash Game Maker Mr_Welfare's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    155
    Does anyone else know? I've tried messing around with the script more today, but I'm still not getting any results.

    Mr_Welfare

  15. #15
    Flash Game Maker Mr_Welfare's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    155
    One last stab at it... anyone?

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