在這個項目中,我試圖追蹤一個球。如果球的顏色是灰色的,我希望它被正方形包圍。我的問題是隻有一個球被追蹤。我想要追蹤所有的灰色球。追蹤動畫球
如果有人能幫助我,我會非常感激。
下面是代碼(球和Box從外部類的稱呼):你需要把你的支票顏色在for循環檢查所有的球
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;
[SWF(frameRate='31')]
public class Main extends Sprite
{
private var balls:Array;
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)
];
private var ballNum: Number = 10;
private var ball:Ball;
private var ball2:Ball;
private var box:Box;
private var ay:Number = 5;
private var gravity:Number = 6;
private var bounce:Number = -0.9;
public function Main()
{
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);
}
box = new Box();
addChild(box);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private 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;
}
if(ball.color == 0xcccccc)
{
box.x = ball.x - box.width/2;
box.y = ball.y - box.height/2;
}
}
}
}
如何改變顏色? – putvande
現在,顏色只設置爲灰色。 – Scope