Home » Programming » ActionScript » 16 Formula Applied Mathematics in ActionScript 3.0

16 Formula Applied Mathematics in ActionScript 3.0

1. Distance between two points

 dx = x2 – x1;
 dy = y2 – y1;
 dist = Math.sqrt(dx*dx + dy*dy);

2. Formula inches

 sprite.x += (targetX – sprite.x) * easing;
//easing: ضریب اینچ
 sprite.y += (targetY – sprite.y) * easing;

3. Elastic formula

 vx += (targetX – sprite.x) * spring;
//spring: ضریب الاستیک
 vy += (targetY – sprite.y) * spring;
 sprite.x += (vx *= friction);
//friction: نیروی اصطکاک
 sprite.y += (vy *= friction);

Example with complete code:

addEventListener(Event.ENTER_FRAME , onEnterFrame , false , 0 , true);

var vx:Number=0;
var vy:Number=0;

var targetX=500;
var targetY=400;

var spring=5;
var friction=0.01;

function onEnterFrame(event:Event):void {

	vx += (targetX-ball.x) * spring;
	vy += (targetY-ball.y) * spring;

	vx *=friction;
	vy *=friction;

	ball.x +=vx;
	ball.y +=vy;
}

4.Flexible transformation formula


var dx:Number = sprite.x – fixedX;
 var dy:Number = sprite.y – fixedY;
 var angle:Number = Math.atan2(dy, dx);
 var targetX:Number = fixedX + Math.cos(angle) * springLength;
 var targetY:Number = fixedX + Math.sin(angle) * springLength;

5. Rotation with the mouse

 dx = mouseX – sprite.x;
 dy = mouseY – sprite.y;
 sprite.rotation = Math.atan2(dy, dx) * 180 / Math.PI;

Example with complete code:

addEventListener(Event.ENTER_FRAME , onEnterFrame , false , 0 , true);

var dx;
var dy;

function onEnterFrame(event:Event):void {
	dx= stage.mouseX - ball.x;
	dy= stage.mouseY - ball.y;
	ball.rotation = Math.atan2(dy,dx) * 180 / Math.PI;
}

6. Wave equation

 public function onEnterFrame(event:Event):void {
 ball.y=centerScale+Math.sin(angle)*range;
 angle+=speed;
 }

Example with complete code:

addEventListener(Event.ENTER_FRAME , onEnterFrame , false , 0 , true);

var angle =10;
var speed =11;
var centerScale=stage.stageHeight/2;
var range=5;

function onEnterFrame(event:Event):void {

	ball.y=centerScale+Math.sin(angle)*range;
	angle+=speed;

	ball.x += 2;
}

7. Formula heart beats

 public function onEnterFrame(event:Event):void {
 ball.scaleX=centerScale+Math.sin(angle)*range;
 ball.scaleY=centerScale+Math.sin(angle)*range;
 angle+=speed;
 }

Example with complete code:

addEventListener(Event.ENTER_FRAME , onEnterFrame , false , 0 , true);

var angle =1;
var speed =1;
var centerScale=2;
var range=1;

function onEnterFrame(event:Event):void {
	ball.scaleX=centerScale+Math.sin(angle)*range;
	ball.scaleY=centerScale+Math.sin(angle)*range;
	angle+=speed;
}

8. Circular rotation formula


public function onEnterFrame(event:Event):void {
 ball.x=centerX+Math.cos(angle)*radius;
 ball.y=centerY+Math.sin(angle)*radius;
 angle+=speed;
 }

9. Area of ​​circle

 public function getArea():Number
 {
 return Math.PI * Math.pow((width / 2), 2);
 }

10.Circumference ratio (environment)

 public function getCircumference():Number
 {
 return Math.PI * width;
 }

11. Elliptical rotation formula

 public function onEnterFrame(event:Event):void {
 ball.x=centerX+Math.cos(angle)*radiusX;
 ball.y=centerY+Math.sin(angle)*radiusY;
 angle+=speed;
 }

Example with complete code:

addEventListener(Event.ENTER_FRAME , onEnterFrame , false , 0 , true);

var angle =2;
var speed =1;
var centerX=stage.stageWidth/2;
var centerY=stage.stageHeight/2;
var radiusX=150;
var radiusY=50;

function onEnterFrame(event:Event):void {
	ball.x=centerX+Math.cos(angle)*radiusX;
	ball.y=centerY+Math.sin(angle)*radiusY;
	angle+=speed;
}

12. Color operation

 var t:uint=0×77ff8877
 var s:uint=0xff000000
 var h:uint=t&s
 var m:uint=h>>>24
 trace(m);

13.Convert hexadecimal  to decimal


var hexValue:uint = 0xFFFFFF;

trace(hexValue); // 16777215

14.Converted to decimal  to hexadecimal

 decimalValue.toString(16);

15. Get color

 red = color24 >> 16;
 green = color24 >> 8 & 0xFF;
 blue = color24 & 0xFF;

 alpha = color32 >> 24 & 0xFF;;
 red = color32 >> 16 & 0xFF;
 green = color32 >> 8 & 0xFF;
 blue = color232 & 0xFF;

Example with complete code:

var color24 = 0xff0000;
trace("color24:",color24)
//color24:16711680

var red24 = color24 >> 16;
var green24 = color24 >> 8 & 0xFF;
var blue24 = color24 & 0xFF;

trace(" red:"+red24 , " green:"+green24 , " blue:"+blue24)
//red:255 | green:0 | blue:0

var color32 = 0xff00ff99;
trace("color32: ",color32)
//color32:4278255513

var alpha32 = color32 >> 24 & 0xFF;

var red32 = color32 >> 16 & 0xFF;
var green32 = color32 >> 8 & 0xFF;
var blue32 = color32 & 0xFF;

trace("red:"+red32 , "green:"+green32 , "blue:"+blue32, "alpha:"+alpha32);
//red:0 | green:255 | blue:153 | alpha:255

16. Calculate bit color

 color24 = red << 16 | green << 8 | blue;

Print Friendly
Tags :
Previous post link
Next post link

About Behrouz Pooladrag

Born: May 27, 1988 Occupation: Adobe Flash, programming and web design, graphic design, multimedia, web-based software

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Scroll To Top