I would like to address this as a non-duplicate because I could not find any answers on google. There are similar questions but the answers are not what I wished for.
Below is what I believe is important for the animations
here is whats inside my player class:
...
...
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();
}
here in my mouse event class:
...
...
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();
}
}
So my logic is that the player is able to select 2 skins, which have their own unique animations.
I set up 2 animations and the first one(ani) is the default. Once the player selects the second skin, the animation images will change. However, it gave me an error after I select my 2nd skin. (the game is able to keep going but the with the skin being default)
Here is the error:
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)
My possible guess on the cause of this issue is that you can not switch images in an animation?
If anyone knows the answer on the fix please answer down below. Thank you!
If you are unsure what I am asking, feel free to ask in the comments.