Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Creating new Object Help!!!

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Exclamation 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
    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

    		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...


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Creating new Object Help!!!

    Quote Originally Posted by Norm View Post
    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.
    ???? 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?

Similar Threads

  1. Creating object everytime object is called
    By aandcmedia in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 12th, 2012, 04:18 PM
  2. Creating and implementing class for creating a calendar object
    By kumalh in forum Object Oriented Programming
    Replies: 3
    Last Post: July 29th, 2011, 08:40 AM
  3. Using a string when creating an object.
    By ZeroLRS in forum Object Oriented Programming
    Replies: 10
    Last Post: July 13th, 2011, 09:22 PM
  4. Creating an object...
    By RodePope4546 in forum Object Oriented Programming
    Replies: 6
    Last Post: June 27th, 2011, 06:44 AM
  5. Creating Servlet object
    By tcstcs in forum Java Servlet
    Replies: 3
    Last Post: May 9th, 2011, 02:13 AM