Actionscript 3 从阵列中确定颜色的目标

Actionscript 3 从阵列中确定颜色的目标,actionscript-3,loops,Actionscript 3,Loops,在这个项目中,我想要一个颜色,我选择由一个框跟踪。我的代码的问题是,即使舞台上有两种相同的颜色,它也只会跟踪其中一种颜色。如果你能帮我,非常感谢 Main.as package { import flash.display.Sprite; import flash.events.Event; import flash.geom.Point; [SWF(frameRate='31', width='1000', height='500')] public class Main ext

在这个项目中,我想要一个颜色,我选择由一个框跟踪。我的代码的问题是,即使舞台上有两种相同的颜色,它也只会跟踪其中一种颜色。如果你能帮我,非常感谢

Main.as

package {

import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;

[SWF(frameRate='31', width='1000', height='500')]

    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 box:Box;

        /*private var padding:Number = 20;

        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 (balls[i].colors == 0x79DCF4) {
                    box.x = balls[i].x - box.width / 2;
                    box.y = balls[i].y - box.height / 2;
                }
            }
        }
    }
}
package{

    import flash.display.Sprite;
    import flash.geom.Point;

    public class Ball extends Sprite
    {
        public var colors:Array = [0xFFFF33, 0xa5a5a5, 0x79DCF4, 0xFF3333, 0xFFCC33, 0x99CC33];
        public var color:uint;
        public var radius:Number;
        public var direction:Point;

        public function Ball(radius:Number = 15, color:uint = 0xcccccc)
        {
            this.color = colors[randomColor(0, colors.length - 1)];
            this.radius = radius;

            init();
        }
        private function randomColor(min:Number, max:Number):Number
        {
            var randomNumber:Number = Math.round(Math.random() * (max - min) + min);
            return randomNumber;
        }

        private function init():void
        {
            graphics.beginFill(color);
            graphics.drawCircle(0,0,radius);
            graphics.endFill();
        }
    }
}

我想你想追踪颜色,而不是颜色

所以改变这条线

if (balls[i].colors == 0x79DCF4) {

尝试在每一帧中跟踪一个球

    private var blueBalls:Array = [];//to save the blue balls

    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);

            if (ball.color == 0x79DCF4) {
                blueBalls.push(ball);
            }
        }

        box = new Box();
        addChild(box);

        addEventListener(Event.ENTER_FRAME, onEnterFrame);
    }

    private var lastBlueBallIndex:int = -1;//save which blue ball has been tracked in blueballs

    private var hasFind:Boolean = false;

    private function onEnterFrame(event:Event):void
    {
        hasFind = false;

        for(var i:int = 0; i < balls.length; i++)
        {
            balls[i].x += balls[i].direction.x;
            balls[i].y += balls[i].direction.y;

            var index:int = blueBalls.indexOf(balls[i]);

            if (index != -1 && !hasFind ) {

                //here is the main part, when reach last blue ball, reset lastBlueBallIndex 
                if (index == blueBalls.length - 1) {//if get last blue ball
                    lastBlueBallIndex = -1;
                } else if (lastBlueBallIndex  >= index) {//if the ball has been tracked
                    continue;
                } else {
                    lastBlueBallIndex = index;//set the current ball index
                }

                hasFind  = true;

                box.x = balls[i].x - box.width / 2;
                box.y = balls[i].y - box.height / 2;
            }
        }
    }

谢谢你的回答,还有一个问题。它仍然只跟踪一个蓝球。我已经编辑了答案。如果你想追踪不同的蓝球,你不能在一帧内追踪。你可以在每一帧内追踪一个。再次感谢,但它仍然只追踪一个蓝球。我已经编辑了答案并修复了语法和逻辑错误。我已经测试了代码,它工作了。非常感谢,它工作了,但是一旦跟踪球,盒子就会闪烁。
    private var blueBalls:Array = [];//to save the blue balls

    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);

            if (ball.color == 0x79DCF4) {
                blueBalls.push(ball);
            }
        }

        box = new Box();
        addChild(box);

        addEventListener(Event.ENTER_FRAME, onEnterFrame);
    }

    private var lastBlueBallIndex:int = -1;//save which blue ball has been tracked in blueballs

    private var hasFind:Boolean = false;

    private function onEnterFrame(event:Event):void
    {
        hasFind = false;

        for(var i:int = 0; i < balls.length; i++)
        {
            balls[i].x += balls[i].direction.x;
            balls[i].y += balls[i].direction.y;

            var index:int = blueBalls.indexOf(balls[i]);

            if (index != -1 && !hasFind ) {

                //here is the main part, when reach last blue ball, reset lastBlueBallIndex 
                if (index == blueBalls.length - 1) {//if get last blue ball
                    lastBlueBallIndex = -1;
                } else if (lastBlueBallIndex  >= index) {//if the ball has been tracked
                    continue;
                } else {
                    lastBlueBallIndex = index;//set the current ball index
                }

                hasFind  = true;

                box.x = balls[i].x - box.width / 2;
                box.y = balls[i].y - box.height / 2;
            }
        }
    }
private function init():void {

    /*the old code*/


    var timer:Timer = new Timer(1000);//set the interval time longger if you want
    // you can change onEnterFrame to another name, like onTimer
    timer.addEventListener(TimerEvent.TIMER, onEnterFrame);
    timer.start();

}