Actionscript 3 使用for循环设置动画第2部分
我还有一个问题一直困扰着我。这是我的另一个问题“好吧,我记下来了,但是我怎么能使每个球在舞台上随机移动,而不是互相跟随?” 这是我的密码: ball从名为ball的外部类中提取Actionscript 3 使用for循环设置动画第2部分,actionscript-3,flash-builder,Actionscript 3,Flash Builder,我还有一个问题一直困扰着我。这是我的另一个问题“好吧,我记下来了,但是我怎么能使每个球在舞台上随机移动,而不是互相跟随?” 这是我的密码: ball从名为ball的外部类中提取 包 { 导入flash.display.Sprite; 导入flash.events.Event; 导入flash.events.KeyboardEvent; 导入flash.text.TextField; 导入flash.ui.Keyboard; 公共类Dummy扩展了Sprite { 私有变量球:数组; 私人var球
包
{
导入flash.display.Sprite;
导入flash.events.Event;
导入flash.events.KeyboardEvent;
导入flash.text.TextField;
导入flash.ui.Keyboard;
公共类Dummy扩展了Sprite
{
私有变量球:数组;
私人var球:球;
私有变量ballNum:Number=10;
私有变量ax:Number=4;
公共函数虚拟()
{
init();
}
私有函数init():void
{
balls=新数组();
对于(变量i:Number=0;i
为每个球对象指定不同的方向,并使用该方向移动球,而不是使用ax
值
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.text.TextField;
import flash.ui.Keyboard;
public class Dummy extends Sprite
{
private var balls:Array;
private var ball:Ball;
private var ballNum: Number = 10;
private var directions:Array = [new Point(-1,-1),new Point(0,-1),new Point(1,-1),
new Point(-1,0),new Point(1,0),
new Point(-1,1),new Point(0,1),new Point(1,1)];
public function Dummy()
{
init();
}
private function init():void
{
balls = new Array();
for(var i:Number = 0; i < ballNum; i++)
{
ball = new Ball(Math.random() * 30);
ball.x = Math.random() * stage.stageWidth;
ball.y = Math.random() * stage.stageHeight;
ball.direction = directions[Math.floor(Math.random()*directions.length)];
addChild(ball);
balls.push(ball);
}
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
protected function onEnterFrame(event:Event):void
{
for(var i:int = 0; i < balls.length; i++)
{
balls[i].x += balls[i].direction.x;
balls[i].y += balls[i].direction.y;
}
}
}
}
包
{
导入flash.display.Sprite;
导入flash.events.Event;
导入flash.events.KeyboardEvent;
导入flash.text.TextField;
导入flash.ui.Keyboard;
公共类Dummy扩展了Sprite
{
私有变量球:数组;
私人var球:球;
私有变量ballNum:Number=10;
私有变量方向:数组=[新点(-1,-1),新点(0,-1),新点(1,-1),
新点(-1,0),新点(1,0),
新点(-1,1),新点(0,1),新点(1,1)];
公共函数虚拟()
{
init();
}
私有函数init():void
{
balls=新数组();
对于(变量i:Number=0;i
为您的球提供速度的类属性,以及一个方法move()
,该方法将调整它们各自的坐标,并且可能会在稍后进行增强以检查碰撞,以便您的球可以反弹到墙壁上
// Ball class additions follow
private var vx:Number;
private var vy:Number;
public function Ball() {
// ... original constructor here
var angle:Number=Math.random(2.0*Math.PI); // random angle
var vel:Number= MAX_VELOCITY*(0.5+0.5*Math.random()); // random velocity module
// define this constant^
vx=vel*Math.cos(angle);
vy=vel*Math.sin(angle); // composites of velocity
}
public function move():void {
this.x+=vx;
this.y+=vy;
}
// main class
protected function onEnterFrame(event:Event):void
{
for(var i:int = 0; i < balls.length; i++)
{
balls[i].move();
}
}
//随后将添加Ball类
私有变量vx:编号;
私人变量vy:数字;
公共功能球(){
//…这里是原始构造函数
变量角度:Number=Math.random(2.0*Math.PI);//随机角度
var-vel:Number=MAX_-VELOCITY*(0.5+0.5*Math.random());//随机速度模块
//定义这个常数^
vx=水平*数学cos(角度);
vy=vel*Math.sin(角度);//速度的合成
}
公共函数move():void{
这个.x+=vx;
这个.y+=vy;
}
//主类
受保护的函数onEnterFrame(事件:事件):void
{
对于(变量i:int=0;i
谢谢您的回答,但它给了我一个错误。这里:ball.direction=directions[Math.floor(Math.random()*directions.length)];你需要在你的ball类中添加一个var direction:Point
成员。这样做了,非常感谢。但是我真的不明白它是怎么发生的。你能帮我理解一下吗?基本上我创建了一个数组,它有球可以进入的8个可能方向,叫做directions
e balls我使用代码方向[Math.floor(Math.random()*directions.length)];
为每个球选择一个随机方向。这将为该球指定一个随机方向。最后在onEnterFrame
事件中,球使用随机方向移动。谢谢,我在这里收到一个错误:var angle:Number=Math.random(2.0*Math.PI);//随机角度变量vel:Number=MAX_速度*(0.5+0.5*Math.random());//随机速度模块
您必须声明常数。您的,船长。
// Ball class additions follow
private var vx:Number;
private var vy:Number;
public function Ball() {
// ... original constructor here
var angle:Number=Math.random(2.0*Math.PI); // random angle
var vel:Number= MAX_VELOCITY*(0.5+0.5*Math.random()); // random velocity module
// define this constant^
vx=vel*Math.cos(angle);
vy=vel*Math.sin(angle); // composites of velocity
}
public function move():void {
this.x+=vx;
this.y+=vy;
}
// main class
protected function onEnterFrame(event:Event):void
{
for(var i:int = 0; i < balls.length; i++)
{
balls[i].move();
}
}