Math(s)
I know this is a scary word for some, but it is not all that scary. Math is a built in function that acts like a calculator.
Math.sqrt()
//this takes the square root of the number in the parenthesis
Math.random()*#
//Finds random number between zero and the number after *
Math.round()[]
//Rounds the number in the parenthesis
Math.ceil()[]
//Rounds the number in the parenthesis to higher integer
Math.floor()[]
//Rounds the number in the parenthesis to lower integer
Lets look at how ‘Math.random()*’ works:
Example1
trace(Math.random()*6);
//5.91174
//1.913511
//5.605396
//1.071749
//5.145421
//Simply picks a random number between 0 and 6
Example2
trace (Math.sqrt(4));
//2
More Math
Math.abs() an absolute value.
Math.acos() an arc cosine.
Math.asin() an arc sine.
Math.atan() an arc tangent.
Math.atan2() an angle from the x axis to the point. Math.ceil() a number up to the nearest integer. Math.cos() a cosine.
Math.exp() an exponential value.
Math.floor() a number down to the nearest integer. Math.log() a natural logarithm.
Math.max() the larger of the two integers. Math.min() the smaller of the two integers.
Math.pow() to the power of the . Math.random() a pseudo-random number between 0.0 and 1.0.
Math.round() to the nearest integer.
Math.sin() a sine.
Math.sqrt() a square root.
Math.tan() a tangent.
I made this next example from my new knowledge of ActionScript:
EXAMPLE 1
var arrow=element ("arr");
arrow.x=0
arrow.y=0
arrow.rotation=45
function rotateArrow(evt:Event):void
{
arrow.rotation=Math.atan2(mouseY,mouseX)*//number_varies
}
stage.addEventListener(Event.ENTER_FRAME,rotateArrow)
trace(Math.atan2(mouseY,mouseX));
//this script makes ‘arrow’ point to wherever the mouse is.
I used this small little script to make an I-pod dial, the one that goes through the menu when you move your finger around it, except it follows the mouse movement. I don’t like long complicated code, especially when the same effect can be achieved with short simple code. You just have to have the idea set.