user1134409
user1134409

Reputation: 31

How to get the invader moving left to right in java

I'm creating a small space invaders project and I have the aliens constantly in a left to right loop. When they hit the end of the screen they repaint on the right side going again left to right. I have set the window size already and I have looked at various tutorials on how to make space invaders in Java, however most of them say the same thing that I have tried. Is it possible someone can point out where I am going wrong with my coding so that I know how to fix it.

This is the code for the alien class. There are different aliens, however all the classes look pretty much the same as this one:

package spaceinvaders2;

import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;

class Alien extends MoveObject 
{
    Image Alien = new ImageIcon(getClass().getResource("alien.gif")).getImage();
Alien(int x, int y, int w, int h) 
{
    super(x, y);
    bounds.width = w;
    bounds.height = h;
}

public void paint(Graphics g) 
{
    System.out.println("Alien generated");
    bounds.x = bounds.x - 2;

    if ((bounds.x + bounds.width)< 0)
    {
        bounds.x = 750;
        dead = false;
    }

    g.drawImage(Alien,bounds.x,bounds.y,bounds.width, bounds.height, this);
}
}

Edit: My paint method is being called in the main game functionality part where it draws all the graphics. The drawing isn't the problem it is the movement of the alien which is in this class.

Upvotes: 1

Views: 7873

Answers (3)

According to your comment, you want the aliens to move from left to right once they hit the screen boundaries, instead of re-entering at the other side.

The solution is simple, you have to keep track of the direction of the alien. An easy way to do this is to make it's step size a variable.

You give your Alien class a step member like this:

int step = -2;

Then:

bounds.x = bounds.x + direction;

if ((bounds.x + bounds.width)< 0)
{
    step = +2;
}
else if ((bounds.x - bound.width) > 750)
{
    step = -2;
}
dead = false;

g.drawImage(Alien,bounds.x,bounds.y,bounds.width, bounds.height, this);

Off-topic, I think the dead = false doesn't belong in your paint method.

Upvotes: 0

dogbane
dogbane

Reputation: 274630

You need to give the Alien a direction attribute (+1 or -1) which tells it which direction it is moving in. When it is about to go off-screen, flip the direction. For example, if the direction was +1 change it to -1 and vice versa.

Here is a simple example I created:

public class Alien extends JPanel 

    private int x = 5;
    private int y = 5;
    private int direction = 1;

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.clearRect(x, y, getWidth(), getHeight());

        // draw the alien.
        g.drawRect(x, y, 10, 10);

        // move it
        x = x + 5 * direction;

        // is it about to go off-screen?
        if (x < 0 || x + 10 > getWidth()) {
            // change the direction
            direction *= -1;
        }
    }
}

Upvotes: 1

stetro
stetro

Reputation: 547

I think the paint Method ist just running once. You have to add a thread to call the paint() method periodi.

Upvotes: 2

Related Questions