• Home
tudwaythecore.com
blog
  • Recent Posts

    • Detecting focus in flash player
    • BitmapData PerlinNoise
    • Creating bridges in APE part 2
    • Creating bridges in APE
    • SteerWheels 2
    • Creating a simple APE example
    • Design me some levels!
    • Finished Migrating
  • Recent Comments

    • random on Making a Shooting Game Part 2
    • SammyG on Making a Shooting Game Part 2
    • Jim on Making a Shooting Game Part 2
    • rraven on Making a Shooting Game Part 2
    • Adrian on Making a Shooting Game Part 2
  • Categories

    • APE
    • flash
    • game
    • platform
    • sound
    • tutorial
    • Uncategorized
  • Archives

    • May 2009
    • October 2008
    • September 2008
    • August 2008
    • May 2008
    • April 2008
    • March 2008
  •  

    August 2008
    M T W T F S S
    « May   Sep »
     123
    45678910
    11121314151617
    18192021222324
    25262728293031
  • games

    • Game Showcase
Aug 28

Making a Shooting Game Part 2

flash, tutorial Add comments

 

In the last tutorial I showed you how to make a turret fire a bullet that doesn’t move. This is little help when creating a turret game, but it leads us on to the next part.

If you think about what exactly we want the bullet to do it is quite simple. All we want it to do is move in a constant speed in the direction that the mouse is pointing. If you look back at the previous tutorial at the createBullet function you can see that the x coordinate is controlled by cos and the y by sin. 

createBullet(turret.x+(40*Math.cos((turret.rotation+90)*degreesToRadians)),turret.y+(40*Math.sin((turret.rotation+90)*degreesToRadians)));

We are going to use the same concepts when making the bullet move.

Can you remember how we used 40*Math.cos to make the bullet appear at the end of the turret, well we are going to increase the x coordinate of the bullet by that, whilst replacing the 40 with the speed of the bullet.

To do this we need to make a function that is triggered every frame that increases the bullets position when triggered. We are going to put this new code into the create bullet function.

var bulletGo = bulletHolder.addChild(bullet);

bulletGo.currentTurretRotation = turret.rotation+90;

bullet.addEventListener(Event.ENTER_FRAME, moveBullet);

function moveBullet(event:Event):void {

bulletGo.x+=Math.cos((bulletGo.currentTurretRotation)*degreesToRadians);

bulletGo.y+=Math.sin((bulletGo.currentTurretRotation)*degreesToRadians);

}

line 1: Creating a variable to handle adding the child of the bullet to the stage. This is to make the variables associated with it independent each time the variables are set. This stops the bullet from changing direction due to the turrets rotation.

line 2: Setting the current rotation variable to the turrets rotation.

line 3: Adding the Enter frame listener to move the bullet.

line 4: Setting up the moveBullet function that is triggered by the event listener in the line above.

