Note that there are some explanatory texts on larger screens.

plurals
  1. POAll objects in arraylist have the same value
    primarykey
    data
    text
    <p>Okay, so all I want to do is create a simple java program that has an arraylist full of objects, in this case bouncing balls, that can be added to in game. The way I want it to work is, you start the program and it is a blank screen. You press space and it creates a ball that bounces off the sides kepp pressing space and it makes more balls. The problem i have though is whem i add more balls it sets every item in the arraylist to the same x and y coordinates. Oh and im using the slick2D libs but i dont think thats the problem.</p> <p>here is the main part of the program</p> <pre><code>public static ArrayList&lt;EntityBall&gt; ballList; @Override public void init(GameContainer gc) throws SlickException { ballList = new ArrayList&lt;EntityBall&gt;(); } @Override public void update(GameContainer gc, int delta) throws SlickException { String TITLE = _title + " | " + gc.getFPS() + " FPS" + " | " + ballList.size() + " entities"; frame.setTitle(TITLE); Input input = gc.getInput(); if (input.isKeyPressed(Input.KEY_SPACE)) { addBall(); } } public void render(GameContainer gc, Graphics g) throws SlickException { for(EntityBall e : ballList) { e.render(g); } } public static void addBall() { ballList.add(new EntityBall(getRandom(0, _width - ballWidth), getRandom(0, _height - ballWidth), 20, 20)); } public static int getRandom(int min, int max) { return min + (int) (Math.random() * ((max - min) + 1)); } </code></pre> <p>and heres the EntityBall Class</p> <pre><code>package me.Ephyxia.Balls; </code></pre> <p>import org.newdawn.slick.Color; import org.newdawn.slick.Graphics;</p> <p>public class EntityBall {</p> <pre><code>public static int x; public static int y; public static int height; public static int width; public EntityBall(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; } public void render(Graphics g){ g.fillOval(x, y, width, height); } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload