ActionScript - 忽略传递的参数?

时间:2011-08-15 07:12:34

标签: actionscript-3 argument-passing

我一直在研究Rung-Kutta物理的这个代码示例,但我不明白acceleration(p:Point, v:Point):Point函数发生了什么。该函数接受2个点对象作为必需参数,但在函数中不使用它们,只需返回一个新点。

我不熟悉这种论证传递方式。有人可以向我解释这个功能的意义吗?

来源于Keith Peters的书 Advanced ActionScript 3.0 Animation ,第6章 - 高级物理,第246页。

package {
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.geom.Point;
    import flash.utils.getTimer;

    public class RK2 extends Sprite
    {
        private var _ball:Sprite;
        private var _position:Point;
        private var _velocity:Point;
        private var _gravity:Number = 32;
        private var _bounce:Number = -0.6;
        private var _oldTime:int;
        private var _pixelsPerFoot:Number = 10;


        public function RK2()
        {
            stage.align = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;

            _ball = new Sprite();
            _ball.graphics.beginFill(0xff0000);
            _ball.graphics.drawCircle(0, 0, 20);
            _ball.graphics.endFill();
            _ball.x = 50;
            _ball.y = 50;
            addChild(_ball);

            _velocity = new Point(10, 0);
            _position = new Point(_ball.x / _pixelsPerFoot, _ball.y / _pixelsPerFoot);

            _oldTime = getTimer();
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }

        private function onEnterFrame(event:Event):void
        {
            var time:int = getTimer();
            var elapsed:Number = (time - _oldTime) / 1000;
            _oldTime = time;

            var accel1:Point = acceleration(_position, _velocity);

            var position2:Point = new Point();
            position2.x = _position.x + _velocity.x * elapsed;
            position2.y = _position.y + _velocity.y * elapsed;

            var velocity2:Point = new Point();
            velocity2.x = _velocity.x + accel1.x * elapsed;
            velocity2.y = _velocity.y + accel1.x * elapsed;

            var accel2:Point = acceleration(position2, velocity2);

            _position.x += (_velocity.x + velocity2.x) / 2 * elapsed;
            _position.y += (_velocity.y + velocity2.y) / 2 * elapsed;

            _velocity.x += (accel1.x + accel2.x) / 2 * elapsed;
            _velocity.y += (accel1.y + accel2.y) / 2 * elapsed;

            if(_position.y > (stage.stageHeight - 20) / _pixelsPerFoot)
            {
                _position.y = (stage.stageHeight - 20) / _pixelsPerFoot;
                _velocity.y *= _bounce;
            }
            if(_position.x > (stage.stageWidth - 20) / _pixelsPerFoot)
            {
                _position.x = (stage.stageWidth - 20) / _pixelsPerFoot;
                _velocity.x *= _bounce
            }
            else if(_position.x < 20 / _pixelsPerFoot)
            {
                _position.x = 20 / _pixelsPerFoot;
                _velocity.x *= _bounce;
            }

            _ball.x = _position.x * _pixelsPerFoot;
            _ball.y = _position.y * _pixelsPerFoot;
        }

        private function acceleration(p:Point, v:Point):Point
        {
            return new Point(0, _gravity);
        }
    }
}

2 个答案:

答案 0 :(得分:2)

我认为作者可能正在使用方法acceleration作为占位符,也许是为了后续章节的更新。

当然,正如现在一样,加速方法可以改写为

private function acceleration(...rest):Point {
    return new Point(0, _gravity);
}

或者可以完全删除参数(尽管这需要更新调用方法的地方不包含任何参数。)

这本身并不是一种编程风格,但是,我之前已经看过这种类型的占位符代码。

答案 1 :(得分:0)

您也可以将参数设置为默认值为null,因此它们是可选

    private function acceleration(p:Point = null, v:Point = null):Point
    {
        return new Point(0, _gravity);
    }