Hi, I am working on a basic space invaders app for an into to java class I'm taking. Here is my issue:
I have a main class called SpaceInvaders. This controls all of the objects in the program(missiles, space invaders, your ship) while the other classes create those objects. Currently I have all of the alien ships stored in a class called Aliens. They are created by using a basic 2D rectangle array. Back in SpaceInvaders I have a method called animateAliens which is supposed to make them move. To create the squadron of aliens I have an instance variable in my SpaceInvaders class:
private Aliens squadron.
I cannot, for the life of me, figure out how to get the aliens to move. I cant set the location of the pieces of the array directly from SpaceInvaders because the array is actually made in Aliens. I have posted the code for SpaceInvaders, Aliens, and my Missile class so you can compile it. Please help! I am very stuck!
/** * SpaceInvaders.java * * @author William * @class CS * @assignment prog12 * @due 4/24/2012 * */ //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //Imports import wheelsunh.users.*; import java.awt.event.*; import java.util.*; import java.awt.Color; public class SpaceInvaders implements Animator, KeyListener { //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Static Class Variables public static final int FRAME_WIDTH=652; public static final int FRAME_HEIGHT=650; private static int cols=11; private static int rows=5; //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Instance Variables private AnimationTimer timer; private Aliens squadron = null; private Vector<Missile> missiles; private Rectangle defender; private static final int LEFT=37, UP=38, RIGHT=39; private int playerScore=0, deltaX=5, alienMoveCount=0, deltaY=0; private Color defenderColor=Color.blue; private boolean tempvar=true; //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Default Constructor public SpaceInvaders() { squadron=new Aliens(); defender=new Rectangle(250,550); defender.setColor(defenderColor); defender.setSize( 50, 30); // initialize the Vector of missiles to an empty Vector missiles=new Vector<Missile>(); // create the timer and have it call animate on this animator timer=new AnimationTimer(10,this); // start the animation timer.start(); } //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Animation Method // For every missile in the Vector // call its "step" method and call the method // this.checkMissile( Missile ) public void animate() { while(tempvar==true); animateAliens(); for(int i=0;i<missiles.size();i++) animateMissile(missiles.get(i)); } //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Animates the aliens public void animateAliens() { System.out.println("Animating"); for(int r=0;r<rows;r++) { System.out.println("Row Loop"); for(int c=0;c<cols;c++) { System.out.println("Column Loop"); if(alienMoveCount%8==0) { System.out.println("moving y axis"); squadron[r][c].setLocation(squadron[r][c].getXLocation(), squadron[r][c].getYLocation()+deltaY); deltaX=-deltaX; alienMoveCount=0; } else { System.out.println("Moving x axis"); squadron[r][c].setLocation(squadron[r][c].getXLocation()+deltaX, squadron[r][c].getYLocation()); alienMoveCount+=1; } } } } //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Animates the Missile // First call missile.step() to move the missile then // Check to see if the missile hit an enemy ship // if it has then hide, set, and remove it from the vector public void animateMissile( Missile missile ) { missile.step(); if(squadron.checkMissileStrike(missile.getLocation())) { playerScore+=10; missile.hide(); missiles.remove(missile); } else if(missile.getYLocation()<0) { missile.hide(); missiles.remove(missile); } } //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Handles the movement of the defender public void keyPressed(KeyEvent e) { int code = e.getKeyCode( ); if(code==LEFT) { if(defender.getXLocation()<0) {} else defender.setLocation(defender.getXLocation()-10, defender.getYLocation()); } else if(code==RIGHT) { if((defender.getXLocation()+defender.getWidth())>=FRAME_WIDTH) {} else defender.setLocation(defender.getXLocation()+ 10,defender.getYLocation()); } else if(code==UP) { if(missiles.size()!=0) { System.out.println("Only 1 missle can be shot at a time!"); } else { Missile attackmissile = new Missile(defender.getXLocation()+ defender.getWidth()/2, defender.getYLocation()); missiles.add(attackmissile); } } } //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Handles the keys hit public void keyTyped(KeyEvent e) { // System.out.println( "XXKEY TYPED: " + e.getKeyCode( ) ); // Not Used But Must Be Kept For KeyListener To Work } //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Handles the keys release public void keyReleased(KeyEvent e) { // System.out.println("XXKEY RELEASED: " + e.getKeyCode( ) ); // Not Used But Must Be Kept For KeyListener To Work } //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //Main public static void main( String args[ ] ) { Frame f = new Frame( FRAME_WIDTH, FRAME_HEIGHT ); SpaceInvaders app = new SpaceInvaders( ); f.addKeyListener( app ); } } //End Of SpaceInvaders.java
/** * Aliens.java * * @author William * @class CS * @assignment prog12 * @due 4/24/2012 * */ //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //Imports import wheelsunh.users.*; import java.awt.Color; import java.awt.Point; public class Aliens { //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Class Variables private static int cols=11; private static int rows=5; //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Instance Variables protected Rectangle[][] squadron; private int alienWidth=36,alienHeight=24,hSpace=6,vSpace=20; //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Default ConstructorCreates The Aliens Based Off Of The // Instance Variables public Aliens( ) { squadron=new Rectangle[rows][cols]; for(int r=0;r<rows;r++) { for(int c=0;c<cols;c++) { Rectangle ship = new Rectangle(); ship.setSize(alienWidth,alienHeight); ship.setLocation(c*(alienWidth+hSpace), r*(alienHeight+vSpace)); ship.setColor(Color.RED); squadron[r][c]=ship; } } } //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //Checks For Missile Strikes With Aliens public boolean checkMissileStrike( Point p ) { for(int r=0;r<5;r++) { for(int c=0;c<11;c++) { if(squadron[r][c].contains(p)) { System.out.println("Row: "+r+", Column: "+c); squadron[r][c].setLocation(-100,-100); return true; } } } return false; } //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //Main public static void main(String[] args) { new Frame(); new Aliens(); } } //End Of Aliens.java
*Edited to remove my name/** * Missile.java * * @author William * @class CS * @assignment prog12 * @due 4/24/2012 * */ //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Imports import wheelsunh.users.*; import java.awt.Color; public class Missile extends ShapeGroup { //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Static Class Variables public static final int FRAME_WIDTH=652; public static final int FRAME_HEIGHT=650; //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Instance Variables private RoundedRectangle body; private int deltaY=-5, bodyX=4, bodyY=25; private Color bodyColor=Color.green, frameColor=Color.orange; //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Default Constructor public Missile(int x, int y) { body=new RoundedRectangle(0, 0); body.setFillColor(bodyColor); body.setFrameColor(frameColor); body.setSize(bodyX,bodyY); add(body); setLocation(x,y); } //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Moves The Missile public void step() { setLocation(getXLocation(), getYLocation() + deltaY); } //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Hides The Missile public void hide( ) { body.hide(); } //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Main public static void main(String[] args) { Frame f = new Frame(FRAME_WIDTH, FRAME_HEIGHT); SpaceInvaders app = new SpaceInvaders(); f.addKeyListener(app); } } //End Of Missile.java