LibGDX Array of a Superclass - java

In my Game World class I wish to have an array of all of my enemies that are using a super class called Fighters.
public Array<Fighters> enemies;
I initialize it in the constructor of my Game World.
enemies = new Array<Fighters>();
This is the Fighters Super class.
public class Fighters {
protected Vector3 position;
protected Vector3 velocity;
protected Texture texture;
public Vector3 getPos() {
return position;
}
public Vector3 getVelocity() {
return velocity;
}
public Texture getTexture() {
return texture;
}
public void setPos(Vector3 newPos) {
position = newPos;
}
}
And a class that is a subclass of Fighters.
public class RedFighter extends Fighters {
public static final int value = 1;
public RedFighter() {
position = new Vector3(-10, -10, 0);
velocity = new Vector3(0, -10, 0);
texture = new Texture("redfighter.png");
}
}
When I call a method called spawn enemies, it should be filling the array with redfighters. However, I keep getting a NullPointException when I try to add it.
Fighters fighter;
fighter = new RedFighter();
enemies.add(fighter);
What am I doing wrong and how do I fix it? Thank you!

I figured it out!
Turns out that I didn't really have a constructor for my game world. Instead of using public GameWorld() as my declaration, I was using public void GameWorld() and so it didn't know it was my constructor. I realize that I should have posted the whole class. Thanks so much everyone! I'll make sure to post everything relevant next time.
As for where Array comes from, it's an array from the libgdx library.

Related

Receiving java.lang.UnsatisfiedLinkError on badlogic.BufferUtils

When lunching the application with DesktopLuncher i experience UnsatisfiedLinkError on root class (Main) in core module inside the project.
public class Main extends Game {
#Override
public void create() {
this.cameraBuilder = ...
setScreen(new LandingMenu(this));
}
#Override
public void render() {
super.render();
}
#Override
public void dispose() {
batch.dispose();
}
public World getWorld() {
return world;
}
public SpriteBatch getBatch() {
return batch;
}
public CameraBuilder<OrthographicCamera> getCameraBuilder() {
return cameraBuilder;
}
private final SpriteBatch batch = new SpriteBatch();;
private CameraBuilder<OrthographicCamera> cameraBuilder;
private final World world = new World(new Vector2(0, -9.8f), true);
}
any answer will be appreciated.
The exception is a cause and issue lives on create method on ApplicationListener Called when the Application is first created, on the other hand SpriteBatch is a Batch which is used for drawing Sprites, more specifically, it deals with drawing a bunch of textures on Quads in OpenGL thus is must be initialized inside of create method and not in the root class level.
#Override
public void create() {
batch = new SpriteBatch();
setScreen(new LandingMenu(this));
}
...
private SpriteBatch batch;
Find more in here.

Java Swing: Is there a way to check for MouseOver for a drawn shape without checking every time the mouse moves?

