Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

Introduction to ActionScript 2.0/Math Class

From Wikibooks, open books for an open world
<Introduction to ActionScript 2.0
Introduction to ActionScript 2.0
SoundsMath ClassOther Classes

Key concepts:



Maths are very important in Flash applications. Let's learn how to use mathematical functions in ActionScript!

Handling numbers

[edit |edit source]

There are several functions of the Math class that can manipulate numbers for us:

  • Math.abs(Number): Finds the absolute value of a number
  • Math.ceil(Number): Rounds a number up to the nearest integer
  • Math.floor(Number): Rounds a number down to the nearest integer
  • Math.round(Number): Rounds a number off to the nearest integer
  • Math.max(Number 1,Number 2): Returns the larger of two numbers
  • Math.min(Number 1,Number 2): Returns the smaller of two numbers
  • Math.random(): Returns a random number smaller than 1 and larger than or equal to 0

Let's look at a quick example:

CodeResult
varmyNumber:Number=-12.3;trace(Math.abs(myNumber));trace(Math.ceil(myNumber));trace(Math.floor(myNumber));trace(Math.round(myNumber));trace(Math.max(Math.ceil(myNumber),Math.floor(myNumber)));trace(Math.min(Math.ceil(myNumber),Math.floor(myNumber)));trace(Math.floor(Math.random()*12)+1);

12.3
-12
-13
-12
-12
-13
7

Note the technique used in the last line to produce random numbers. Math.random() is multiplied by 12, rounded down (giving a random integer from 0 to 11) and then one is added (giving a random integer from 1 to 12).

Exponents, roots and logarithms

[edit |edit source]

Trigonometry and pi

[edit |edit source]
Retrieved from "https://en.wikibooks.org/w/index.php?title=Introduction_to_ActionScript_2.0/Math_Class&oldid=3677920"
Category:

[8]ページ先頭

©2009-2025 Movatter.jp