-2

我一直试图将我在 AS2 中制作的这个很棒的代码转换为 AS3,但没有成功,我知道它一定相当简单,但我对 AS3 了解不多。请帮忙

reference = 240;

MovieClip.prototype.asNewStar = function()
{
    this.x = random(Stage.width)-Stage.width/2;
    this.y = random(Stage.height)-Stage.height/2;
    this.z = 1000;

    this.onEnterFrame = function()
    {
        if ((this.z -= 20) < -100) this.removeMovieClip();
        else this.drawStar();
    }
}

MovieClip.prototype.drawStar = function()
{
    this.clear();
    this.lineStyle(10-this.z/200, Math.random() * 0xFFFFFF, 100);
    perspectiveRatio = reference/(reference + this.z);
    this.moveTo(this.x*perspectiveRatio, this.y*perspectiveRatio);
    perspectiveRatio = reference/(reference + this.z + 40);
    this.lineTo(this.x*perspectiveRatio, this.y*perspectiveRatio);
}


this.createEmptyMovieClip("galaxy",1);

galaxy._x = Stage.width / 2;
galaxy._y = Stage.height / 2;
galaxy.onEnterFrame = function()
{
    this.createEmptyMovieClip("star"+ ++depth, depth).asNewStar();
}
4

1 回答 1

1
package
{
import flash.display.DisplayObjectContainer;
import flash.display.MovieClip;
import flash.events.Event;

public class Star extends MovieClip // or Sprite if not need MovieClip's features.
{

    //------------ properties ------------//

    //------------ constructor -----------//

    public function Star()
    {
        super();
        addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
        addEventListener(Event.ENTER_FRAME, enterFrameHandler);
    }

    //----- initialize / dispose ---------//

    // in your code it was:
    // createEmptyMovieClip("star"+ ++depth, depth).asNewStar();
    public static function createStarIn(container:DisplayObjectContainer):Star
    {
        return container.addChild(new Star()) as Star;
    }

    public function dispose():void
    {
        removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
        removeFromParent(false);
    }

    // in your code it was:
    // removeMovieClip()
    public function removeFromParent(dispose:Boolean = false):void
    {
        parent && (parent as DisplayObjectContainer).removeChild(this);
        dispose && this.dispose();
    }

    //--------------- ctrl ---------------//

    private function drawStar():void
    {
        graphics.clear();
        graphics.lineStyle(10 - z / 200, Math.random() * 0xFFFFFF, 100);
        perspectiveRatio = reference / (reference + z);
        graphics.moveTo(x * perspectiveRatio, y * perspectiveRatio);
        perspectiveRatio = reference / (reference + z + 40);
        graphics.lineTo(x * perspectiveRatio, y * perspectiveRatio);
    }

    //------------ accessors -------------//

    //------- handlers / callbacks -------//

    private function addedToStageHandler(e:Event):void
    {
        removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);

        x = Math.random() * stage.stageWidth - stage.stageWidth / 2;
        y = Math.random() * stage.stageHeight - stage.stageHeight / 2;
        z = 1000;
    }

    private function enterFrameHandler(e:Event):void
    {
        if((z -= 20) < -100)
            dispose();
        else
            drawStar();
    }
}
}
于 2013-07-12T11:09:08.883 回答