FLex中AS实现地球轨迹运动

效果如图所示:

<!--StartFragment -->
该运用中你可以使用键盘方向键来改变球的运动,用副键盘+ -键改变球大小!
 此例用到两个AS文件一个是Ball类,另一个是Roation类。
Ball.as
package AS3_Ball
{
import flash.display.Sprite;

public class Ball extends Sprite
{
public var radius:Number;
public var color:uint;
public var vx:Number = 0;
public var vy:Number = 0;
public var drag:Boolean = false;

public function Ball(radius:Number=40,color:uint=0xff0000)
{
this.radius = radius;
this.color = color;
init();
}
private function init():void
{
graphics.beginFill(color);
graphics.drawCircle(0,0,radius);
graphics.endFill();
}
}
}
 
Roation.as:
写道
package AS3_Ball
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;

[SWF(backgroundColor="0x000000",width="700",height="300",frameRate="30")]
public class Roation extends Sprite
{
private var flag:Boolean = true;
private var _ball:Ball; //小球
private var _angerX:Number = 0; //x角度
private var _angerY:Number = 0; //y角度
private var _centerX:Number = 0; //X坐标
private var _centerY:Number = 0; //Y坐标
private var _rangeX:Number = 0; //X半径长度
private var _rangeY:Number = 0; //Y半径长度
private var _xspeed:Number = .05; //X轴速度
private var _yspeed:Number = .05; //Y轴速度
private var _xpos:Number; //lineTo X轴
private var _ypos:Number; //lineTo y轴
private var _pen:Number = .01; //轨迹大小
private var _speed:Number = .8 //方向键值增减
private var _nums:Number = 0; //统计次数

public function Roation()
{
init();
}
private function init():void
{
for(var i:uint=0;i<500;i++)
{
var balls:Ball = new Ball(Math.random()*1,0xFFFFFF);
balls.x = Math.random()*stage.stageWidth;
balls.y = Math.random()*stage.stageHeight;
addChild(balls);
}
_ball = new Ball(5,0x0000FF); //自行创建Ball类
var ball:Ball = new Ball(15);
addChild(ball); //创建中间太阳
addChild(_ball); //创建 地球
_centerX = stage.stageWidth/2;
_centerY = stage.stageHeight/2;
ball.x = _centerX;
ball.y = _centerY;
graphics.lineStyle(1,0xCCCCCC,1);
graphics.moveTo(_centerX,_centerY);
addEventListener(Event.ENTER_FRAME,onEnterFrame);
stage.addEventListener(KeyboardEvent.KEY_UP,onKeyUp);
stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);
}
private function onEnterFrame(event:Event):void
{
//2 PI也就是一圈,14 PI 也就是7圈
if(_angerX>Math.PI*13&&flag)
{
flag = false;
}
graphics.lineStyle(_pen,0xE7E7E7,1);
_xpos = _ball.x = _centerX+Math.cos(_angerX)*_rangeX;
_ypos = _ball.y = _centerY+Math.sin(_angerY)*_rangeY;
_angerX += _xspeed;
_angerY += _yspeed;
//让开始作用于椭圆运行
if(flag)
{
_rangeX += 0.3;
_rangeY += .08;
}
//用次数统计清图,即Keyboar.UP事件的清图等于多余
if(_nums>800)
{
graphics.clear();
_nums = 0;
}
_nums++;
graphics.lineTo(_xpos,_ypos);
}
private function onKeyUp(event:KeyboardEvent):void
{
graphics.clear();
graphics.lineTo(_xpos,_ypos);
}
private function onKeyDown(event:KeyboardEvent):void
{
if(event.keyCode==Keyboard.LEFT)
{
_rangeY += -_speed;
}else if(event.keyCode==Keyboard.RIGHT){
_rangeX += _speed;
}else if(event.keyCode==Keyboard.UP){
_rangeX += -_speed;
}else if(event.keyCode==Keyboard.DOWN){
_rangeY += _speed;
}else if(event.keyCode==Keyboard.NUMPAD_ADD){
_ball.scaleX = _ball.scaleY += .1;
}else if(event.keyCode==Keyboard.NUMPAD_SUBTRACT){
_ball.scaleX = _ball.scaleY -= .1;
}
}
}
}
 

你可能感兴趣的:(UI,Flex,Flash,UP)