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 2 of 2

Thread: Pretty new to Java can someone help with an addObject() problem?

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Pretty new to Java can someone help with an addObject() problem?

    Hi I'm pretty knew to programming and I have an assignment which I've been working on but I am stuck

    I have to create a 'space invaders' style game, and I've got some basics working. I wanted to add a boss enemy, which appears when all other enemies have been destroyed. It kind of works, only the program spawns a seemingly infinite amount of the boss which then creates a 'wipe' effect across the top of the screen.
    Here is my code:
    import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
    import java.util.List;
     
     
    public class Space extends World
    {
     
         /**
         * Constructor for objects of class Space.
         *
         */
         public Space()
         {
              // Create a new world with 200x250 cells with a cell size of 3x3 pixels.
              super(200, 250, 3);
              //add class with starstreaks
              addObject(new EmptyBox(),1,1);
              //add player character
              addObject(new Player(), 100, 228);
     
              //add 4 romulan enemies row 1
              for(int i=0; i<4; i++){
                   addObject(new Romulan(), (25 + 50 * i), 15);
              }
     
              //add 3 klingon enemies row 2
              for(int i=0; i<3; i++){
                   addObject(new Klingon(), (50 + 50 * i), 40);
              }
     
              //add 4 romulan enemies row 3
              for(int i=0; i<4; i++){
                   addObject(new Romulan(), (25 + 50 * i), 65);
              }
     
              //add 3 klingon enemies row 4
              for(int i=0; i<3; i++){
                   addObject(new Klingon(), (50 + 50 * i), 90);
              }
     
              //add 3 shields
              for(int i=0; i<3; i++){
                   addObject(new Shield(), (30 + 70 * i), 180);
              }
         }
     
         [B]public void act(){
     
              List<Enemy> enemies = getObjects(Enemy.class);
     
              if(enemies.size()==0 ){
                   addObject(new Borg(), 100, 30 );
              }
         }[/B]
    }

    I believe the part in bold is the problem, but I dont know why. Klingon and Romulan are subclasses of the Enemy class. The Borg class is it's own seperate class, but with the exact same functions as Enemy. I made it seperate as I figured it would keep respawning if it was a subclass of Enemy.


    Any ideas?
    Last edited by helloworld922; November 1st, 2009 at 12:07 PM.


  2. #2
    Junior Member
    Join Date
    Nov 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Pretty new to Java can someone help with an addObject() problem?

    Its ok, after more than an hour I've got it.
    I simply added private int bossValue = 0; at the beginning and then changed act to this:
    public void act(){
     
    List<Enemy> enemies = getObjects(Enemy.class);
     
     
    if(enemies.size()==0 && bossValue==0 ){
    //for(int i=0; i<1; i++){
     
    addObject(new Borg(), 100, 30 );
    bossValue =1;
    //}
    }
    }
    Last edited by helloworld922; November 1st, 2009 at 04:43 PM.

Similar Threads

  1. java. Text problem...
    By Ranger-Man in forum Java SE APIs
    Replies: 8
    Last Post: September 7th, 2009, 07:19 PM
  2. please could you help me with this java problem im very lost
    By eyeore in forum Java Theory & Questions
    Replies: 4
    Last Post: September 7th, 2009, 09:19 AM
  3. java problem
    By Mj Shine in forum Java Theory & Questions
    Replies: 1
    Last Post: August 14th, 2009, 12:24 AM
  4. Java GUI problem in paintComponent?
    By Richard_ in forum AWT / Java Swing
    Replies: 2
    Last Post: May 1st, 2009, 08:19 AM
  5. [SOLVED] JAVA JList Problem in visual images
    By antitru5t in forum AWT / Java Swing
    Replies: 4
    Last Post: April 15th, 2009, 03:09 PM