[英]2D Game not repainting
I checked the other questions and couldn't find one with the same case as me so here's my question. 我检查了其他问题,但找不到与我相同的案例,所以这是我的问题。
I am making a 2 player stick fighting game that you can play on the same computer using different keys. 我正在制作一个2人棍棒格斗游戏,您可以使用不同的键在同一台计算机上玩。 Everything's fine but when I try to move the oval on the screen with the keys, it's not moving.
一切都很好,但是当我尝试使用键在屏幕上移动椭圆形时,它没有移动。
Here's the code for my first class - http://pastebin.com/wA0JXdzr second class - http://pastebin.com/ArByyirt 这是我第一堂课的代码-http://pastebin.com/wA0JXdzr第二堂课-http://pastebin.com/ArByyirt
I think I need to call repaint in my second class in the gameloop, but it's saying that it can't make a static reference to it. 我想我需要在游戏循环的第二个类中调用repaint,但这是说它不能对其进行静态引用。
You are trying to call a non static method directly from another class which is not legal in java. 您试图直接从另一个在Java中不合法的类中调用非静态方法。 The paint() method in your first class is the non-static method.
第一个类中的paint()方法是非静态方法。 You were able to use the variables stickx2 and such because they are static as defined in your first class.
您可以使用stickx2之类的变量,因为它们是静态的,如您在第一堂课中所定义。
Thus, I suggest you create an object of stickFrame() in the gameLoop class and copy all your code in your stickframe main method and put it in your gameLoop main method. 因此,建议您在gameLoop类中创建一个stickFrame()对象,并将所有代码复制到stickframe main方法中,然后将其放入gameLoop main方法中。 It is highly not recommended that you have two main methods.
强烈建议您不要使用两种主要方法。
Declare a Stick Frame variable below your Serialization ID. 在序列化ID下声明一个Stick Frame变量。
StickFrame s;
Then instantiate it in your gameLoop constructor 然后在您的gameLoop构造函数中实例化它
s = new StickFrame();
Now we need to fix the repaint from another class problem. 现在,我们需要修复另一个类问题的重绘。 To do this we need a method in the gameLoop Class.
为此,我们需要在gameLoop类中使用一个方法。
public void repaintStickFrame()
{
s.repaint();
}
Then call it by 然后打电话给
s.repaintStickFrame() in your loop.
or you can just call 或者你可以打电话
s.repaint();//place in loop
Heres a link to a question that is similar to yours and has solutions as well 这是指向与您的问题相似且也有解决方案的问题的链接
Calling repaint from another class JFrame 从另一个类JFrame调用重画
Heres a link that explains how you can call an objects method once you've created one (Like we did above, which allowed us to call the repaint() method from a different class): 这里的链接说明了创建对象后如何调用对象方法(就像我们上面所做的那样,它允许我们从其他类中调用repaint()方法):
https://docs.oracle.com/javase/tutorial/java/javaOO/usingobject.html https://docs.oracle.com/javase/tutorial/java/javaOO/usingobject.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.