为什么我会收到 InvocableTargetException 异常?安卓2D游戏

发布于 2024-12-28 02:11:21 字数 2847 浏览 1 评论 0原文

我正在用 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())) {

我收到一个 InvocableTargetException。这是 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);
}

I am making a 2D game in Android with Cocos2D, written in Java. Here is my code for the main stuff:

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;
}

As you see, this function looks just fine, and the rectangles in canExecuteMovement are perfectly fine too, however in this line:

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

I am getting a InvocationTargetException. Here is the 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)

What could be the problem? the getBounds() class in guy is this:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沫离伤花 2025-01-04 02:11:21

InitationTargetException 只是一个包装器对于动态调用中引发的异常。真正的问题是 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] 处的元素为 null。

InvocationTargetException is just a wrapper for an exception that's thrown within a dynamic invocation. The true problem is the NullPointerException that it's wrapping:

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

As you've pointed out, this is the offending line:

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

The only place a null pointer could be happening on this line is at platform[i].getBounds(). Either platform itself is null, or the element at platform[i] is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文