I have a JPanel that I draw upon. The shapes I draw are some objects stored in a list. I would like to register MouseOvers over these drawn objects. What I am currently doing is adding a MouseMotionListener that checks the list of objects for a hit every time the mouse moves. This is of course pretty inefficient once there are a lot of objects. Is there a better way to check for MouseOvers than just checking all the objects every time the mouse moves?
Here is a minimal example:
class Ui.java:
public class Ui {
private static JFrame frame;
private static DrawPanel drawPanel;
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> { createGui();
});
}
private static void createGui() {
frame = new JFrame("Test");
drawPanel = new DrawPanel();
frame.setContentPane(drawPanel);
frame.pack();
frame.setVisible(true);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
}
}
class SomeObject.java:
public class SomeObject {
//class that represents some object that will be drawn. note that this is
//just a minmal example and that in my actual application, there are
//other aspects and functions to this class that have nothing to do with drawing.
//This is just kept small for the sake being a minimal example
private String id;
private Point2D origin;
private int length;
private int height;
public SomeObject(String id, Point2D origin, int length, int height) {
this.id = id;
this.origin = origin;
this.length = length;
this.height = height;
}
public String getId() {
return id;
}
public Point2D getOrigin() {
return origin;
}
public int getLength() {
return length;
}
public int getHeight() {
return height;
}
}
class CustomMouseMotionListener.java:
public class CustomMouseMotionListener implements java.awt.event.MouseMotionListener {
public CustomMouseMotionListener() { }
#Override
public void mouseDragged(MouseEvent e) {
}
#Override
public void mouseMoved(MouseEvent e) {
for (SomeObject object: DrawPanel.objectsToDraw) {
Shape s = new Rectangle((int)object.getOrigin().getX(), (int)object.getOrigin().getY(), object.getLength(), object.getHeight());
if (s.contains(e.getPoint())) {
System.out.println("hit: " + object.getId());
}
}
}
}
class DrawPanel.java:
public class DrawPanel extends JPanel {
public static List<SomeObject> objectsToDraw = new ArrayList<>();
public DrawPanel() {
objectsToDraw.add( new SomeObject("a", new Point2D.Float(20,1), 20,20));
objectsToDraw.add( new SomeObject("b", new Point2D.Float(20,45), 20,20));
addMouseMotionListener(new CustomMouseMotionListener());
setFocusable(true);
setVisible(true);
grabFocus();
}
protected void paintComponent(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
super.paintComponent(g2D);
for (SomeObject object: objectsToDraw) {
g.drawRect((int)object.getOrigin().getX(), (int)object.getOrigin().getY(), object.getLength(), object.getHeight());
}
}
}
Instead of using your SomeObject class, i recommend using Shape or Area. The whole purpose of SomeObject seems to be to be turned into a Shape anyway, right? Not only that, but with an ArrayList of Shapes, you can eliminate creating rectangles for your shapes in every mouseMove.
BTW, years ago I put together a package for treating Areas like 1st class Components. You can see this here: https://sourceforge.net/p/tus/code/HEAD/tree/tjacobs/ui/shape/
(Start with AreaManager and AreaModel). Area has some advantages and disadvantages: Advantages: Easy to manage as a group, fairly easy to test if they are overlapping. Disadvantage: You lose the information about how the Area was constructed (ex. Polygon points, circle radius, etc)
You've already taken this a long way, so kudos to you. This answer is (a) responding to your question about efficiency, but also pointing you in some ways you could choose to go
There is more to SomeObject than just being drawn, so I think I can't just replace it with Shape,
Then you keep a Rectangle instance as part of your SomeObject class, instead of your Point and length/height variables.
Then you modify your methods to return the values from the Rectangle.
This will prevent you from continually creating new Rectangle instances, make the process more memory efficient and reducing garbage collection.
But, you should also be able to extend the Rectangle class and add your extra functionality in the same way that you extend JPanel to add custom painting logic.

When adding a velocity vector to position vector, the velocity vector tends towards infinity

