Calculating much Ammo is left on the Clip and how many Clips are left
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 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.















W00T FIRST COMMENT! I must say that it’s good tutorial!
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
ja, I might do a better one if I can be bothered.
good tutorial.
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.
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
??? it did not do the whole code
var ammo:Number = 160;
var clipSize:Number = 16;
shoot.onRelease = function(){
ammo -= 1;
if(_root.ammo
Ok under
ammo -= 1;
just add
if(_root.ammo
What the???????????
if(_root.ammo
sorry awesty for all the comments it just won’t let the code go in.
You cant put a less than sign. Only I can. < <
it would be best it you posted the code to stop it going into the negatives
@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
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)
http://www.awestyproductions.c.....-in-flash/
i want the images to rollover every 30 sec
anyone how i must do it?
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