gpt4 book ai didi

java - 为什么我会收到 InvocationTargetException?安卓2D游戏

转载 作者:IT老高 更新时间:2023-10-28 23:18:14 24 4
gpt4 key购买 nike

我正在使用 Java 编写的 Cocos2D 在 Android 中制作 2D 游戏。这是我的主要内容代码:

public void gameLoop(float dt) {
//Player Gravity
if(canExecuteMovement(0, 6)) {
guy.moveY(6);
}

//Player Movement
if(direction == 1) {
if(canExecuteMovement(-3, 0))
guy.moveX(-3);
} else if(direction == 2) {
if(canExecuteMovement(3, 0))
guy.moveX(3);
}
}

private boolean canExecuteMovement(int xChange, int yChange) {
int projectedX = guy.getBounds().left + xChange;
int projectedY = guy.getBounds().top + yChange;
Log.i("DD", "guy:" + guy.getBounds().toString());
Rect projectedBounds = new Rect(projectedX, projectedY, projectedX + guy.getWidth(), projectedY + guy.getHeight());
Log.i("DD", "guy:" + projectedBounds.toString());
for (int i = 0; i < platformCount; i++) {
if (Rect.intersects(projectedBounds, platform[i].getBounds())) {
return false;
}
}

return true;
}

如你所见,这个函数看起来很好,canExecuteMovement 中的矩形也很好,但是在这一行中:

LINE 107: if (Rect.intersects(projectedBounds, platform[i].getBounds())) {

我收到 InvocationTargetException。这是logcat:

01-21 23:10:12.601: W/System.err(13118): java.lang.reflect.InvocationTargetException
01-21 23:10:12.601: W/System.err(13118): at java.lang.reflect.Method.invokeNative(Native Method)
01-21 23:10:12.605: W/System.err(13118): at java.lang.reflect.Method.invoke(Method.java:511)
01-21 23:10:12.605: W/System.err(13118): at org.cocos2d.actions.CCTimer.update(CCTimer.java:82)
01-21 23:10:12.605: W/System.err(13118): at org.cocos2d.actions.CCScheduler.tick(CCScheduler.java:253)
01-21 23:10:12.605: W/System.err(13118): at org.cocos2d.nodes.CCDirector.drawCCScene(CCDirector.java:679)
01-21 23:10:12.605: W/System.err(13118): at org.cocos2d.nodes.CCDirector.onDrawFrame(CCDirector.java:649)
01-21 23:10:12.605: W/System.err(13118): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1462)
01-21 23:10:12.605: W/System.err(13118): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1216)
01-21 23:10:12.605: W/System.err(13118): Caused by: java.lang.NullPointerException
01-21 23:10:12.608: W/System.err(13118): at com.qasim.platformer.GameLayer.canExecuteMovement(GameLayer.java:107)
01-21 23:10:12.608: W/System.err(13118): at com.qasim.platformer.GameLayer.gameLoop(GameLayer.java:86)
01-21 23:10:12.608: W/System.err(13118): ... 8 more
01-21 23:10:12.620: D/dalvikvm(13118): GC_CONCURRENT freed 460K, 6% free 9279K/9863K, paused 2ms+3ms
01-21 23:10:12.624: I/DD(13118): guy:Rect(252, 63 - 300, 111)

可能是什么问题? guy 中的 getBounds() 类是这样的:

public Rect getBounds() {
return new Rect(x, y, x+width, y+height);
}

最佳答案

InvocationTargetException只是动态调用中抛出的异常的包装器。真正的问题是 NullPointerException它正在包装:

Caused by: java.lang.NullPointerException
at com.qasim.platformer.GameLayer.canExecuteMovement(GameLayer.java:107)
at com.qasim.platformer.GameLayer.gameLoop(GameLayer.java:86)

正如您所指出的,这是违规行:

if (Rect.intersects(projectedBounds, platform[i].getBounds())) {

空指针可能出现在这一行的唯一位置是 platform[i].getBounds()platform 本身是 null,或者 platform[i] 处的元素是。

关于java - 为什么我会收到 InvocationTargetException?安卓2D游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8958882/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com