• home
  • forum
  • my
  • kt
  • download
  • Calculate how much Ammo and Clips you have left from 1 Variable using Modulo

    Author: 2007-06-06 14:50:04 From:

    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.

    Code (actionscript)
    1. var ammo:Number = 160;
    2. var clipSize:Number = 16;
    3.  
    4. shoot.onRelease = function(){
    5.         ammo -= 1;
    6. }
    7.  
    8. onEnterFrame = function(){
    9.         display = "Ammo: "+ammo%clipSize+" Clips:"+Math.floor(ammo/clipSize);
    10. }

    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.

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      3D (20)
      Math Physics (14)
      3rd Party (5)
      Navigation (60)
      Actionscripting (26)
      Optimization (16)
      Animation (32)
      Projector (9)
      Audio (46)
      Special Effects (112)
      Backend (25)
      Text Effects (65)
      Drawing (18)
      Tips and Techniques (41)
      Dynamic Content (25)
      Tricks (6)
      Games (66)
      Utilities (19)
      Getting Started (71)
      Video (10)
      Interactivity (21)
      Web Design (22)

    New

    Hot