Informatics, we were asked to make a game and I very often meet with such a mistake.

  • Monstro (Monster)
  • BadGuy (Bad Guy) He Extends Monster Class
  • Hero

Monster Class:

package pt.iul.ista.poo.example; import java.util.List; import pt.iul.ista.poo.gui.ImageTile; import pt.iul.ista.poo.rogue.utils.Position; import pt.iul.ista.poo.rogue.utils.Vector2D; public class Monstro extends GameObjects { public Monstro (Position position, String name) { super(position, name); } public void move (Hero hero) { double r = Math.random(); System.out.println(r); int Hx = hero.getPosition().getX(); int Hy = hero.getPosition().getY(); Position M = super.getPosition(); int Mx = super.getPosition().getX(); int My = super.getPosition().getY(); int Dx = Hx - Mx; int Dy = Hy - My; if(Dx <= 2 && Dy <= 2 || Dx >= -2 && Dy >= -2){ } else { if (0 < r && r <= 0.25) { //cima (0,-1) M = new Position(Mx,My-1); } if (0.25 < r && r <= 0.50) { //esquerda (-1,0) M = new Position(Mx-1,My); } if (0.50 < r && r<= 0.75) { //baixo (0,1) M = new Position(Mx,My+1); } if (0.75 < r && r <= 1) { //direito (1,0) M = new Position(Mx+1,My); } } Position positionNext = M; int x = positionNext.getX(); int y = positionNext.getY(); if (x < 0 || y < 0 || x > 9 || y > 9) return; List<ImageTile> tiles = Engine.getInstance().getSelectedRoom().getTiles(); for (ImageTile a: tiles) { if(a.getName().equals("Wall") && positionNext.equals(a.getPosition())) { System.out.println(positionNext); } else { super.setPosition(positionNext); } } } 

}

Hero Class:

  package pt.iul.ista.poo.example; import java.util.List; import pt.iul.ista.poo.gui.ImageTile; import pt.iul.ista.poo.rogue.utils.Position; import pt.iul.ista.poo.rogue.utils.Vector2D; public class Hero extends GameObjects{ private Room room; public Hero(Position position) { super (position, "Hero"); } public void move (Vector2D s) { Position z = super.getPosition(); Position positionNext = super.getPosition().plus(s); int x = positionNext.getX(); int y = positionNext.getY(); if (x < 0 || y < 0 || x > 9 || y > 9) return; List<ImageTile> tiles = Engine.getInstance().getSelectedRoom().getTiles(); for (ImageTile a: tiles) { if(a.getName().equals("Wall") && positionNext.equals(a.getPosition())) positionNext = z; // System.out.println(positionNext); else { super.setPosition(positionNext); } } } public void setRoom(Room room) { this.room = room; } 

}

Bad Boy Class:

 package pt.iul.ista.poo.example; import pt.iul.ista.poo.rogue.utils.Position; public class BadGuy extends Monstro { public BadGuy(Position position) { super (position, "BadGuy"); } public void movement(){ BadGuy.move(null); // Здесь пишет проблему которая находить в заголовке } } 

The code is made so that the hero and the bad guy move. Since I will have a lot of monsters (and the bad guy is one of them) I decided to make a class in which there will be a code for the monster to move and resort to it when needed.

Any criticism of the code is welcome.

  • What kind of mistake is that? Throw off the stack of errors (text of errors. You can copy it to LogCat - write in Yandex "logcat how to use") - gc986

1 answer 1

BadGuy.move(null); - with a similar entry, you are trying to call the static class method BagGuy , although this does not exist in your class or superclass, but there is a non-static in the ancestor.

If you still want to refer to the method, then create an instance of the BadGuy class in order to call the move() method relative to it.

 BadGay badGay = new BadGay(...); badGay.move(...); 

PS With a similar record, you are about to attack a NullPointerException

 .move(null);