Back to "flash Basic Theory Class-Catalog"
Linear vertical Motion
In the wave1.as file, a linear vertical motion is added, just to add some inspiration to our animation. The following is the code for this file:
package {
import flash.display.Sprite;
import flash.events.Event;
public class Wave1 extends Sprite {
private var ball:Ball;
private var angle:Number = 0;
private var centerY:Number = 200;
private var range:Number = 50;
private var xspeed:Number = 1;
private var yspeed:Number = .05;
public function Wave1() {
init();
}
private function init():void {
ball = new Ball();
addChild(ball);
ball.x = 0;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
public function onEnterFrame(event:Event):void {
ball.x += xspeed;
ball.y = centerY + Math.sin(angle) * range;
angle += yspeed;
}
}
}
Heart Movement
Use sine as a tool, not just for controlling physical locations. In the pulse.as file, a value is used to influence the scaling of the ball to achieve a heartbeat effect, the code is as follows:
package {
import flash.display.Sprite;
import flash.events.Event;
public class Pulse extends Sprite {
private var ball:Ball;
private var angle:Number = 0;
private var centerScale:Number = 1;
private var range:Number = .5;
private var speed:Number = .1;
public function Pulse() {
init();
}
private function init():void {
ball = new Ball();
addChild(ball);
ball.x = stage.stageWidth / 2;
ball.y = stage.stageHeight / 2;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
public function onEnterFrame(event:Event):void {
ball.scaleX = ball.scaleY = centerScale +Math.sin(angle) * range;
angle += speed;
}
}
}
The principle is the same, Centerscale represents the scaling ratio of 100%, range represents range, and speed represents speed. Not only that, sine wave is also applied in attributes such as alpha,rotation.