I am working on some code, using LibGdx. I have entity code that draws on update and an input adapter that adds to the velocity vector on key down. During debugging I've noticed that when adding the velocity vector to the position vector the velocity goes to infinity after a moment. The code can be found below:
InputAdapter:
#Override
public boolean keyDown(int keycode) {
switch (keycode) {
case Input.Keys.UP:
e.getVelocity().add(0,1);
break;
case Input.Keys.DOWN:
e.getVelocity().add(0,-1);
break;
case Input.Keys.LEFT:
e.getVelocity().add(-1,0);
break;
case Input.Keys.RIGHT:
e.getVelocity().add(1,0);
break;
}
return true;
}
#Override
public boolean keyUp(int keycode) {
switch (keycode) {
case Input.Keys.UP:
e.getVelocity().sub(0,1);
break;
case Input.Keys.DOWN:
e.getVelocity().sub(0,-1);
break;
case Input.Keys.LEFT:
e.getVelocity().sub(-1,0);
break;
case Input.Keys.RIGHT:
e.getVelocity().sub(1,0);
break;
}
return true;
}
Game Class:
SpriteBatch batch;
Map<UUID,Entity> entities = new HashMap<>();
#Override
public void create () {
batch = new SpriteBatch();
Entity e = new Entity(UUID.randomUUID(),new Texture("avatar.png"),Vector2.Zero,Vector2.Zero);
entities.put(e.getId(),e);
Gdx.input.setInputProcessor(new InputAdapter(){...});
}
#Override
public void render () {
entities.values().forEach(Entity::update);
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
entities.values().forEach(e->e.draw(batch));
batch.end();
}
#Override
public void dispose () {
batch.dispose();
entities.values().forEach(Entity::dispose);
entities.clear();
}
Entity Class:
private UUID id;
private Texture texture;
private Vector2 position;
private Vector2 velocity;
public Entity(UUID id, Texture texture, Vector2 position, Vector2 velocity) {
this.id = id;
this.texture = texture;
this.position = position;
this.velocity = velocity;
}
public UUID getId() {
return id;
}
public Texture getTexture() {
return texture;
}
public Vector2 getPosition() {
return position;
}
public void setPosition(Vector2 position) {
this.position = position;
}
public Vector2 getVelocity() {
return velocity;
}
public void setVelocity(Vector2 velocity) {
this.velocity = velocity;
}
public void draw(SpriteBatch batch) {
batch.draw(texture,getPosition().x,getPosition().y);
}
public void dispose() {
texture.dispose();
}
public void update() {
System.out.println(getVelocity());
getPosition().add(getVelocity());
}
Output (Once down key is pressed):
(0.0,0.0)
(0.0,-1.0)
(0.0,-2.0)
(0.0,-4.0)
(0.0,-8.0)
(0.0,-15.0)
(0.0,-30.0)
(0.0,-60.0)
(0.0,-120.0)
(0.0,-240.0)
(0.0,-480.0)
(0.0,-960.0)
(0.0,-1920.0)
(0.0,-3840.0)
(0.0,-7680.0)
(0.0,-15360.0)
(0.0,-30720.0)
(0.0,-61440.0)
(0.0,-122880.0)
(0.0,-245760.0)
(0.0,-491520.0)
(0.0,-983040.0)
(0.0,-1966080.0)
(0.0,-3932160.0)
(0.0,-7864320.0)
(0.0,-1.572864E7)
(0.0,-3.145728E7)
(0.0,-6.291456E7)
(0.0,-1.2582912E8)
(0.0,-2.5165824E8)
(0.0,-5.0331648E8)
(0.0,-1.00663296E9)
(0.0,-2.01326592E9)
(0.0,-4.02653184E9)
(0.0,-8.0530637E9)
(0.0,-1.61061274E10)
(0.0,-3.22122547E10)
(0.0,-6.4424509E10)
(0.0,-1.28849019E11)
(0.0,-2.57698038E11)
(0.0,-5.15396076E11)
(0.0,-1.03079215E12)
(0.0,-2.0615843E12)
(0.0,-4.1231686E12)
(0.0,-8.2463372E12)
(0.0,-1.64926744E13)
(0.0,-3.29853488E13)
(0.0,-6.5970698E13)
(0.0,-1.31941395E14)
(0.0,-2.63882791E14)
(0.0,-5.27765581E14)
(0.0,-1.05553116E15)
(0.0,-2.11106233E15)
(0.0,-4.22212465E15)
(0.0,-8.4442493E15)
(0.0,-1.68884986E16)
(0.0,-3.37769972E16)
(0.0,-6.7553994E16)
(0.0,-1.35107989E17)
(0.0,-2.70215978E17)
(0.0,-5.40431955E17)
(0.0,-1.08086391E18)
(0.0,-2.16172782E18)
(0.0,-4.32345564E18)
(0.0,-8.6469113E18)
(0.0,-1.7293823E19)
(0.0,-3.4587645E19)
(0.0,-6.917529E19)
(0.0,-1.3835058E20)
(0.0,-2.7670116E20)
(0.0,-5.5340232E20)
(0.0,-1.10680464E21)
(0.0,-2.2136093E21)
(0.0,-4.4272186E21)
(0.0,-8.854437E21)
(0.0,-1.7708874E22)
(0.0,-3.5417749E22)
(0.0,-7.0835497E22)
(0.0,-1.4167099E23)
(0.0,-2.8334199E23)
(0.0,-5.6668398E23)
(0.0,-1.13336796E24)
(0.0,-2.2667359E24)
(0.0,-4.5334718E24)
(0.0,-9.0669436E24)
(0.0,-1.8133887E25)
(0.0,-3.6267775E25)
(0.0,-7.253555E25)
(0.0,-1.450711E26)
(0.0,-2.901422E26)
(0.0,-5.802844E26)
(0.0,-1.1605688E27)
(0.0,-2.3211376E27)
(0.0,-4.642275E27)
(0.0,-9.28455E27)
(0.0,-1.85691E28)
(0.0,-3.71382E28)
(0.0,-7.42764E28)
(0.0,-1.485528E29)
(0.0,-2.971056E29)
(0.0,-5.942112E29)
(0.0,-1.1884224E30)
(0.0,-2.3768449E30)
(0.0,-4.7536898E30)
(0.0,-9.5073795E30)
(0.0,-1.9014759E31)
(0.0,-3.8029518E31)
(0.0,-7.6059036E31)
(0.0,-1.5211807E32)
(0.0,-3.0423614E32)
(0.0,-6.084723E32)
(0.0,-1.2169446E33)
(0.0,-2.4338892E33)
(0.0,-4.8677783E33)
(0.0,-9.7355566E33)
(0.0,-1.9471113E34)
(0.0,-3.8942226E34)
(0.0,-7.7884453E34)
(0.0,-1.5576891E35)
(0.0,-3.1153781E35)
(0.0,-6.2307562E35)
(0.0,-1.24615125E36)
(0.0,-2.4923025E36)
(0.0,-4.984605E36)
(0.0,-9.96921E36)
(0.0,-1.993842E37)
(0.0,-3.987684E37)
(0.0,-7.975368E37)
(0.0,-1.5950736E38)
(0.0,-3.1901472E38)
(0.0,-Infinity)
(0.0,-Infinity)
(0.0,-Infinity)
(0.0,-Infinity)
(0.0,-Infinity)
(0.0,-Infinity)
(0.0,-Infinity)
I'm unsure why the velocity vector is increasing, since the only time that it is modified is during the keydown event. If anyone could explain why this is happening, it would be much appriciated!
In Java, there is no such thing as an immutable object. You have passed in two copies of the static object instance Vector2.Zero to every new Entity. So whenever you press a key, you are adding a value to Vector2.Zero, and whenever you update an entity, you are adding Vector2.Zero's new value to itself, multiple times because the same instance is referenced by every Entity object.
A safe way to create your Entity class is to have it instantiate its own final Vector2 instances:
public class Entity {
private UUID id;
private Texture texture;
private final Vector2 position = new Vector2();
private final Vector2 velocity = new Vector2();
public Entity(UUID id, Texture texture) {
this.id = id;
this.texture = texture;
}
//...
}
Also, it is not a good idea to instantiate a new Texture object for every instance of Entity, when they all use the same image. You are loading many copies of the exact same image data to the GPU redundantly. Load your Texture one time and pass the same instance to all Entities, and don't forget to dispose of it in your dispose() method. Don't dispose of it in the Entity's dispose method, since it would then be disposed multiple times (the Entity doesn't "own" the Texture since it didn't instantiate it).

