Creating new Object Help!!!
Hi so I wanted to create 2 shields in my Space Invaders class that you can hit with a missile or bomb, but when I created them both, only one you are able to shoot at... here are my segments of code
Shield Class
Code :
public class Shield extends ActiveObject
{
/*
* constants
*/
private static final int ROW = 25;
private static final int COL = 25;
/*
* instance fields
*/
private FilledRect[][] shield;
/*
* class fields
*/
/**
* Shield constructor
*
* This is the shield constructor that creates the shields for the game
*
* @param x the X coordiante where the shield is created
* @param y the Y coordinate where the shield is created
* @param canvas the drawing canvas of the game
*/
public Shield (double x, double y, DrawingCanvas canvas)
{
shield = new FilledRect[ROW][COL];
Random randGen = new Random ();
for (int row = 0; row < shield.length; row++)
{
for (int col = 0; col < shield[0].length; col++)
{
shield[row][col] = new FilledRect (x + row * 3, y + col * 3, 3, 3, canvas);
switch (randGen.nextInt (5))
{
case 0: shield[row][col].setColor (Color.PINK);
break;
case 1: shield[row][col].setColor (Color.CYAN);
break;
case 2: shield[row][col].setColor (Color.YELLOW);
break;
case 3: shield[row][col].setColor (Color.ORANGE);
break;
case 4: shield[row][col].setColor (Color.MAGENTA);
break;
}
}
}
}
/**
* isHit method
*
* This is the isHit method that determines whether or not the shield has been hit
* and then creates a hole if it has been.
*
* @param missile the missile shot off by the laser base
* @param bomb the bomb shot off by the aliens
* @return true or false depending on whether or not the shield is hit by the missile or bomb
*/
public synchronized boolean isHit (FilledRect missile, FilledRect bomb)
{
for (int row = 0; row < shield.length; row++)
{
for (int col = 0; col < shield[0].length; col++)
{
if (!shield[row][col].isHidden () && shield[row][col].overlaps (missile))
{
shield[row][col].hide ();
missile.hide ();
return true;
}
else if (!shield[row][col].isHidden () && shield[row][col].overlaps (bomb))
{
shield[row][col].hide ();
bomb.hide ();
return true;
}
}
}
return false;
}
}
Space Invaders Class where I create the shields
Code :
Shield shield1 = new Shield (SpaceInvaders.BOTTOM_BORDER / 3 - 50, SpaceInvaders.RIGHT_BORDER / 3 + 50, canvas);
base.setTarget (shield1);
Shield shield2 = new Shield (SpaceInvaders.BOTTOM_BORDER / 3 + 150, SpaceInvaders.RIGHT_BORDER / 3 + 50, canvas);
base.setTarget (shield2);
/*
* Set targets
*/
base.setTarget (ship);
base.setTarget (invaders);
invaders.setTarget (shield1);
invaders.setTarget (shield2);
can anyone please help me why only one of the shields is working???? Its kind of urgent, since this is due in 2 days...
Re: Creating new Object Help!!!
If you want anyone to test the problem, you need to make a small complete program that compiles, executes and shows the problem. Don't post the whole project.
Re: Creating new Object Help!!!
Quote:
Originally Posted by
Norm
If you want anyone to test the problem, you need to make a small complete program that compiles, executes and shows the problem. Don't post the whole project.
Ya, what Norm said. Because:
base.setTarget (shield1);
followed by:
base.setTarget (shield2);
Makes it look like you replaced the shield1 with shield2. But without seeing what the method actually does, we can only guess.
I am guessing this was not the problem because:
base.setTarget (ship);
and:
base.setTarget (invaders);
Where you setTarget on non shield objects.
Quote:
???? Its kind of urgent, since this is due in 2 days...
It is only urgent to you, I promise. Best to leave that kind of chatter out of your post and stick to the point. It wastes readers time, draws attention away from the problem you have, and leads some people to ignoring your post completely.
Outside posting more code, look back where you think you set up 2 shields. Make sure they are set up and able to be used. Which of the two is being used and which is being ignored? What code should have hit the shield that is not working? How do you determine which shield to hit? Have you added printlns to see values you use in creating/using the shield? ...or since you are using eclipse, have you watched in the debugger?