Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Object Array - Drop objects in array by int
    primarykey
    data
    text
    <p>Im currently am working on a game in Java that spawns blocks on the top of the screen and they fall down (you have to dodge them). Currently I have the game spawning blocks on the top of the screen from an array but I do not know how to make their y position go down by (int).</p> <p>Basically I just need help making a public void that checks an object array and with every instance of a object it finds it drops the y position of it by 12. </p> <p>Selected snippits from code:</p> <pre><code>static Object[][] food = new Object[7][700]; public void draw() { try { Graphics g = bufferStrategy.getDrawGraphics(); g.clearRect(0, 0, this.getSize().width, this.getSize().height); // Draw to back buffer g.setColor(Color.WHITE); g.fillRect(0, 0, this.getSize().width, this.getSize().height); g.setColor(Color.BLUE); g.fillRect(Player.getX(), Player.getY(), Player.WIDTH, Player.HEIGHT); g.setColor(Color.GRAY); for(int x = 0; x &lt; food.length; x++) { for(int y = 0; y &lt; food[x].length; y ++) { Object o = food[x][y]; if(o instanceof Hamburger) { new Hamburger(x * 100, y, g); } else if(o instanceof Salad) { new Salad(x * 100, y, g); } } } GUI.draw(g); } catch(Exception e) { e.printStackTrace(); } finally { bufferGraphics.dispose(); } } public void dropBlocks() { } public void addBlock() { if(new Random().nextInt(2) == 1) { food[new Random().nextInt(7)][0] = new Hamburger(0, 0); } else food[new Random().nextInt(7)][0] = new Salad(0, 0); } public void drawBackbufferToScreen() { bufferStrategy.show(); Toolkit.getDefaultToolkit().sync(); } public void run() { int i = 0; while(running) { i++; draw(); Player.update(); drawBackbufferToScreen(); if(i == 50) { addBlock(); i = 0; } dropBlocks(); Thread.currentThread(); try { Thread.sleep(10); } catch(Exception e) { e.printStackTrace(); } } } </code></pre> <p>Food.java (What Hamburger and Salad extend)</p> <pre><code>public class Food { public static int x; public static int y; public static int HEIGHT; public static int WIDTH; public int type; private static Image blockImage; // Default constructor public Food() { Food.x = 0; Food.y = 0; Food.HEIGHT = 80; Food.WIDTH = 80; } public Food(int x, int y) { Food.x = x; Food.y = y; Food.HEIGHT = 80; Food.WIDTH = 80; } // Getters and setters </code></pre>
    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