line 5: Using the bullet child handler that i set up earlier to move the bullets x coordinate by Math.cos( the rotation of the turret, set by the currentRotation variable in line 2, in radians. As i said before i move the bullet though this handler so that each bullet moves independently of each other.

line 6: Just like above i move the bullets y coordinate by the same method, but use Math.sin.

Full code

PLAIN TEXT
Actionscript:
  1. stage.frameRate = 30;>
  2. var angle:Number = 0;
  3. var radiansToDegrees:Number = 180/Math.PI;
  4. var degreesToRadians:Number = Math.PI/180;
  5. var currentTurretRotation:Number;
  6. var bulletHolder:MovieClip = new MovieClip();
  7. addChild(bulletHolder);
  8. var turret:MovieClip = new MovieClip();
  9. turret.graphics.beginFill(0xFFFFFF);
  10. turret.graphics.lineStyle(2, 0xFFFFFF);
  11. turret.graphics.drawRect(-10, 0, 20, 40);
  12. turret.graphics.endFill();
  13. turret.graphics.beginFill(0xFFFFFF);
  14. turret.graphics.drawCircle(0,0,20);
  15. turret.graphics.endFill();
  16. turret.x = 275;
  17. turret.y = 200;
  18. addChild(turret);
  19. function createBullet(startx:Number, starty:Number) {
  20. var bullet:MovieClip = new MovieClip();
  21. bullet.graphics.lineStyle(2, 0xFFFFFF);
  22. bullet.graphics.drawCircle(0, 0, 5);
  23. bullet.x = startx;
  24. bullet.y = starty;
  25. var bulletGo = bulletHolder.addChild(bullet);
  26. bulletGo.currentTurretRotation = turret.rotation+90;
  27. bullet.addEventListener(Event.ENTER_FRAME, moveBullet);
  28. function moveBullet(event:Event):void {
  29. bulletGo.x+=Math.cos((bulletGo.currentTurretRotation)*degreesToRadians);
  30. bulletGo.y+=Math.sin((bulletGo.currentTurretRotation)*degreesToRadians);
  31. }
  32. }
  33. turret.addEventListener(Event.ENTER_FRAME, rotateTurret);
  34. function rotateTurret(event:Event):void {
  35. angle = Math.atan2(mouseY-turret.y, mouseX-turret.x);
  36. angle *= radiansToDegrees;
  37. turret.rotation = angle - 90;
  38. }
  39. stage.addEventListener(MouseEvent.CLICK, shoot);
  40. function shoot(event:Event) {
  41. createBullet(275+(40*Math.cos((turret.rotation+90)*degreesToRadians)),200+(40*Math.sin((turret.rotation+90)*degreesToRadians)));
  42. }

changes to previous code:

line 4: I am creating a variable called currentTurretRotation, which you know is where i hold the rotation of the turret when the mouse is clicked.

Example.

 

In the next tutorial i will show you how to delete the bullets when they go offstage, and maybe add some things to shoot.

12 Responses to “Making a Shooting Game Part 2”

  1. iSc00t Says:
    September 2nd, 2008 at 2:59 am

    Wonderful code. Been trying to figure this out for a few days now and was able to mod this to fit my needs perfectly. Thanks for sharing!

  2. tudway Says:
    September 2nd, 2008 at 6:51 pm

    Awesome, i’m glad i helped i will do a tutorial soon which shows you how to delete the bullets as this one doesn’t.

  3. fox Says:
    October 22nd, 2008 at 11:55 pm

    This has helped me so much, you have no idea. Is there a part 3? I am in desperate need of the next step and you are the only man who can help me… let me know!

  4. tudway Says:
    October 23rd, 2008 at 7:53 am

    I haven’t made a prat 3 but i will soon, what is it exactly that you want to be able to do?

  5. fox Says:
    October 24th, 2008 at 3:03 am

    I am making an asteroids-style shooter… with your help I’ve been able to make it this far but now i’ve run into a whole new problem: spawning enemies and hittesting them with the bullets :( . Whats worse is that I have to finish it this weekend… please let me know if you can help!! you are my hero.

  6. Tony Says:
    April 27th, 2010 at 1:13 pm

    Really nice tutorials you’ve got there mate. I would really like to talk about some stuff with you regarding this code.

    Please mail me your msn or something!

    Thanks!

  7. daush Says:
    June 29th, 2010 at 11:05 pm

    THANK YOU A LOT!!! i was hour trying to duplicate a movie clip with some code inside (AS2).. but this way to duplicate and move bullets help me a lot!!

    THANK YOU!

  8. Adrian Says:
    November 2nd, 2010 at 12:45 am

    You have helped me so much with this tutorial. Other tutorials are way too advanced for me and you make it look simple because you adhere to people with little programming experience. Thanks to you, I got my character shooting properly. With a couple of tweaks I figured out, I made the spped of the bullet go faster. Now, I just need to figure out how to erase the bullets when it’s off the screen. *Sigh*…video games are made in baby steps.

  9. rraven Says:
    February 6th, 2011 at 2:41 pm

    Thank you for this great and helpful tutorial! I’ll put this to use as soon as possible. I was looking for the right math functions to do this.

  10. Jim Says:
    May 27th, 2011 at 9:32 am

    Nice code man,

    Was wondering if you ever made that code to delete the bullets when they go off stage or if they hit any enemies?

  11. SammyG Says:
    May 27th, 2011 at 2:53 pm

    Can you please make tutorial 3.
    Remove bullets if it hits an enemy or goes of stage.
    Please!!

  12. random Says:
    August 6th, 2011 at 8:37 am

    Can you please tell me how to remove the bullet when it is outside the stage? I’m creating a game. I know the code to remove the bullet but when I test it, the output says the following:
    “ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller…etc”
    PLEASE HELP!!!
    Send the answer to my email if you please and thanks for the wonderful tutorials.

Leave a Reply

Powered by WordPress .::. Designed by SiteGround Web Hosting

cssandhtml