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

2011/12/26

Checking if object is in the world

prti prti

2011/12/26

#
Hi, when I press enter, I wanna load a new world in my game, if there is (or isn't) a certain object currently in my world. How do i check if there is an object currently located in the world?
danpost danpost

2011/12/26

#
In the new world class, in the constructor, or in a method the constructor calls, add the code
List objectslookingfor = getObjects(ObjectLookingFor.class);
if (objectslookingfor.size() == 0) addObject(new ObjectLookingFor(), x, y);
On the second line of the class, insert the line
import java.util.List;
You need this because getObjects(Class class) returns a List object
prti prti

2011/12/27

#
Tnx danpost, now I get how to check for objects.. But I dont want to add new object after check, I want to remove current world, and load a new one.. Edit: also dont know how to load a new object into the existing world(if there is/isn't certain object in the world), without creating another world .. I tried writing a similar code in actor classes and in my world, just can't figure it out..
Duta Duta

2011/12/28

#
If what you mean is you want to just reset everything, then just have in the world's act() method, do something like: - if(enter is down) - remove all objects - method(); In the method I called method(), you should put in there all of the stuff in the worlds constructor that is creating the initial objects (all the addObject(new ActorName(), x, y)'s). This probably isn't my clearest explanation ever, so tell me if you don't understand and I'll post something more comprehensible - I'm currently on the verge of falling asleep
prti prti

2011/12/28

#
Looks like Im missig something.. here's my source code.. Beegator When i press enter, i want this to happen: if alligator is visible, remove all object currently in the world, and add new Man, if bee is visible, remove all object currently in the world, and add new Flower
danpost danpost

2011/12/29

#
One thing I have noticed, is that you are using isKeyDown(String key), where you should probably be using getKey(). When using the former, if the key is down (referring to your scenario, with "up") the act of creating a new object repeats until you release the key (at least as far as the 'start' object is concerned). However, you probably did not intent for the code dealing with keystrokes in your 'start' object to be there. Anyway, 'isKeyDown(String key)' is intended for repetitive action, whereas 'getKey()' is intended to act once for each keypress. Fortunately, because you are removing the 'bee' or the 'aligator' object when the appropriate key is pressed, it does act only once (barring the actions of the 'start' object). In short, the code in 'bee' and in 'aligator' are, in essence, OK. I did not see any code, or attempt at code to add a man or a flower when the "enter" key was pressed. If the object is to be deleted when one of these is created, the same basic code structure would suffice. On the other hand, if the object is not removed, and you only want to create a single instance of the 'man' or 'flower', you could use code something like this
String key = Greenfoot.getKey();
if (key != null)
{
    if ("enter".equals(key))
    {
        getWorld().addObject(new Man(), x, y);  // x and y can be literals or preset integer variables
        // getWorld().removeObject(this);   ***   if removing object also  ***
    }
}
This code would of course be in the 'aligator' class, and similar code would go in the 'bee' class, with 'new Flower()' instead of 'new Man()'.
prti prti

2011/12/29

#
Thanks!
prti prti

2011/12/29

#
I'm stuck again.. I have 2 objects in the World: object A(user controls movement) and object B(static). I want to add object C in the world on the position of object A, when A intersects with B. I added this to World class:
private A theA;

public A getA()
   {
       return theA;
   }
this to A class:
public static int x =0; 
public static int y =0; 
   
 public void act() 
    {
        x=getX();
        y=getY();        
     }
Than I tried putting this code in class B, but that only creates object C in the middle of object B
public void addC()
    {
        
        Actor A;
        A=getOneIntersectingObject(A.class);
        

        if(A !=null) 
        
        {
            World world;
            world=getWorld();
            world.addObject(new C(), getX(), getY());
                            
        }
    }
I think i should put something like this on the last line, but i cant figure out how
addObject(new C(), A.x, A.y);
also, class A moves 5 forward every time I press "up", and i just want to add object C once, every time A moves, but I have no idea how to do that.
danpost danpost

2011/12/29

#
Since you want to add object C in the middle of A when A intersects B, it stands to reason that 'addC()' should be in A, not in B. In the A.class act() method,
if (intersectingB()) addC();'
Then write the two methods
private boolean intersectingB()
{
    Actor b;
    b = getOneIntersectingObject(B.class);
    return (b != null)
}

private void addC()
{
    World world;
    world.getWorld();
    world.addObject(new C(), getX(), getY());
}
Make sure you move A far enough, so it no longer intersects B, after creating C, or you will create multiple Cs.
prti prti

2011/12/30

#
In B class is this code:
    public void addC()  
        {  
              
            Actor A;  
            A=getOneIntersectingObject(A.class);  
              
      
            if(A !=null)   
              
            {  
                World world;  
                world=getWorld();  
                world.addObject(new C(), getX(), getY());  
                                  
            }  
        }  
I have a line of multiple B objects in the world. When there is a specific number of C objects created over one B object, I want to remove that B, and all C objets created over it. I want to remove each B object individuali, when there's enough C over it. this is the last thing i need, hope i didnt bug u too much, tnx
danpost danpost

2011/12/30

#
Then, if keeping to the B class, as far as
prti wrote...
I think i should put something like this on the last line, but i cant figure out how
addObject(new C(), A.x, A.y);
you could use
addObject(new C(), A.getX(), A.getY());
As far as removal,
List cList = getIntersectingObjects(C.class);
if (cList.size() >= maxCperB)
{
    getWorld().removeObjects(cList); // removing all c objects that are on this b object
    getWorld().removeObject(this); // removing this b object
}
You will need to import java.util.List for this code.
prti prti

2012/1/4

#
how can C objects have the same rotation as B objects underneath, when Cs are created? i have multiple Bs with differend rotations
You need to login to post a reply.