我想将此解决为非重复,因为我在谷歌上找不到任何答案。有类似的问题,但答案不是我想要的。
以下是我认为对动画很重要的内容
这里是我的播放器类中的内容:
...
...
Animation ani;
Animation ani2;
public Player(double x, double y, Texture tex, StarDestroyerAlpha game, Controller c) {
super (x,y);
this.tex = tex;
this.game = game;
this.c = c;
ani = new Animation(3, game.getSpriteSheet(), game.getSpriteSheet1(), game.getSpriteSheet2(), game.getSpriteSheet3(), game.getSpriteSheet4(), game.getSpriteSheet5());
ani2 = new Animation(3, game.getSpriteSheet21(), game.getSpriteSheet22(), game.getSpriteSheet23(), game.getSpriteSheet24(), game.getSpriteSheet25(), game.getSpriteSheet26());
}
}
public void tick() { //movement of player
x += velX;
y += velY;
if (x<=0)
x = 0;
if (x>= 1280 - 70) //there will be space because of the sprite size
x = 1280 - 70;
if (y<=0)
y = 0;
if (y>= 800 - 85) {
y = 800 - 85;
}
ani.runAnimation();
}
这里是我的鼠标事件类:
...
...
Player p;
...
public MouseInput(StarDestroyerAlpha game, ShipSelection ship, Texture tex, Player p) {
this.game = game;
this.ship = ship;
this.tex = tex;
this.p = p;
}
...
...
...
if(my >300 && my < 500 && shipRotation == 1) {
game.setDestroyerLogo(game.getDestroyerLogo2());
game.setPilotImage(game.getPilotImage2());
game.Health = 300;
p.ani = null;
p.ani2.runAnimation();
}
}
所以我的逻辑是玩家可以选择2个皮肤,它们有自己独特的动画。
我设置了2个动画,第一个(ani)是默认设置。一旦玩家选择第二个皮肤,动画图像就会改变。但是,在我选择第二层皮肤后,它给了我一个错误。 (游戏能够继续运行,但皮肤是默认的)
这是错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at star.destroyer.alpha.MouseInput.mouseClicked(MouseInput.java:107)
at java.awt.Component.processMouseEvent(Component.java:6536)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
我可能猜到这个问题的原因是你无法在动画中切换图像?
如果有人知道修复的答案,请在下面回答。谢谢!
如果您不确定我在问什么,请随时在评论中提问。