Best viewed in Firefox

Awesty Productions

Calculating much Ammo is left on the Clip and how many Clips are left

May 6th, 2007 by awesty

In this tutorial you will learn how to calculate how much ammo on your clip you have left and how many clips you have left. Alot of the time when you would do this you would have a separate variable for the clips and the ammo. But for this tutorial all we need is one for the ammo. I am using flash CS3 but Flash 8 and Flash MX 2004 will work as well. If you are using Flash CS3 make sure you select an AS 2.0 document.

Click here to see an example of what we will be making.

Make a dynamic text box and a button on the stage. Give the button an instance name of ’shoot’ and the dynamic text box a variable name of ‘display’. The text box will display the ammo and clips and the button will subtract the ammo.

Select one of the frame and open the actions panel. Enter this code.

var ammo:Number = 160;
var clipSize:Number = 16;

shoot.onRelease = function(){
    ammo -= 1;
}

onEnterFrame = function(){
    display = "Ammo: "+ammo%clipSize+" Clips:"+Math.floor(ammo/clipSize);
}

That is a very short code. I am working on a game right now that involves shooting and I wish had of done something similar to this.

The first two lines are declaring the variables. The variable ‘ammo’ is contains how much ammo (bullets) we have left. The variable clipSize is how big each clip is. You can change this to be whatever you want.

The next three lines are setting an onRelease function for the button. So when the button is released the variable ammo decreases by one. ammo -= 1 is the same as ammo = ammo - 1.

onEnterFrame = function(){} means every frame flash will run the code between the curly braces. The next line is what makes the text box ‘display’ display the ammo and the clip. If we had put display = “Hello”, then the text box would read hello. If we had of put display = “Hello “+name, and the variable name is equal to ‘Peter’ the text box would read Hello Peter. So ours is going to read Ammo: [someNumber] Clips: [someOtherNumber].

To calculate how much ammo is left on the clip I am using the modulo (%) operator. What is does is divides the two numbers and returns the remainder. So 5%2 would equal one and 15%6 would equal 3. Therefore flash will give us how much ammo is left on the clip of we divide the amount of ammo by the clip size. To calculate how many clips we have left we are dividing the ammo by the clip size and then rounding it down with the Math.floor() function. So if the answer was 34.9352 flash would return 34.

Now if you test your movie you should see something like THIS.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • del.icio.us
  • digg
  • Furl
  • MyShare
  • NewsVine
  • Netscape
  • Reddit
  • Simpy
  • Slashdot
  • StumbleUpon
  • Technorati
  • YahooMyWeb

RSS feed | Trackback URI

18 Comments »

Comment by Eibwen Subscribed to comments via email
2007-05-07 03:58:50

W00T FIRST COMMENT! I must say that it’s good tutorial!

 
Comment by austin Subscribed to comments via email
2007-05-09 11:28:14

this might be useful. i am getting into trigometry so making a shooting system with this would be great…even though the tutorial for a shooting system here sucks. lawlz

Comment by awesty
2007-05-13 17:21:38

ja, I might do a better one if I can be bothered.

 
 
Comment by Kurt Subscribed to comments via email
2007-05-12 22:11:33

good tutorial.

 
Comment by Kurt Subscribed to comments via email
2007-05-12 22:28:14

one problem you can shoot forever and that means it goes to -’s i will try to fix it my self though for me cause I’m good with this stuff.

Comment by Kurt Subscribed to comments via email
2007-05-12 22:34:11

This is the code for it not to be in the -’s

var ammo:Number = 160;
var clipSize:Number = 16;
shoot.onRelease = function(){
ammo -= 1;
if(_root.ammo

Comment by Kurt Subscribed to comments via email
2007-05-12 22:36:02

??? it did not do the whole code

var ammo:Number = 160;
var clipSize:Number = 16;
shoot.onRelease = function(){
ammo -= 1;
if(_root.ammo

Comment by Kurt Subscribed to comments via email
2007-05-12 22:36:57

Ok under

ammo -= 1;

just add

if(_root.ammo

Comment by Kurt Subscribed to comments via email
2007-05-12 22:37:40

What the???????????

(Comments wont nest below this level)
Comment by Kurt Subscribed to comments via email
2007-05-12 22:39:11

if(_root.ammo

 
Comment by Kurt Subscribed to comments via email
2007-05-12 22:39:55

sorry awesty for all the comments it just won’t let the code go in.

 
Comment by awesty
2007-05-13 17:23:26

You cant put a less than sign. Only I can. < <

 
Comment by JOeY Subscribed to comments via email
2007-09-18 08:22:57

it would be best it you posted the code to stop it going into the negatives :)

 
 
 
 
 
 
Comment by Grifo
2007-05-13 07:46:41

@Kurt Oh my friend you cannot use the “less than” symbol because of hacking protection. Whenever you need to use it, just use LESSTHAN . People here are used to it and they will understand you.

Grifo

 
Comment by randall Subscribed to comments via email
2007-05-13 14:39:50

How do you make a button that will brong you to any frame or scene you want it to? (im making a gallery of all my movies so I want to make a frame with a bunch of buttons leading to each clip)

 
 
Comment by elianita Subscribed to comments via email
2007-07-12 17:47:43

i want the images to rollover every 30 sec
anyone how i must do it?

 
Comment by Jamie Subscribed to comments via email
2008-03-29 08:36:11

hey, good tutorial, simple efficient and WORKS! i like it, but it would be great if *scrlls up, then back down* kurt (or awesty) could give us the code to keep it positive :)
thanks,
Jamie

 
Name (required)
E-mail (required - never shown publicly)
URI
Subscribe to comments via email
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> in your comment.