2015-06-19 56 views
0

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

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

我只是想清理我的eventEnterFrame函数并将代码移出Main。在Main中做这件事并不难,但我不希望它在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; 
      } 


     } 

    } 
} 
+0

你如何创建一个秒杀?你可能不想在每一帧产生一个敌人......如果spawing是基于时间的话,可能要使用一个计时器。如果您将所有尖峰存储在容器精灵中,或将数组存储为跟踪它们,然后使用回车框更新每个位置。 – BadFeelingAboutThis

回答

1

首先,你需要以某种方式实例化一个新项目以产生它。因此,您需要使用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处理程序中的尖峰,只需将它的位置重置为任何你想要的。

//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 
    } 
+0

private var spikeA:MovieClip = new spike();这是在我的主要。这个钉是它自己在Main以外的类,我猜是一个子类。这个秒杀是一个动画片段,它能不能成为一个精灵? –

+0

'MovieClip'扩展了'Sprite',所以如果'spike'扩展了MovieClip,它也是一个Sprite。它不是主要的子类(这将意味着你的秒级将如下所示:'public class spike extends Main')我认为你的意思是它是'Main'的孩子? – BadFeelingAboutThis

+0

所以秒杀是它自己的主课外如果是有道理的 包{ \t进口对象类型:flash.events.Event; \t import flash.display.MovieClip; \t import flash.display.Stage \t import gravity; \t \t公共类秒杀扩展影片剪辑{ \t \t \t \t \t \t VAR random1:号码; \t \t \t \t \t 公共\t功能尖峰(){ \t \t \t random1 = Math.floor(的Math.random()* 700)1; \t \t \t \t \t \t \t \t \t this.x = random1; \t \t \t this.y = 50; \t \t \t \t \t \t如果(this.y> 600){ \t \t \t \t this.y = 200; \t \t \t} \t \t \t \t \t \t} \t }} 编辑 –