首页 > 解决方案 > ACTIVATE/DEACTIVATE 事件不适用于使用 Starling 框架的移动游戏应用

问题描述

所以我正在使用starling框架开发一个手机游戏,我希望游戏在我点击手机上的主页/返回按钮时暂停,当然在回到游戏时恢复。我做了一些研究,并尝试了以下方法:

this.addEventListener(FlashEvent.DEACTIVATE, stopGame);
this.addEventListener(FlashEvent.ACTIVATE, continueGame);

private function continueGame(event:FlashEvent):void 
{
    ...
}
private function stopGame(event:FlashEvent):void 
{
    ...
}

我不得不添加一个名为 FlashEvent 的新类,它扩展了 flash.events.Event,因为我在同一个类中使用了 starling Event 和 flash Event,而当我使用 flash.events.Event 时,我得到了这个错误:

Error: Access of undefined property flash

starling.events.Event 也是如此。

所以我使用了上面的代码并在我的手机上进行了尝试,但是当我返回/home 时,游戏继续进行,音乐继续播放。

我的问题是:在空中移动应用程序中调度激活/停用事件的正确方法是什么?

标签: airgame-developmentstarling-framework

解决方案


在您的主要启动课程中使用。(请注意,在本例中,'app:Main' 是我调用 Starling 启动方法的类。

请注意,您应该使用以下内容确定事件类:starling.events.Event.XXX flash.events.Event.XXX

_mStarling.addEventListener(starling.events.Event.ROOT_CREATED, 
function onRootCreated(event:Object, app:Main):void
{

_mStarling.removeEventListener(starling.events.Event.ROOT_CREATED, onRootCreated);

app.start(assets);

_mStarling.start();

NativeApplication.nativeApplication.addEventListener(
flash.events.Event.ACTIVATE, function (e:*):void { 

_mStarling.start(); 

try {
//  optionally call some other methods
} catch(e:Error) {

}
});

NativeApplication.nativeApplication.addEventListener(
flash.events.Event.DEACTIVATE, function (e:*):void { 

try{
//  optionally call some other methods before stopping
} catch(e:Error) {

}
_mStarling.stop(); 
}); 

});

推荐阅读