Actionscript 3 - 旋转符号?

时间:2012-11-10 16:20:01

标签: actionscript-3 actionscript rotation symbols

我正在研究youtube上的教程,以学习一些动作脚本3.我的成品基本上是symbol,称为ball并且它有一个实例叫_ball。本教程中的成品如下所示。

Tutorial Video - Youtube

所以基本上我想要实现的是球旋转,这取决于球的移动方式我将如何实现这一目标?我是动作脚本的新手,所以一些代码示例将被赞赏或深入解释。

任何人都想要一份代码 - 这就是它 - 我在某些方面对它进行了编辑,但它没有太大影响。

package
{
    import flash.display.MovieClip
    import flash.text.TextField
    import flash.events.Event
    import flash.events.MouseEvent

    public class DocumentMain extends MovieClip
    {
        public const GRAVITY:Number = 2; // Declaring a const variable known as gravity
        public const BOUNCE:Number = 0.8;
        public const HIT:Number = 15;

        public var _bounces:TextField;
        public var _highscore:TextField;
        public var _ball:Ball;

        private var _vx:Number; // Declaring a variable known as _vx
        private var _vy:Number; // Declaring a variable knwon as _vy

        public function DocumentMain(): void
        {
            _vx = Math.random(); // Initalising _vx
            _vy = Math.random(); // Initalising _vy

            _ball.buttonMode = true;

            addEventListener(Event.ENTER_FRAME, enterFrameHandler);
            addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
        }

        private function enterFrameHandler (e:Event):void
        {
            // Gravitate the Ball
            _vy += GRAVITY; // The ball is effected by gravity each frame

            // Move The Ball
            _ball.x += _vx;
            _ball.y += _vy;

            // Check Stage Boundaries For Collisions
            checkBoundaryCollision();
        }

        private function mouseDownHandler (e:MouseEvent):void
        {
            // Hit the ball if it has been clicked
            if (e.target == _ball)
            {
                hit(e.target.mouseX, e.target.mouseY);
            }
        }

        private function checkBoundaryCollision():void
        {
            var left:Number;
            var right:Number;
            var bottom:Number;
            var top:Number;

            left = _ball.x - (_ball.width / 2);
            right = _ball.x + (_ball.width / 2);
            bottom = _ball.y + (_ball.height / 2);
            top = _ball.y - (_ball.height / 2);

            if (left < 0 && _vx < 0)
            {
                _ball.x = (_ball.width / 2)
                _vx  *= -1;
            }
            else if (right > stage.stageWidth && _vx > 0)
            {
                _ball.x = stage.stageWidth - (_ball.width / 2)
                _vx  *= -1;
            }

            if (top <= 42.70 && _vy < 0)
            {
                _ball.y = (_ball.height / 2)
                _vy  *= -1;
            }
            else if (bottom > stage.stageHeight && _vy > 0)
            {
                _ball.y = stage.stageHeight - (_ball.height/2)
                _vy *= -BOUNCE;
                _vx *= BOUNCE;

                if (Number(_bounces.text) > Number(_highscore.text))
                {
                    _highscore.text = _bounces.text;
                }

                _bounces.text = "0";
            }
        }

        private function hit(hitX:Number, hitY:Number):void
        {
            // increment bounces
            _bounces.text = String(Number(_bounces.text) + 1);
            // Adjust vertical velocity
            if (_vy > 0)
            {
                _vy *= -BOUNCE / 2;
            }

            _vy -= HIT;

            //adjust horizontal veloity
            if (_vx * hitX > 0)
            {
                _vx *= -BOUNCE;
            }

            _vx -= (hitX / _ball.width * HIT);
        }
    }    
}

1 个答案:

答案 0 :(得分:0)

我现在无法对此进行测试,但它应该像更新你的enterFrameHandler一样简单,将每个帧上的球旋转一个基础值乘以球的x速度:

    public const ROTATION:Number = 1;

    private function enterFrameHandler (e:Event):void
    {
        // Gravitate the Ball
        _vy += GRAVITY; // The ball is effected by gravity each frame

        // Move The Ball
        _ball.x += _vx;
        _ball.y += _vy;

        // Rotate the ball each frame at a speed and direction 
        // related to the ball's current x velocity. A negative
        // x velocity will result in a negative rotation.
        _ball.rotation += ROTATION * _vx;

        // Check Stage Boundaries For Collisions
        checkBoundaryCollision();
    }