The task is to be able to move the alien right, down, and left. So far all i can get the alien to do is move right and down. I need to get it to be able to move left. Can you help me please?

import java.awt.Graphics;
import java.awt.Image;


/**
 * A single alien in the Space Invaders game.
 * 
 * @author Kevin Glass
 */
class Alien {

    // CONSTANTS
    private final String ALIEN_ICON = "alien.gif";
    private final int MAX_WIDTH;  // Size of canvas on which Alien is moving
    private final int MAX_HEIGHT;

    // Initial speed at which the alien moves horizontally 
    private final double INITIAL_X_SPEED = 0.5;  // NOT an int - must be double

    private final Sprite SPRITE;    // Graphical representation of alien

    // VARIABLES
    private double upperLeftX, upperLeftY;  // Position of alien on canvas


    /*
     * Create a new alien object - Constructor
     */
    public Alien(int theX, int theY, int theMaxWidth, int theMaxHeight) {
        upperLeftX = theX;
        upperLeftY = theY;
        MAX_WIDTH = theMaxWidth;
        MAX_HEIGHT = theMaxHeight;

        // Get the graphical representation of the alien
        SPRITE = SpriteStore.get().getSprite(ALIEN_ICON);
    }

    public void draw(Graphics g) {
        SPRITE.draw(g, (int) upperLeftX, (int) upperLeftY);
    }

    public void move() {

        int x = MAX_WIDTH;  

             x = 700;  


                if (upperLeftX >= x){  

                    upperLeftY++;  

                }  



                if (upperLeftX <= x){  

                 upperLeftX++;  

                } 

Recommended Answers

All 12 Replies

Your pretty much there if you are able to move the alien up and down. In order to move it left you just need to decrease the x variable when pressed relative to when pressed right.
Here is an example I made a while ago:

if (key == KeyEvent.VK_LEFT) {
        	
        	ImageIcon ii = new ImageIcon(this.getClass().getResource("dukeleft.jpg"));
            image = ii.getImage();
            
            dx = -5;
        }

key is of type integer and declared like:

int key = e.getKeyCode();

It returns the image declared in that class and when the left arrow is pressed it moves the image by -5.

Does this help at all?

The alien has to move right, down, left, down continuously and stop when it reaches the bottom automatically. I am not able to press arrows during the game, it has to do it automatically.

This is the code that I can up with, but I cant get it all to work together either. The first if statement moves the alien left, the second one moves it down, and the third moves it right, but when I try to run it, it just goes down. Any ideas?

public void move() {  

        if (upperLeftX >= MAX_WIDTH - 40);{  
        upperLeftX = upperLeftX + INITIAL_X_SPEED;  
        }  

        if (upperLeftX <= MAX_WIDTH);{  
        upperLeftY = upperLeftY + INITIAL_X_SPEED;  
        }  

        if (upperLeftX <= MAX_WIDTH-40);{  
       upperLeftX = upperLeftX - INITIAL_X_SPEED;}

this post was wrong :/

although:

try adding print statements (im assuming you dont have a debugger), put System.out.println(upperLeftX) and System.out.println(MAX_WIDTH-40) to find out whats going on, whether its getting into if statements and why not

I am able to print out statements, i just need help with getting the aliens to move.

Okay, looks like you need the following...

public static final int XMIN = 0;
public static final int XMAX = 700;

private int xPos = 0;
private int yPos = 0;
private int xDir = 1;

public void move()
{
  xPos += xDir;
  if( ( xPos <= XMIN ) || ( xPos >= XMAX ) )
  {
    xDir = -xDir;
    yPos++;
  }
}

I am able to print out statements, i just need help with getting the aliens to move.

yes, i know you can print out statements. Printing out statements is the easiest and most intuative way of debugging problems with code, finding out what the variables are each step/whether its getting into if statements is invaluable for working out why your program isn't doing what it should be.

I wasn't asking you to use print statements and give up making it move, i was asking you to use print statements to find out why it wasnt't moving.

Eg, if it printed out your X value increasing every frame, your problem is not even in this part of the program, but with drawing - how would you find that out otherwise?

When I tried the code that was given to me above, the alien didn't move at all.

When I tried the code that was given to me above, the alien didn't move at all.

Sorry, you'll need to replace the variable names to those in your program. The idea is that every time your sprite hits either the left or right side of the screen, you change its direction of travel in x and move it down.

So "xPos" would be your "upperLeftX", "yPos" would be your "upperLeftY", "XMIN" would be "MAX_WIDTH", and "XMAX" would be "MAX_WIDTH".

This is the new code I used

public void move(){
			
		int MIN_WIDTH = 0;
		int MAX_WIDTH = 700;

		int upperLeftX = 0;
		int upperLeftY = 0;
		int INITIAL_X_SPEED = 1;

		
		{

		    upperLeftX += INITIAL_X_SPEED;
		    if( (upperLeftX <= MIN_WIDTH ) || ( upperLeftX >= MAX_WIDTH ) )
		    {

		        INITIAL_X_SPEED = -INITIAL_X_SPEED;
		        upperLeftY++;

		    }

		}

The alien still didn't move. Is there something I did wrong?

Ah...you need to put the variable declarations outside of the function. That should solve the problem :-)

This is the new code I used

public void move(){
			
		int MIN_WIDTH = 0;
		int MAX_WIDTH = 700;

		int upperLeftX = 0;
		int upperLeftY = 0;
		int INITIAL_X_SPEED = 1;

		
		{

		    upperLeftX += INITIAL_X_SPEED;
		    if( (upperLeftX <= MIN_WIDTH ) || ( upperLeftX >= MAX_WIDTH ) )
		    {

		        INITIAL_X_SPEED = -INITIAL_X_SPEED;
		        upperLeftY++;

		    }

		}

The alien still didn't move. Is there something I did wrong?

Change to...

int MIN_WIDTH = 0;
int MAX_WIDTH = 700;
int X_SPEED = 1;
int Y_SPEED = 1; // Note: upperLeftY will increase by this amount every time the
                 // invader hits the side of the screen (so you'll probably need
                 // a larger value, as it will only move down 1 pixel each time).

int upperLeftX = 0;
int upperLeftY = 0;
int velocityX = INITIAL_X_SPEED

public void move()
{
    upperLeftX += velocityX;
    if( ( upperLeftX <= MIN_WIDTH ) || ( upperLeftX >= MAX_WIDTH ) )
    {
        velocityX = -velocityX;
        upperLeftY += Y_SPEED;
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.