在不同的框架中初始化代码(在 actionscript 文件中)

Initialize the code (in a actionscript file) in a different frame

我怎样才能在不同的帧中使用函数 init 来初始化这个游戏?我正在使用 Actionscript 3.0。

希望你能帮助我。 这是代码(代码在 actionscript 文件中):

package{
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event; //used for ENTER_FRAME event

public class Main extends MovieClip{

    //constants
    const gravity:Number = 1.5;            //gravity of the game
    const dist_btw_obstacles:Number = 300; //distance between two obstacles
    const ob_speed:Number = 8;             //speed of the obstacle
    const jump_force:Number = 15;          //force with which it jumps

    //variables
    var player:Player = new Player();      
    var lastob:Obstacle = new Obstacle();  //varible to store the last obstacle in the obstacle array
    var obstacles:Array = new Array();     //an array to store all the obstacles
    var yspeed:Number = 0;                 //A variable representing the vertical speed of the bird
    var score:Number = 0;                  //A variable representing the score

    public function Main(){
        init();
    }

    function init():void {
        //initialize all the variables
        player = new Player();
        lastob = new Obstacle();
        obstacles = new Array();
        yspeed = 0;
        score = 0;

        //add player to center of the stage the stage
        player.x = stage.stageWidth/2;
        player.y = stage.stageHeight/2;
        addChild(player);

如果有框架(对于 Adob​​e Animate 项目),使用 addFrameScript:

someMC.addFrameScript(2, myFunction);

private function myFunction():void{
  this.stop();
}

myFunction 如果框架 2 存在,则工作。

如果没有,您可以使用 Tween 框架,例如 TweenLite or TweenMax(推荐):

TweenLite.to(mc, 1.5, {onComplete:myFunction});
function myFunction():void {
    trace("tween finished");
}

myFunction 在 1.5 秒后 运行