简体   繁体   English

用于2D滚动游戏的Java nullpointerexception

[英]Java nullpointerexception for a 2D scrolling game

I'm making a game, its 2D side scrolling, kinda like terraria. 我正在制作一款游戏,它的2D侧滚动,有点像terraria。 In my game, i have offsets, XOff and YOff , and those move the terrain. 在我的游戏中,我有偏移, XOffYOff ,那些移动地形。 I also have a method for my character, move() , and all that does is this: 我也有一个方法为我的角色, move() ,所有这一切是这样的:

public void move() {
        if (!ableToMove) //makes sure you can move
            return;
        // x += moveX; these 2 methods work, but x and y are just my player coords.
        // y += moveY;
        core.setYOff(1); //core is my main class, where the global variables are stored
    }

the comments label and describe each variable. 注释标签并描述每个变量。 so, setYOff(1) sets the YOff like so: 所以,setYOff(1)将YOff设置为:

private void setYOff(int i){
     YOff += i;
}

Now, there is only one way that i am accessing the move method. 现在,我只有一种方法可以访问move方法。 in a thread, called PlayerThread , i have a constant call to the move() and another method called applyGravity() , which doesnt really have a relevance to this question. 在一个名为PlayerThread的线程中,我对move()和另一个名为applyGravity()方法有一个常量调用,它与这个问题没有关系。 That is in the PlayerThread . 那是在PlayerThread

Now here is the problem i'm having... When I call the setYOff() , I'm getting the following error: 现在这里是我遇到的问题...当我调用setYOff() ,我收到以下错误:

Exception in thread "player Thread" java.lang.NullPointerException
    at main.Player.move(Player.java:33)
    at main.PlayerThread.run(PlayerThread.java:24)
    at java.lang.Thread.run(Unknown Source)

which is telling me that when i'm calling the setYOff() method in my move() method, it doesnt work. 这是告诉我,当我打电话setYOff()在我的方法move()方法,它不工作。 I've tried just testing and moving the call to set the Y offset into the thread itself, and it gives the same error... I then tried to move it to the RefreshThread , and interestingly it did work... it moved the entire map down 1 pixel every 20 milliseconds... and i have no clue why! 我已经尝试过测试并移动调用以将Y偏移设置为线程本身,并且它给出了相同的错误...然后我尝试将其移动到RefreshThread ,并且有趣的是它确实有效...它移动了整个地图每20毫秒下降1个像素......我不知道为什么! what is wrong with my move method that makes this not work, or better yet, my PlayerThread , that isnt making this work in the long run? 我的移动方法有什么问题,这使得这不起作用,或者更好的是,我的PlayerThread ,从长远来看,这不是很有效吗? What is different about my RefreshThread ? 我的RefreshThread什么不同? If you guys need the entire code for everything involving the calls, creation, and such of each thread, i can provide that, but i'm not sure you would need that... What do i need to do differently? 如果你们需要涉及调用,创建等每个线程的所有代码的整个代码,我可以提供,但我不确定你是否需要...我需要做什么不同的事情? Thanks in advance! 提前致谢!

EDIT: I tried, in my PlayerThread just commenting out the move() call, and just having it adjust the y offset, and it worked! 编辑:我试过,在我的PlayerThread只是注释掉move()调用,只是让它调整y偏移量,它就有效了! so it has to do with the move method, but again, why? 所以它与移动方法有关,但同样,为什么呢?

CORRECTION TO PREVIOUS EDIT: so testing more, i put the move() call back into the thread, and kept the setYOff(1) in it, and it still worked, but again not from within the move method itself... 修改以前的编辑:所以测试更多,我把move()调用回到线程中,并将setYOff(1)保留在其中,它仍然有效,但同样不是来自移动方法本身...

The NullPointerException indicates that the object on which the method is being called is Null , in this case, the core object. NullPointerException指示调用该方法的对象是Null ,在本例中是core对象。 Fix that and the problem should go away. 解决这个问题,问题应该消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM