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

2016/5/14

Move Actor1, if Actor2 moves and he is up

Meh Meh

2016/5/14

#
Hi, I have a problem. How can I move any Actor1 (no matter if .get(0) or .get(1) and so on) if it is on the moving Actor2?
//actor1IsUp() is a method, that proves if Actor1 is up
//this Actor(Actor2) moves automatically and it's direction changes automatically
if(actor1IsUp() & direction == "left")
{
            Actor actor1 = getWorld().getObjects(Actor1.class).get(0);
            actor1.setLocation(actor1.getX() - 1, actor1.getY());
}
else if(actor1IsUp() & direction == "right")
{
            Actor actor1 = getWorld().getObjects(Actor1.class).get(0);
            actor1.setLocation(actor1.getX() + 1, actor1.getY());
}
How can I move the Object, that is on Actor2 and not the first one in the list(so something else instead of .get(0))
danpost danpost

2016/5/14

#
Just have Actor2 tell any Actor1 object to move when it detect one on it:
// in Actor2 class
Actor1 actor1 = (Actor1) getOneIntersectingObject(Actor1.class);
if (actor1 != null) actor1.move();
where you add a method in the Actor1 class called 'move' that controls the movement of an Actor1 object when an Actor2 object detects it. The name of the method can be more specific to better describe the move (maybe 'moveOff2' or 'moveBy2').
Meh Meh

2016/5/15

#
ah, thank you. Is actor1.setLocation(getX() + 1, getY()); also possible? and does it make a difference if I write Actor actor1 or Actor1 actor1?
danpost danpost

2016/5/15

#
Meh wrote...
ah, thank you. Is actor1.setLocation(getX() + 1, getY()); also possible?
Well, it is possible; but not quite what you want to do. This line, as given, if replacing 'actor1.move();', will keep the Actor1 object one pixel from the Actor2 object. To keep it moving away, you would need to use this:
actor1.setLocation(actor1.getX()+1, actor1.getY());
keeping the move with respect to the location of the Actor1 object and not that of the Actor2 object.
and does it make a difference if I write Actor actor1 or Actor1 actor1?
If you used my line 3, calling a method in the Actor1 class (like my 'move' or 'moveBy2'), then yes. If replacing it with calling a method in the Actor class (like 'setlocation'), then no.
Meh Meh

2016/5/15

#
Ok, thank you very much! your first line is what I meant, I just failed at my first line
You need to login to post a reply.