Note that there are some explanatory texts on larger screens.

plurals
  1. POJava platformer game doesn't run the initial few lines of code...?
    primarykey
    data
    text
    <p>I am making a basic outline for a 2d platformer game in Java... I'm having an issue that started pretty recently! Sometimes, when I run my program, my frame shows up, and everything is set, and you can see the guy, but for some reason the ground does not get "added" (i have an addGround() method which I call in the very first segment that runs). As I got farther into the game, this started happening more and more until now, most of the time it fails and won't show/add the ground! Sometimes, though, it does it perfectly.</p> <p>This is how it looks when it works:</p> <p><img src="https://i.imgur.com/KmDeh.png" alt=""></p> <p>This is how it looks most of the time, when it doesn't work:</p> <p><img src="https://i.imgur.com/oi2z4.png" alt=""></p> <p>So, this is my main class code: (I have not included all the imports and stuff, just the core)</p> <pre><code> public BusiWorld() { this.setFocusable(true); if(init) { ground.addGround(x-300, y+100, 35, 1); ground.addGround(x-100, y-360, 1, 46); ground.addGround(x+100, y+90, 1, 1); ground.addGround(x+400, y+20, 2, 1); ground.addGround(x+460, y-50, 2, 1); ground.addGround(x+520, y-120, 2, 1); ground.addGround(x+460, y-190, 2, 1); ground.addGround(x+140, y-260, 15, 1); ground.addGround(x, y-280, 1, 1); init = false; } t = new Timer(16, new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { repaint(); } }); t.start(); } public static void main(String[] args) { mainFrame.setTitle("Busiworld"); mainFrame.setSize(600,500); mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainFrame.setBackground(Color.WHITE); mainFrame.setResizable(false); mainFrame.setVisible(true); } } </code></pre> <p>And this is the ground class code:</p> <pre><code>public class Ground extends JFrame { private Image groundImg = Toolkit.getDefaultToolkit().getImage("ground.png"); private ArrayList&lt;Point&gt; groundLocs = new ArrayList&lt;Point&gt;(); public Ground() { } public void addGround(int xCoord, int yCoord, int howManyWide, int howManyTall) { for(int i = 0; i &lt; howManyWide; i++) { for(int j = 0; j &lt; howManyTall; j++) { groundLocs.add(new Point(xCoord+i*groundImg.getWidth(null), yCoord+j*groundImg.getHeight(null))); } } } public void drawGround(Graphics g) { for(Point p: groundLocs) { g.drawImage(groundImg, p.x, p.y, this); } } public int groundArraySize() { return groundLocs.size(); } public Point getGroundArray(int index) { return groundLocs.get(index); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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