首页 > 解决方案 > 无法使用 Java 中的动画功能更改动画图像

问题描述

我想将此作为非重复项来解决,因为我在谷歌上找不到任何答案。有类似的问题,但答案不是我想要的。

以下是我认为对动画很重要的内容

这是我的播放器类中的内容:

...
...
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)

我对这个问题的原因可能的猜测是你不能在动画中切换图像?

如果有人知道修复的答案,请在下面回答。谢谢!

如果您不确定我在问什么,请随时在评论中提问。

标签: javaanimationnullpointerexception

解决方案


推荐阅读