0

我正在 youtube 上编写一个教程来学习一些动作脚本 3。我已经完成了我的成品,它基本上是一个symbol被称为的ball,它有一个名为_ball. 教程中的成品显示在这里。

教程视频 - 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);
        }
    }    
}
4

1 回答 1

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();
    }
于 2012-11-10T21:00:50.967 回答