Flash / Flex / ActionScript/Animation/Velocity — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 08:14, 26 мая 2010
Calculating position based on velocity
package {
import flash.display.*;
import flash.events.*;
import flash.utils.*;
public class Main extends Sprite {
private var distancePerSecond:int = 50; // Pixels to move per second
private var now:int; // The current time
private var then:int; // The last screen-update time
private var circle:Shape; // The object to animate
public function Main( ) {
circle = new Shape( );
circle.graphics.beginFill(0x0000FF, 1);
circle.graphics.lineStyle(1);
circle.graphics.drawEllipse(0, 0, 25, 25);
addChild(circle);
then = getTimer( );
now = then;
addEventListener(Event.ENTER_FRAME, enterFrameListener);
}
private function enterFrameListener (e:Event):void {
then = now;
now = getTimer( );
var elapsed:int = now - then;
var numSeconds:Number = elapsed / 1000;
var moveAmount:Number = distancePerSecond * numSeconds;
circle.x += moveAmount;
}
}
}
Change speed direction after hitting the boundary
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.ColorTransform;
import flash.geom.Rectangle;
public class Main extends Sprite {
public var speedX:int = 10;
public var speedY:int = -10;
public function Main() {
addEventListener(Event.ENTER_FRAME, onEnterFrame);
graphics.beginFill(0xFF, 1);
graphics.drawCircle(0, 0, 25);
graphics.endFill();
var colorTransform:ColorTransform = new ColorTransform();
colorTransform.color = Math.random()*0xFFFFFF;
transform.colorTransform = colorTransform;
}
private function onEnterFrame(event:Event):void {
x += speedX;
y += speedY;
var bounds:Rectangle = getBounds(parent);
if (bounds.left < 0 || bounds.right > stage.stageWidth) {
speedX *= -1;
}
if (bounds.top < 0 || bounds.bottom > stage.stageHeight) {
speedY *= -1;
}
}
}
}
Velocity Animation
package {
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite {
private var _sprite:Sprite = new Sprite( );
public function Main( ) {
_sprite.graphics.beginFill(0x0000ff, 100);
_sprite.graphics.drawCircle(0, 0, 25);
_sprite.graphics.endFill( );
_sprite.x = 50;
_sprite.y = 10;
addChild(_sprite);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
public function onEnterFrame(event:Event):void {
_sprite.x += 1;
_sprite.y += 2;
}
}
}