This site requires JavaScript, please enable it in your browser!
Greenfoot back
BradH
BradH wrote ...

2012/12/29

Real quick question

BradH BradH

2012/12/29

#
This seems like a simple problem but for some reason it has me flustered, in this code I have the actor Bullet1 being shot out of a gun in a short burst of 3 but as I compile this I get an error saying "<=" is a bad operator type why is this, I defined the Bullet1 and once "l" is pressed it should fire only if there are <= 3 Bullets in the world. Have I misplaced the <= 3 and it should go somewhere else in the code? Thanks for your time
//declare bullet1 to be bullet.class
Actor Bullet1;
        Bullet1 = getOneObjectAtOffset(0, 0, Bullet1.class);
        //if the key is pressed execute the rest of the code
           if("l".equals(Greenfoot.getKey()))
           {
             if ((Bullet1.class) <= 3)
                //fire only if there are 3 or less 
                //Bullet1 in the world
                  fire();
                //fire the bullet
        }
danpost danpost

2012/12/29

#
Line 7 compares a class with a number. Obviously, that cannot be done; hence the error. You need to compare the number with the size of the list of items of that class that are in the world.
if(getWorld().getObjects(Bullet1.class).size()<=3)
Lines 2 and 3 are not needed at all.
You need to login to post a reply.