1 / 14

Math Class

Math Class. Objectives of this topic:. You can …. Use the Math Class in Flash CS3. Use mathematical operators. Generate random numbers and round the numbers. Overview. Introduction to Math Class Basic Math Operators Generating Random Numbers Rounding Numbers. Introduction to Math Class.

apu
Download Presentation

Math Class

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Math Class

  2. Objectives of this topic: You can … Use the Math Class in Flash CS3 Use mathematical operators Generate random numbersand round the numbers

  3. Overview • Introduction to Math Class • Basic Math Operators • Generating Random Numbers • Rounding Numbers

  4. Introduction to Math Class • Math Class is a built-in AS class • It simplifies a lot of task by shortening and simplifying the code • It contains: • methods representing common mathematical functions (log, max/min and round) • Properties representing mathematical constants (pi – 3.141…)

  5. Example: var myInstance:MyClass = new myClass(); • This code creates a variable named myInstance that is equal to a new instance of the class myClass Math.random(); • This code creates a new instance of a random number

  6. Basic Math Operators • Basic math operations simply can be used in AS. • Math operations are not complicated to code • The operations symbol include: • + for addition • - for subtraction • * for multiplication • / for division

  7. Flash follows the standard order of operations for arithmetic operation • There are four rules to the order of operations: • Solve the operations inside parentheses; () • Solve the exponents; 22 • Perform the multiplication and division operations, working from the left to the right side of the equation • Perform any addition and subtraction, also from left to right

  8. Generating Random Numbers • Use a method of the Math class to generate a random number; called random() Example: trace(Math.random()); • run the Math class’s with randon() function

  9. Random number can contain, theoretically, an infinite number of decimal places, but they will never reach 1.0 • Random numbers are great for generating random patterns or effect Example: giving a direction and speed to individual snowflakes in a snowstorm.

  10. To generate a whole random number, we can simply multiply the random method by 10 Example trace(Math.random() * 10); • Generate a random numbers between 0 and 10 (but not including 10)

  11. Rounding Numbers • Rounding technique uses to narrow the result of number generated Example: trace(Math.round(Math.random()*6)); • Math.round() uses the “nearest” rounding technique for rounding numbers up and down (decimals from .0 to .4 are rounded down; decimals from .5 to .9 are rounded up)

  12. Decimals are rounded to the nearest whole number, either: • Round down – math.floor() • .0 to .9 are round down • Round up – math.ceil() • .0 to .9 are round up Flash: Math

  13. Example of Math class implementation: random_mc.addEventListener(MouseEvent.CLICK, onClick); function onClick(event:MouseEvent):void { dice1_mc.gotoAndStop(Math.ceil(Math.random()*6)); } • Rolling the dice (Rounding, RoundingTwo)

  14. Example of Games • MemoryGame • Source: • MemoryGame • Card • Memory

More Related