actionscript 3 - 从 eventEnterFrame 函数 AS3 调用循环



我无法弄清楚如何从eventEnterFrame函数中完全进行循环。 它在一帧中完成整个循环。我试图让它只调用类函数并让它通过它的过程运行。 我的代码正在尝试从eventEnterFrame调用一个函数,然后该函数将调用其他函数并执行其任务。

任务是创建一个随机的 Y 值,在那里放置一个 movieClip,然后实现一个重力函数,以便 movieClip 下降。 eventEnterFrame只是通过 If 循环调用 create movieClip 函数,因此它会创建多个,并且它们都落在不同的 Y 位置。

我只想清理我的eventEnterFrame函数并将代码移出 Main。 只在主区做并不难,但我不希望它在主区做。 任何帮助将不胜感激。

private function eventEnterFrame(e:Event):void{
    if(i<10){
        i++;
    } else if(i>=10){
        spikeA.name = "spike_"+j;
        addChild(spikeA);
        j++;
        i=0;
    }
    spikeA.y+=5;
    if(spikeA.y>600){
        spikeA.y=100;
    }
}

这就是我让它在主目录中生成一个"尖峰"的方式

第二个问题是控制每个创建的"spikeA_"+j并给每个下降的类命令,现在它只创建一个spikeA并使其向下移动。

谢谢

尖峰代码,大多数已经从我身上取出,尝试了很多方法来让它工作,所以它只是放置它,因为我感到沮丧并做了一个干净的石板

package  {
    import flash.events.Event;
    import flash.display.MovieClip;
    import flash.display.Stage
    import gravity;
    public class spike extends MovieClip {

        var random1:Number;

        public function spike() {
            random1 = Math.floor(Math.random()*700)+1;

            this.x = random1;
            this.y = 50;
            if(this.y>600){
                this.y=200;
            }

        }
    }
}

首先,您需要以某种方式实例化一个新项目才能生成它。 因此,您需要在某个地方使用new关键字。 如果尖峰是库中的某个项目,并且选中了 actionscript 的导出,并且其属性和类名为 Spike(例如),则可能需要执行以下操作:

//create a container for all your spikes
private var spikeContainer:Sprite;
//create timer that ticks every 2 seconds
private var spawnTimer:Timer = new Timer(2000); 
//Then in a start game type function do the following:
public function startGame():void {
    //create and add the container to the screen
    spikeContainer = new Sprite();
    addChild(spikeContainer);
    //listen for the tick event on the timer
    spawnTimer.addEventListener(TimerEvent.TIMER, spawnSpike);
    spawnTimer.start(); //start the timer
    //listen for the enter frame event
    this.addEventListener(Event.ENTER_FRAME, enterFrame);
}
function stopGame():void {
    removeChild(spikeContainer);
    spawnTimer.stop();
    this.removeEventListener(Event.ENTER_FRAME, enterFrame);
}
private function spawnSpike(e:Event):void {
    var mySpike:spike = new spike(); //create a new spike
    spikeContainer.addChild(mySpike); //add it to the container
    mySpike.y = Math.random() * (stage.stageHeight * 0.5); //put randomly on the top half of the screen
}
private function enterFrame(e:Event):void {
    //iterate over all children of the spike container (we iterate backwards so that if you remove an item, it doesn't throw off the index)
    var i:int = spikeContainer.numChildren;
    while(i--){
        //move the spike down 5 pixels
        spikeContainer.getChildAt(i).y += 5; 
        //check to see if the spike is off stage, if it is, remove it
        if(spikeContainer.getChildAt(i).y > stage.stageHeight){
            spikeContainer.removeChild(i);
        }
    }
}

如果你想提高效率,你可以回收你的尖峰,而不是产生新的尖峰并移除它们。

在开始时创建所有尖峰(或者如果使用FlashPro,您可以将它们全部放在容器影片剪辑内的时间轴上)

从上面的代码中取出计时器和生成函数。

然后,无需删除 enterframe 处理程序中的尖峰,只需将其 y 位置重置为您想要的任何位置即可。

    //check to see if the spike is off stage, if it is, remove it
    if(spikeContainer.getChildAt(i).y > stage.stageHeight){
        spikeContainer.getChildAt(i).y = 100; //reset the y position
    }

最新更新