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

2012/7/16

Bringing an object under another?

nooby123 nooby123

2012/7/16

#
So, I'll start explaining with an example. You add an object(lets call it A) and then you add another object(B). When you drag A to B, B overlaps A, because it was added into the world last, right? I want to prevent this. In my program, there is an enemy spawner in the water. If you spawn an enemy, it looks like the enemy is infront of the water and not in it. What I would like to know is that, is there any way of bringing the enemy under the water? If so, how?
erdelf erdelf

2012/7/16

#
You could use this:
1
setPaintOrder(Water.class, Enemy.class);
DonaldDuck DonaldDuck

2012/7/16

#
You could have the water update itself when it sees an enemy by having it create a new instance of it then removing it from the water class. This tends to get fairly complicated and likely quite laggy though.
erdelf erdelf

2012/7/16

#
This would cause an endless loop. let's try this: enemy on water, water creates new one, deletes itself enemy on new water, new water creates new water, deletes itself
nooby123 nooby123

2012/7/16

#
Thanks! DonaldDuck, I'm not that good at programming, so thanks, but no thanks!
danpost danpost

2012/7/17

#
This would cause an endless loop, IF you processed this in an act method or a method it calls. On the other hand, if this was processed in the enemy's 'addedToWorld' method, it would do it just once! Also, you do not have to create a new water instance and delete the old one. You just need to remove the one you have and add the same one back. In the enemy class, add this method
1
2
3
4
5
6
7
8
9
public void addedToWorld(World world)
{
    Water water = (Water) getOneIntersectingObject(Water.class);
    if (water == null) return;
    int x = water.getX();
    int y = water.getY();
    world.removeObject(water);
    world.addObject(water, x, y);
}
erdelf erdelf

2012/7/17

#
I think the world has more then water at the beginning, if I understand this correctly, you only check at the begin.
DonaldDuck DonaldDuck

2012/7/17

#
danpost wrote...
This would cause an endless loop, IF you processed this in an act method or a method it calls. On the other hand, if this was processed in the enemy's 'addedToWorld' method, it would do it just once! Also, you do not have to create a new water instance and delete the old one. You just need to remove the one you have and add the same one back.
Touche. Thanks for the clean-up there danpost :) It was late at night and I wasn't thinking properly.
danpost danpost

2012/7/17

#
Actually, if your scenario allows either A or B to be dragged, then you would want to do the following:
1
2
3
4
5
6
7
8
9
10
11
boolean dragging = false;
// the above is a field in the class
// the below is in the act method (or a method it calls)
if (!dragging && Greenfoot.mousePressed(this))
{
    dragging = true:
    int x = getX(), y = getY();
    World world = getWorld();
    world.removeObject(this);
    world.addObject(this, x, y);
}
If you do this for all classes with draggable objects, your dragging object will always appear on top! Of course, the above only checks for the beginning of a possible dragging. You will need more code for the dragging itself 'if (dragging && Greenfoot.mouseMoved(null))', and for the releasing of the object 'if (dragging && Greenfoot.mouseClicked(null))'. The first one to keep the object at the location of the move (drag) and the other to end the dragging (drop), where 'dragging' should be set back to false.
steved steved

2012/7/17

#
If you want the Enemy to go underneath the water what you could do is put a variable in the world and once the water adds and deletes itself (reseting) it would make the variable change. Then before it tried to reset it would check the varible and if it was off it would not do anything.
You need to login to post a reply.