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

2013/1/9

Adding an object...

zakariaonlyy zakariaonlyy

2013/1/9

#
hey peeps, so i have a little problem. I want one of my actors to add an object (zandzak) if the upper neighbour is a water object. however it seems their is something wrong with my if statement because i can add an object everywhere hitting space... this is what my code looks like and i hope you can help List kijkbuuren; public void act() { kijkbuuren = getNeighbours(0,false, Watervloed.class); if(Greenfoot.isKeyDown("left")) { setLocation(getX()-1,getY()); } if(Greenfoot.isKeyDown("right")) { setLocation(getX()+1,getY()); } if(Greenfoot.isKeyDown("up")) { setLocation(getX(),getY()-1); } if(Greenfoot.isKeyDown("down")) { setLocation(getX(),getY()+1); } if(kijkbuuren !=null && Greenfoot.isKeyDown("space")) { int x = getX(); int y = getY(); getWorld().addObject(new zandzak(),x,y); } }
danpost danpost

2013/1/9

#
The condition 'kijkbuuren !=null' will always be 'true' because 'kijkbuuren' is a List object and will never be 'null'. However, it can be an empty list, so you should probably check the condition '!kijkbuuren.isEmpty()'.
zakariaonlyy zakariaonlyy

2013/1/9

#
I just tried it but i doesn't do what i want it to do. Actually i want it to check if the upper neighbour is a watervloed object and if that is so they "getWorld().addObject(new zandzak()x,y,);" must be executed. Now i can just go around in my field and place new zandzak everywhere. It has to match the two conditions so that it runs the getWorld()....
danpost danpost

2013/1/9

#
Then you can use:
if (Greenfoot.isKeyDown("space") && 
    getOneObjectAtOffset(0, -1, kijkbuuren.class) != null && 
    getOneObjectAtOffset(0, 0, zandzak.class) == null)
        getWorld().addObject(new zandzak(),getX(), getY());
zakariaonlyy zakariaonlyy

2013/1/9

#
It worked perfectly, thank you :)
You need to login to post a reply.