如何创建一个假掷的效果呢?创建一个、效果

2023-09-08 15:21:30 作者:低調de张揚

我学AS3,我试图建立一个小的互动,我可以抛出一个正方形走下舞台。它只是需要留在x轴,被抛出或者向左或向右移动。

I'm learning AS3 and I'm trying to build a little interaction where I can throw a square off the stage. It just need to stay on the x-axis, to be thrown either to the left or right.

我发现了不少的教程,其中涉及物理学。一样复杂,我什么都不需要。随着我的,只要你点击向下广场和扔了一下,并没有多么艰苦,投掷动作,该广场将永远被甩出的阶段,这取决于你鼠标的移动方向。

I found quite a few tutorials where it involves physics. I don't need anything as complicated as that. With mine, as long as you click down on the square and throw it a bit, doesn't matter how hard that throwing action is, the square will always be thrown off the stage, depending on the direction of your mouse.

我种有这个想法在我的脑海了扔的感觉可以用吐温在影片剪辑来实现(因为它仅仅需要到左或右),这将取决于该方向被激活鼠标。

I kind of have the idea in my head that the "throw" feel can be done with tween in a movieclip (since it only need to go to either left or right), which will be activated depending on the direction of the mouse.

我道歉,如果这是一个基本的问题,我不知道如何下手就可以了。请帮帮忙!

I apologies if this is a basic question, I have no idea how to start on it. Please help!

推荐答案

尝试使用这个作为你想扔对象的基类。

Try using this as a base class for the object that you want to throw.

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

    public class ThrowableObject extends Sprite
    {
        // properties
        public var yv:Number = 0;
        public var xv:Number = 0;

        private var _grabbed:Boolean = false;
        private var _gy:int = 0;
        private var _gx:int = 0;

        private var ox:int = 0;
        private var oy:int = 0;

        /**
         * Constructor
         */
        public function ThrowableObject()
        {
            addEventListener(Event.ADDED_TO_STAGE, _init);
        }

        /**
         * Called on dispatch of Event.ADDED_TO_STAGE
         * @param e Event.ADDED_TO_STAGE
         */
        private function _init(e:Event):void
        {
            addEventListener(Event.ENTER_FRAME, _handle);
            addEventListener(MouseEvent.MOUSE_DOWN, _handleClick);
            stage.addEventListener(MouseEvent.MOUSE_UP, _handleRelease);
        }

        /**
         * Called on dispatch of Event.ENTER_FRAME
         * @param e Event.ENTER_FRAME
         */
        private function _handle(e:Event):void
        {
            ox = x;
            oy = y;

            if(_grabbed)
            {
                x = parent.mouseX - _gx;
                y = parent.mouseY - _gy;
            }
            else
            {
                x += xv;
                y += yv;
            }
        }

        /**
         * Called on dispatch of MouseEvent.MOUSE_DOWN
         * @param e MouseEvent.MOUSE_DOWN
         */
        private function _handleClick(e:MouseEvent):void
        {
            grabbed = true;
            parent.addChild(this);
        }

        /**
         * Called on dispatch of MouseEvent.MOUSE_UP
         * @param e MouseEvent.MOUSE_UP
         */
        private function _handleRelease(e:MouseEvent):void
        {
            grabbed = false;
        }

        /**
         * Sets grabbed
         * @param val Boolean representing value to set grabbed as
         */
        protected function set grabbed(bool:Boolean):void
        {
            _grabbed = bool;

            if(_grabbed)
            {
                _gx = mouseX;
                _gy = mouseY;
            }
            else
            {
                xv = x - ox;
                yv = y - oy;
            }
        }
    }
}