我的问题是,我想每秒添加一块石头(每秒30帧),我有不同的等级,这意味着我在每个等级都有不同数量的岩石,我有不同的速度,所以我想要在1级总共30秒内添加10个岩石 在2级它是20个岩石,总共20秒等。我愿意完全改变它,我只想要最好的解决方案。我希望它是动态的,所以我可以创造很多关卡。我应该怎么做呢
我不想保留一个计数器,每次都在30,然后添加一个摇滚并重置它。
提前谢谢
switch(difficulty)
{
case 1:
timer = 30;
numberOfRocks = 10;
break;
case 2:
timer = 20;
numberOfRocks = 20;
break;
case 3:
timer = 10;
numberOfRocks = 30;
break;
case 4:
timer = 5;
numberOfRocks = 40;
break;
}
addEventListener(Event.ENTER_FRAME, loop)
}
private function loop(e:Event):void
{
for (var i:int = 0; i < (timer * 30); i++)
{
a_bitmap = new a_class();
a_bitmap.x = 750;
a_bitmap.y = Math.ceil(Math.random() * (600 - a_bitmap.height));
a_bitmap.height = 35;
a_bitmap.width = 35;
addChild(a_bitmap);
a_bitmap.name = "astroid" + i + "";
myArray.push(true);
}
}
答案 0 :(得分:1)
定时器可能比帧处理程序更适合您的需求。我建议使用数学计算您的关卡参数而不是硬编码的开关语句,然后您可以添加任意数量的关卡(或让您的游戏无限期地进行)
var rockLoadTimer:Timer;
function gameInit():void {
//your games initialization code here
//create a timer that fires every second when running
rockLoadTimer = new Timer(1000);
//the function to call every time the timer fires. You could also listen for TIMER_COMPLETE is wanted to run a function once all rocks are loaded
rockLoadTimer.addEventListener(TimerEvent.TIMER, addNextRock);
}
function startLevel(difficulty:int = 1):void {
//your code here to clear the level
//repeat count is used as the amount of rocks to load this level - level number times 10
rockLoadTimer.repeatCount = difficulty * 10;
//the delay time is how quickly you want the rocks to load. this is 5 seconds divided by the difficulty level
//so the first level would be every 5 seconds, second would be every 2 and a half seconds, third level would be every second and two thirds. etc.
rockLoadTimer.delay = Math.round(5000 / difficulty);
rockLoadTimer.reset();
rockLoadTimer.start();
}
function addNextRock(e:Event = null):void {
//your rock creation code here
}
答案 1 :(得分:1)
您可以使用Timer
,并在每次打勾期间创建a_class
的实例:
var timer:Timer = new Timer(1000, 30); // One per second for 30 seconds
timer.addEventListener(TimerEvent.TIMER, addAsteroid);
timer.start();
function addAsteroid():void {
a_bitmap = new a_class();
// etc.
}
计时器延迟是“每毫秒的小行星”,因此如果要创建超过30秒的10,则将其设置为30000/10
= 3000。
然而,这种方法在平滑帧率时效果最好 - 它每秒执行一次,但如果Flash以低于30fps的速度运行,动画帧数可能会有所不同。如果你的游戏按照我的想法运作,这可能会导致小行星被“聚集”。所以,保持计数器可能是更好的解决方案,除非您计划以可以解决帧速率变化的方式处理剩余的游戏逻辑(即小行星移动速度)。
如果您想使用计数器:
var creationCounter:Number = 0; // put this at class level
// Then in the ENTER_FRAME event:
creationCounter += asteroids_per_second / 30;
while (creationCounter-- >= 1) {
a_bitmap = new a_class();
// etc.
}
答案 2 :(得分:1)
您也可以使用flash.utils.setInterval函数。
setInterval(trace , 1000 , "trace message once per second");
答案 3 :(得分:0)
如果您使用TweenLite
,则可以使用delayedCall
功能,它可以使用时间或帧,
// delayedCall(delay:Number, onComplete:Function, onCompleteParams:Array = null, useFrames:Boolean = false):TweenMax
TweenLite.delayedCall(30, addAstroid, null, true);
更多信息,请参阅文档:http://www.greensock.com/as/docs/tween/com/greensock/TweenMax.html#delayedCall()