【问题标题】:How to get MovieClip.prototype in ActionScript 3?如何在 ActionScript 3 中获取 MovieClip.prototype?
【发布时间】:2011-03-28 23:21:17
【问题描述】:

在 ActionScript 2 代码示例中,它使用了一些 MovieClip.prototype 的函数,如 MovieClip.prototype.setModel;

我正在尝试使用 ActionScript 3 类编写此示例,但 MoviewClip.prototype 不存在,我可以从哪里获得它?它是否存在于 ActionScript 3 中?

--更新

好的,正如您所问的那样,这段代码可以作为 frame1 中包含的 ActionScript 正常工作,但我想用相同的代码创建一个 ActionScript 3 类:

//package
//{
//  public class asteroids extends MovieClip
//  {

        var MW = 8;                     // Scaling factor for models (which were originally drawn on graph paper)
        var SW = Stage.width;           // Stage coords
        var SH = Stage.height;
        var kDegToRad = Math.PI/180;    // Useful constant for drawing circles & such
        var kDamp = 0.99;               // Damping in ship acceleration

        // The models
        //
        // Rocket fuselage
        var fuseModel = [{mx:0, my:-5.5, x:0, y:-4, pen:.5, clr:0x000033, alpha:50},
            {cx:-1,cy:-3,x:-1,y:-1, pen:2, clr:0x000033, alpha:50},
            {x:-.75,y:4},
            {cx:0, cy:4.5, x:.75,y:4},  // from -.75,4
            {x:1,y:-1},
            {cx:1,cy:-3,x:0,y:-4}];

        // Rocket fins
        var finModel = [ {mx:-1,my:-1,cx:-3,cy:4,x:-2,y:6,bf:0x0000FF,bfa:80},
            {cx:-1,cy:4.5,x:-.75,y:4}, 
            {x:-1,y:-1},
            {mx:.75,my:4,cx:1,cy:4.5,x:2,y:6},
            {cx:3,cy:4,x:1,y:-1},
            {x:.75,y:4,ef:1}
        ];

        // Routine to scale model to arbitrary size
        function scaleModel(m,s)
        {
            for (var i = 0; i < m.length; ++i)
            {
                var pt = m[i];
                if (pt.mx != undefined)
                {
                    pt.mx *= s;
                    pt.my *= s;
                }
                if (pt.cx != undefined)
                {
                    pt.cx *= s;
                    pt.cy *= s;
                }
                pt.x *= s;
                pt.y *= s;
            }
        }

        // Draw a model
        //
        function drawModel(m)
        {
            for (var i = 0; i < m.length; ++i) 
            {
                var pt = m[i];
                if (pt.bf != undefined)
                    this.beginFill(pt.bf, pt.bfa);
                if (pt.pen != undefined)
                    this.lineStyle(pt.pen,pt.clr,pt.alpha);
                if (pt.mx != undefined)
                    this.moveTo(pt.mx,pt.my);
                if (pt.cx != undefined)
                    this.curveTo(pt.cx,pt.cy,pt.x,pt.y);
                else if (pt.x != undefined)
                    this.lineTo(pt.x,pt.y);
                if (pt.ef != undefined)
                    this.endFill();
            }
        }


        // Ship Movement and most game-play stuff happens here
        function ShipMove()
        {

            // Steering & Thrust
            if (Key.isDown(Key.LEFT))
                this._rotation -= 5;
            else if (Key.isDown(Key.RIGHT))
                this._rotation += 5;
            if (Key.isDown(Key.CONTROL) || Key.isDown(Key.UP))
            {
                this.vx += Math.sin(this._rotation*kDegToRad);
                this.vy -= Math.cos(this._rotation*kDegToRad);
            }
            else if (Key.isDown(Key.DOWN)) 
            {
                this.vx *= .9;
                this.vy *= .9;
            }
            // Basic movement with acceleration and damping
            this._x += this.vx;
            this._y += this.vy;

            // Wrap around edges of stage
            if (this._x < -this._width)
                this._x += SW+this._width*2;
            else if (this._x > SW+this._width)
                this._x -= SW+this._width*2;
            if (this._y < -this._height)
                this._y += SH+this._height*2;
            else if (this._y > SH+this._height)
                this._y -= SH+this._height*2;

        }


        // Initialize new ship
        function NewShip()
        {
            ship_mc._x = SW/2;
            ship_mc._y = SH/2;
            ship_mc.onEnterFrame = shipMove;
        }

        // Assign a sprite model to a movieclip & draw it
        //
        MovieClip.prototype.setModel = function(m)
        {
            this.model = m;
            this.drawModel(m);
        }

        MovieClip.prototype.drawModel = drawModel;

        scaleModel(fuseModel, MW*.8); // Using a slightly smaller rocket than original drawing
        scaleModel(finModel, MW*.8);

        // One Time Initialziation
        //
        _root.createEmptyMovieClip("ship_mc", 2);
        ship_mc.createEmptyMovieClip("fuselage", 1);
        ship_mc.fuselage.setModel(fuseModel);
        ship_mc.createEmptyMovieClip("fins", 2);
        ship_mc.fins.setModel(finModel);

        NewShip();

