创造边界

时间:2015-08-31 11:13:05

标签: actionscript-3 flash boundary

Hello Everyone again :)我实际上有关于创建边界的边界问题。在我的舞台上,我有2个名字:Dispenser和Puller。产生颗粒和拉拔器的分配器正在拉动它们。我只需要像路径一样设置边界。enter image description here
但我无法请求你的帮助:)

Bdw代码来自此网站:http://www.freeactionscript.com/?s=puller&x=0&y=0

package 
{
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;

    public class Main extends Sprite
    {
        // you can play around with these
        private var particlesTotal:Number = 50;
        private var particleSpeed:Number = 10;
        private var particleFadeoutSpeed:Number = .0175;

        // don't change these
        private var particlesCurrent:Number = 0;
        private var particlesArray:Array;      
        private var radians:Number;
        private var _sayac:uint;

        public function Main():void
        {
            init();
        }

        private function init():void
        {
            radians = 180 / Math.PI;
            particlesArray = [];

            addEventListener(Event.ENTER_FRAME, onEnterFrameLoop);

            dispenser_mc.addEventListener(MouseEvent.MOUSE_DOWN, onThingDown);
            puller_mc.addEventListener(MouseEvent.MOUSE_DOWN, onThingDown);

            dispenser_mc.addEventListener(MouseEvent.MOUSE_UP, onThingUp);
            puller_mc.addEventListener(MouseEvent.MOUSE_UP, onThingUp);

        }

        private function onThingDown(event:MouseEvent):void
        {
            event.currentTarget.startDrag();
        }
        private function onThingUp(event:MouseEvent):void
        {
            event.currentTarget.stopDrag();
        }

        private function createParticle(target1:MovieClip, target2:MovieClip):void
        {      
            if(particlesTotal <= particlesCurrent)
            {
                return;
            }
            particlesCurrent++;

            var tempParticle:Particle = new Particle();

            tempParticle.x = (target1.x - target1.width / 2) + (Math.random() * target1.width);
            tempParticle.y = (target1.y - target1.height / 2) + (Math.random() * target1.height);
            tempParticle.rotation = Math.random()*360;

            tempParticle.rot = Math.atan2(target1.y - target2.y, target1.x - target2.x);
            tempParticle.xSpeed = Math.cos(tempParticle.rot) * radians / particleSpeed;
            tempParticle.ySpeed = Math.sin(tempParticle.rot) * radians / particleSpeed;
            tempParticle.mass = tempParticle.width / 2 + tempParticle.height / 2;

            particlesArray.push(tempParticle);

            addChild(tempParticle);

        }

        private function updateParticle():void
        {
            for (var i = 0; i < particlesArray.length; i++)
            {
                var tempParticle:MovieClip = particlesArray[i];

                tempParticle.x -= tempParticle.xSpeed;
                tempParticle.y -= tempParticle.ySpeed;  
                tempParticle.alpha -= particleFadeoutSpeed;
                // I know i can set boundries here but idk how to so :)

                if(tempParticle.hitTestObject(puller_mc))
                {
                    destroyParticle(tempParticle);
                    _sayac++;
                    trace(_sayac);
                }
                else if (tempParticle.alpha <= 0)
                {
                    destroyParticle(tempParticle);
                }
                else if (tempParticle.x < 0)
                {
                    destroyParticle(tempParticle);
                }
                else if (tempParticle.x > stage.stageWidth)
                {
                    destroyParticle(tempParticle);
                }
                else if (tempParticle.y < 0)
                {
                    destroyParticle(tempParticle);
                }
                else if (tempParticle.y > stage.stageHeight)
                {
                    destroyParticle(tempParticle);
                }
            }
        }

        private function destroyParticle(particle:MovieClip):void
        {
            for (var i = 0; i < particlesArray.length; i++)
            {
                var tempParticle:MovieClip = particlesArray[i];
                if (tempParticle == particle)
                {
                    particlesCurrent--;
                    particlesArray.splice(i,1);
                    removeChild(tempParticle);
                }
            }
        }

        private function onEnterFrameLoop(event:Event):void
        {
            createParticle(dispenser_mc, puller_mc);
            updateParticle();
        }

    }   
}

1 个答案:

答案 0 :(得分:0)

实际上这段代码没有边界。让我为您解释一下这段代码的主要部分:

这是最重要的部分:

        tempParticle.rot = Math.atan2(target1.y - target2.y, target1.x - target2.x);
        tempParticle.xSpeed = Math.cos(tempParticle.rot) * radians / particleSpeed;
        tempParticle.ySpeed = Math.sin(tempParticle.rot) * radians / particleSpeed;

rot是到达拉拔器的角度 xSpeed是粒子应该到达每一帧到达拉拔器的程度 ySpeed是粒子应该沿着每一帧下降到达拉拔器的程度。

并且在updateParticle函数中,partcle将根据xSpeedySpeed移动。

所以正确的xSpeedYSpeed会导致粒子直线移动。不是边界!

我知道我解释得很好,而且 我知道这有帮助!