【发布时间】:2013-05-07 14:52:53
【问题描述】:
我构建了一个基本的回避游戏。当我在 iOS 中运行游戏时,我遇到了 setInterval 或 setTimeout 的问题(我都试过了)。奇怪的是,它作为 .swf 或 .apk 可以正常工作,但在 iOS 中却不行。我发现问题与setTimeout/setInterval有关。
我有一个文档类,它控制显示的内容,例如教程、1 级屏幕、升级屏幕等。它是这样的:
public function DocumentClass()
{
menuScreen = new MenuScreen();
menuScreen.addEventListener(NavigationEvent.START, onRequestStart);
menuScreen.addEventListener(NavigationEvent.CREDI, onRequestCredits);
addChild(menuScreen);
}
public function onRequestStart(navigationEvent:NavigationEvent):void
{
tut = new Tutorial();
tut.addEventListener(NavigationEvent.NEXT, onRequestNext);
addChild(tut);
menuScreen.removeEventListener(NavigationEvent.START, onRequestStart);
menuScreen.removeEventListener(NavigationEvent.CREDI, onRequestCredits);
removeChild(menuScreen);
menuScreen = null;
}
总是这样,移除最后一个屏幕和侦听器,然后弹出新屏幕,用于不同的级别或信息屏幕。
在我发现错误的课程中,我正在使用计时器,主要用于交互目的和生成敌人。 像这样的:
public function onTick( timerEvent:TimerEvent ):void
{
if ( Math.random() < 0.02 ){
enemySpot();
}
}
public function enemySpot()
{
var enemySpot = new EnemySpot(posX[incDec], posY[incDec])
spots.push(enemySpot);
addChild(enemySpot);
enemyBorn = setTimeout(enemyGenerator, 1000);
}
public function enemyGenerator()
{
enemy = new Enemy(posX[incDec], posY[incDec])
army.push(enemy);
addChild(enemy);
removeEnemy = setTimeout(enemyRemoval, 6000);
incDec = incDec + 1;
clearTimeout(enemyBorn);
}
public function enemyRemoval()
{
for each (var elem:Bola in army)
{
removeChild(elem);
elem = null;
army.reverse();
army.pop();
army.reverse();
clearTimeout(removeEnemy);
break
}
}
在玩家击中敌人之前它可以正常工作。
if (avatarHasBeenHit)
{
avatar.alpha = 0;
avatar.stopDrag();
if (bol2)
{
aDeath = new avatarDeath();//just an animation, of the avatar dyeing
addChild(aDeath);
aDeath.x = avatar.x;
aDeath.y = avatar.y;
uniTimer1 = getTimer();
bol2 = false;
}
goNext = setInterval(leaveScreen,1500);
}
public function leaveScreen()
{
gameTimer.stop();
dispatchEvent(new AvatarEvent(AvatarEvent.DEAD));
clearInterval(goNext);
}
一旦玩家击中敌人,游戏将停止在下一个级别生成敌人,这些敌人位于不同的类文件中。它是这样的: Avatar 命中敌人 -> 调度 AvatarEvent.DEAD; DocumentClass 移除了 Level Screen 子级,并使用下一个 Level Screen 部署子级,该子级具有与前一个级别相同的结构,不知何故 setTimeout 赢得了运行,因此没有新的敌人。但它适用于 swf 和 apk 文件!知道是什么导致了问题吗? 提前致谢
【问题讨论】:
-
我从未在 iOS 上使用过 setTimeout,所以我无法确认这是软件的错误还是限制,但如果你真的被卡住了,你可以改用
Timer,我有之前在 iOS 上用过 Timer 类。 -
我用过 setTimeout 它工作正常,简单但有效
-
可能混合 Timer 和 setTimeout/setInterval 会导致 iOS 中的某种错误。我可以真正得出结论,因为这是我第一次尝试 iOS。无论如何,我只使用 Timer + getTimer() 解决了这个问题,避免了任何类型的间隔/超时。感谢您的提示
标签: ios actionscript-3 flash settimeout setinterval