繁体   English   中英

在Flash / AS3中扔一个按钮

[英]Throwing a button in Flash / AS3

我正在尝试使按钮可拖动并将其拖到屏幕上。 我知道我必须为该按钮使用mouseDown和mouseUp选项,但是有人可以解释一种允许我先拖动然后将其抛出的方法吗? 换句话说,它应该继续跟随mouseUp。

预先感谢您提供的任何建议-克里斯

有趣的是,我在这附近有个老班。 只需将以下内容用作您想要能够抛出的任何对象的基类:

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

    /**
     * Use as a base class for objects to be grabbed and thrown with the mouse.
     * @author Marty Wallace.
     * @version 1.01.
     */
    public class BouncingObject 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;

        protected var bounce:Number = 0.3;
        protected var weight:Number = 3;
        protected var friction:Number = 2;


        /**
         * Constructor
         */
        public function BouncingObject()
        {
            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
        {
            removeEventListener(Event.ADDED_TO_STAGE, _init);

            addEventListener(Event.ENTER_FRAME, _handle);
            addEventListener(MouseEvent.MOUSE_DOWN, _handleClick);
            addEventListener(MouseEvent.MOUSE_UP, _handleRelease);
            addEventListener(MouseEvent.MOUSE_OUT, _handleRelease);
        }


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

            yv += weight;

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

            if(x < 0)
            {
                x = 0;
                xv = -xv*bounce;
                yv -= yv / friction;
            }
            if(x + width > stage.stageWidth)
            {
                x = stage.stageWidth - width;
                xv = -xv*bounce;
                yv -= yv / friction;
            }
            if(y < 0)
            {
                y = 0;
                yv = -yv*bounce;
                xv -= xv / friction;
            }
            if(y + height > stage.stageHeight)
            {
                y = stage.stageHeight - height;
                yv = -yv*bounce;
                xv -= xv / friction;
            }
        }


        /**
         * 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;
            }
        }


        /**
         * Deconstructor - remove event listeners.
         */
        public function deconstruct():void
        {
            removeEventListener(Event.ENTER_FRAME, _handle);
            removeEventListener(MouseEvent.MOUSE_DOWN, _handleClick);
            removeEventListener(MouseEvent.MOUSE_UP, _handleRelease);
            removeEventListener(MouseEvent.MOUSE_OUT, _handleRelease);
        }

    }
}

随意更改您需要的任何内容,例如将weight设置为0将防止物体掉落。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM