我一直在使用 HaxeFlixel 和 OpenFL 2.1.3 编写的针对 Android 的游戏遇到问题。游戏随机崩溃,有时在开始后不久,有时在几分钟后,并立即关闭应用程序,没有任何消息或明显的错误。无论出于何种原因,我在调试附加了 IDE 的应用程序时遇到了困难,因此我在设备上运行它时附加了 logcat,并将其范围缩小到以下跟踪:
I/trace ( 8505): Lib.hx:324: Null Object Reference
I/trace ( 8505): Called from openfl._v2.display.Stage.__render (openfl/_v2/display/Stage.hx line 1035)
I/trace ( 8505): Called from openfl._v2.display.DisplayObjectContainer.__broadcast (openfl/_v2/display/DisplayObjectContainer.hx line 280)
I/trace ( 8505): Called from openfl._v2.display.DisplayObject.__broadcast (openfl/_v2/display/DisplayObject.hx line 174)
I/trace ( 8505): Called from openfl._v2.display.DisplayObject.__dispatchEvent (openfl/_v2/display/DisplayObject.hx line 195)
I/trace ( 8505): Called from openfl._v2.events.EventDispatcher.dispatchEvent (openfl/_v2/events/EventDispatcher.hx line 100)
I/trace ( 8505): Called from openfl._v2.events.Listener.dispatchEvent (openfl/_v2/events/EventDispatcher.hx line 270)
I/trace ( 8505): Called from flixel.FlxGame.onEnterFrame (flixel/FlxGame.hx line 493)
I/trace ( 8505): Called from flixel.FlxGame.step (flixel/FlxGame.hx line 648)
I/trace ( 8505): Called from flixel.FlxGame.update (flixel/FlxGame.hx line 696)
I/trace ( 8505): Called from flixel.system.frontEnds.SoundFrontEnd.update (flixel/system/frontEnds/SoundFrontEnd.hx line 278)
I/trace ( 8505): Called from flixel.group.FlxTypedGroup.update (flixel/grou
I/WindowState( 467): WIN DEATH: Window{2b71e64d u0 com.al.shb/com.al.shb.MainActivity}
W/WindowManager( 467): Force-removing child win Window{d7de13 u0 SurfaceView} from container Window{2b71e64d u0 com.al.shb/com.al.shb.MainActivity}
W/WindowManager( 467): Failed looking up window
W/WindowManager( 467): java.lang.IllegalArgumentException: Requested window android.os.BinderProxy@2038b502 does not exist
W/WindowManager( 467): at com.android.server.wm.WindowManagerService.windowForClientLocked(WindowManagerService.java:8539)
W/WindowManager( 467): at com.android.server.wm.WindowManagerService.windowForClientLocked(WindowManagerService.java:8530)
W/WindowManager( 467): at com.android.server.wm.WindowState$DeathRecipient.binderDied(WindowState.java:1142)
W/WindowManager( 467): at android.os.BinderProxy.sendDeathNotice(Binder.java:551)
I/WindowState( 467): WIN DEATH: null
I/InputDispatcher( 467): Dropping event because there is no touchable window at (87, 430).
I/Zygote ( 130): Process 8505 exited cleanly (1)
I/ActivityManager( 467): Process com.al.shb (pid 8505) has died
W/ActivityManager( 467): Force removing ActivityRecord{3f0e8887 u0 com.al.shb/.MainActivity t13}: app died, no saved state
SoundFrontEnd
/FlxTypedGroup
表明这是游戏内音效组的问题。声音似乎可以正常播放并编码为 .ogg 文件。当它特别尝试播放任何声音时,它并没有崩溃,它更像是随机发生的。
我按照 HaxeFlixel 的建议缓存了 Android 目标的声音,但我将它们缓存init()
在我的 Main 类中,这应该不是问题:
private function setupGame():Void
{
var stageWidth:Int = Lib.current.stage.stageWidth;
var stageHeight:Int = Lib.current.stage.stageHeight;
if (zoom == -1)
{
var ratioX:Float = stageWidth / gameWidth;
var ratioY:Float = stageHeight / gameHeight;
zoom = Math.min(ratioX, ratioY);
gameWidth = Math.ceil(stageWidth / zoom);
gameHeight = Math.ceil(stageHeight / zoom);
}
#if android
FlxG.sound.cache("button");
FlxG.sound.cache("t1");
FlxG.sound.cache("t2");
FlxG.sound.cache("fx");
#end
addChild(new FlxGame(gameWidth, gameHeight, initialState, zoom, framerate, framerate, skipSplash, startFullscreen));
}
它们使用FlxG.sound.play("button")
.
我还查看了 OpenFLLib.hx
以检查Null Object Reference
,这似乎是以下方法中底部代码块的问题,但我怀疑这是一个红鲱鱼:
public static function rethrow (error:Dynamic):Void {
var event = new UncaughtErrorEvent (UncaughtErrorEvent.UNCAUGHT_ERROR, true, true, error);
if (__uncaughtExceptionHandler != null && __uncaughtExceptionHandler (event)) {
return;
}
Lib.current.loaderInfo.uncaughtErrorEvents.dispatchEvent (event);
if (!event.__getIsCancelled ()) {
var message = "";
if (error != null && error != "") {
message = error + "";
}
var stack = CallStack.exceptionStack ();
if (stack.length > 0) {
message += CallStack.toString (stack) + "\n";
} else {
message += "\n";
}
#if (mobile && !ios)
trace (message);
#else
Sys.stderr ().write (Bytes.ofString (message));
#end
Sys.exit (1);
}
}
值得注意的是,如果我以 Flash 或 Windows 为目标,则完全没有问题,只有在以 Android 为目标时。不同之处在于 Flash 和 Windows 使用mp3
编码文件,而 Android 使用ogg
.