//  }
//}

【问题讨论】:

  • 你能描述更多关于你想做的事情吗? AS3 的内部有一种类似于原型的东西,但它通常不是一个很有帮助的东西。通常你想继承 MovieClip 并装饰或覆盖东西,但如果你评论你的目标,我们可以更具体。

标签: actionscript-3


【解决方案1】:

您可能应该扩展 MovieClip 并将新功能添加到子类中,这是最好/正确的方法。 AS2 中的原型很旧,并且在语言不支持真正的 OOP 时使用。

【讨论】:

    【解决方案2】:

    不鼓励在 AS3 中使用原型。 但是如果你用 flex 编译器编译 AS3,有一种方法可以使用它。

    首先,您需要提供一些编译器选项。

    PrototypeExtension-config.xml

    <flex-config>
        <compiler>
           <as3>false</as3>
           <es>true</es>
           <strict>false</strict>
        </compiler>
    </flex-config>
    

    PrototypeExtension.as

    package {
        import flash.display.Sprite;
        import flash.display.MovieClip;
        import flash.events.Event;
        import flash.geom.Point;
        import flash.utils.getTimer;
    
        public class PrototypeExtension extends Sprite {
            public function PrototypeExtension() {
                stage.align = "tl";
                stage.scaleMode = "noScale";
                MovieClip.prototype.move = function (x:int, y:int):void {
                    this.x = x; this.y = y;
                };
                MovieClip.prototype.tween = function (to:Point, duration:int):void {
                    var start:int = getTimer();
                    var from:Point = new Point(this.x, this.y);
                    var thisInstance:MovieClip = this;
                    this.addEventListener(Event.ENTER_FRAME, function tweener(e:Event):void {
                        var time:int = getTimer() - start;
    
                        if (time > duration) {
                            thisInstance.move(to.x, to.y);
                            thisInstance.removeEventListener(Event.ENTER_FRAME, tweener);
                        }
                        var t:Number = time / duration;
                        t = t * (2 - t);
                        var u:Number = 1 - t;
                        thisInstance.move(u * from.x + t * to.x, u * from.y + t *to.y);
                    });
    
                }
                var mc:MovieClip = new MovieClip;
                mc.graphics.beginFill(0);
                mc.graphics.drawRect(0, 0, 100, 100);
                mc.graphics.endFill();
                mc.tween(new Point(400, 300), 1000);
                addChild(mc);
            }
        }
    }
    

    通过

    mxmlc PrototypeExtension.as

    您可以看到在 AS3 中如何启用原型扩展。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-06
      • 1970-01-01
      • 2011-04-07
      • 1970-01-01
      相关资源
      最近更新 更多