我无法弄清楚如何从eventEnterFrame
函数完全循环进度。它在一帧中完成整个循环。我试图让它只是调用类函数,让它运行通过它的过程。我的代码试图从eventEnterFrame
调用一个函数,然后该函数将调用其他函数并执行其任务。
任务是创建一个随机的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;
}
}
这就是我如何产生一个&#34; spike&#34;在主要
第二个问题是控制每个创建的&#34; spikeA _&#34; + 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 :(得分:1)
首先,您需要以某种方式实例化一个新项目以产生它。因此,您需要使用new
关键字。如果你的 spike 是你库中的一个项目,其中 export for 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
}