required and found error in inheritance

Hi im having trouble with how to user the super() keyword properly. It's a bit difficult to explain so please try to understand.
So I have a class called "Window" which is a subclass of another class called "Room".
This is Room class with a constructor
public class Room {
private Position position;
private Color color;
public Room(Position pos, Color colour){
this.position = pos;
this.color = color;
}
}
SO I want to give the doors a color Color.RED A Window object is constructed with an input position parameter.
What I have so far is:
public class Window extends Room{
private Color color;
public Window(Position position, Color color) {
super(position, color);
this.colour = Color.RED;
}
However in my other classes there is code that create a new Window with only a position parameter
This is example code
public class example{
private Window window;
private Position position;
public example() {
}
#Before
public void create() {
position = new Position(4,3);
window = new Window(position);
}
So then it becomes a required and found error, telling me that the length is not right. I understand what the error is telling me.
So here is my question. How do I redo the Window constructor so that it will satisfy both the super class and my other classes without those classes being changed.
Create new constructor in Window and set default color.
public Window(Position position) {
//Set default color
super(position, Color.RED);
this.colour = Color.RED;
}
Add another constructor to your Window class:
public class Window extends Room {
public Window(Position position) {
super(position, null);
}
Alternatively, define a default color which gets passed instead of null.

LibGDX not running render() method

I'm experiencing a problem using LibGDX.
I have a Screen and in its render method I have:
public class LoadingScreen implements Screen{
private OrthographicCamera guiCam;
private TankChallenge tankChallenge;
private SpriteBatch batcher;
private GL10 OpenGL;
public LoadingScreen(TankChallenge tankChallenge) {
this.tankChallenge=tankChallenge;
Gdx.graphics.setTitle("RobotChallange Beta - Loading");
float AR = Gdx.graphics.getHeight()/Gdx.graphics.getWidth();
guiCam = new OrthographicCamera(10,10 * AR);
guiCam.position.set(10f/2,AR*10f/2,0);
batcher = new SpriteBatch();
OpenGL = Gdx.graphics.getGL10();
//loadAssets();
}
#Override
public void render(float delta) {
OpenGL.glClearColor(1, 0.5f, 1, 1);
OpenGL.glClear(GL10.GL_COLOR_BUFFER_BIT);
System.out.print("e");
guiCam.update();
Sprite sp = new Sprite();
sp.setColor(1f,1f,0, 1);
sp.setSize(100,100);
sp.setOrigin(20, 0);
batcher.setProjectionMatrix(guiCam.combined);
batcher.begin();
batcher.disableBlending();
sp.draw(batcher);
batcher.end();
}
I call this by setting it in the implementes ApplicationListener class. I know it is arriving the LoadingScreen because Title is actually set to "RobotChallenge Beta - Loading".
I found a solution for both problems. I added to the Game extending class this:
public void render() {
super.render();
}
Thanks to this the problem is solved and I don't need to override the SetScreen method. Thanks!
Immediately after setting this as your screen, you need to call super.render() in your Game extending class. For instance, I use the following method in my main Game extending class for setting the screen:
#Override
public void setScreen(Screen screen)
{
super.setScreen(screen);
super.render();
}
This seems to kick off render() being called.
Just remove #Override render() in your Game extending class, that fixes the problem. Overriding setScreen method causes flickering you mentioned.

Categories

Resources