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

2018/4/18

Have an Actor Move Back and Forth between Walls

ann241 ann241

2018/4/18

#
Hi, I'm making a game where I want "monsters" (Monsters.class) to be moving back and forth between walls. The wall is another class of object (Wall.class). My problem is that I can't have the monster change direction when it hits a wall without the image flipping upside down. Right now my code in the monster's act method looks like this: if(!isTouching(Wall.class)) move(1); else if (isTouching(Wall.class)) { turn(180); move(5); } I was also wondering if there's a way to make it so that the monster doesn't have to move a greater distance (5 currently) after it turns, because right now if I make it move just 1 or even 3 pixels, the monster will still be touching the wall and end up turning indefinitely in one spot. Any help would be appreciated!
danpost danpost

2018/4/18

#
It would help to get a reference to any intersecting Wall object and use its position with respect to itself as a condition in turning. In other words, if moving left and intersecting wall is to the left, turn around; likewise, if moving right and intersecting wall is to the right, turn around. This should prevent the indefinite turning in one spot as well as not having to move off any walls at all. As far as the image, it will need adjusted, or changed, any time the turning around is implemented. You could mirror the image vertically; but there might be some loss in detail each time it is mirrored. Best is to create a new image and mirror it vertically if facing left before setting it to the actor.
WeNeedHelp22 WeNeedHelp22

2018/4/18

#
DanPost & ann241 please help me and my partner
nolttr21 nolttr21

2018/4/18

#
@WeNeedHelp22 you should create a new discussion thread for the thing you need help on
danpost danpost

2018/4/18

#
nolttr21 wrote...
@WeNeedHelp22 you should create a new discussion thread for the thing you need help on
That was already done prior to the unnecessary injection into this discussion thread.
You need to login to post a reply.