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

2016/5/17

Making an enemy character stay on the screen until killed.

xlRenn xlRenn

2016/5/17

#
Hello, I'm currently making a Slayin' type game where the background is non-scrolling, and the player/enemies have a static y value. The enemies currently move left from spawn, but I would like them to change direction when hitting the edge of the world. I came up with:
public void act() 
    {
        move(-movespeed);
        
        if(atWorldEdge() == true)
        {
            
            World world;
            world = getWorld();
            getImage().mirrorHorizontally();
            move(movespeed);     
            }
        }
The problem is though, once it reaches the end of the world, it's just stuck there because the move(-movespeed) from act is still running. So I tried this:
public void act() 
    {
        setImage(myGif.getCurrentImage());
        if(getX()<580 && getX()>25)
        {
          move(-movespeed);
          countdown --;
        }
        if(countdown <= 1 && atWorldEdge() == true)
        {
            respawntime = 0;
            World world;
            world = getWorld();
            getImage().mirrorHorizontally();
            move(movespeed);
             
             
            }
        }
but probably just made the problem even worse, because now it'll never reach the other end of the world. So I also thought about having something like
if (getX() == valueofleftedge)
{
  move(movespeed);
}else if(getX() == valueofrightedge)
{
  move(-movespeed);
}
but this doesn't work either, because it only works for that set point. Any suggestions on how to alternate the directions?
AreebRaza AreebRaza

2016/5/17

#
I'm making a game just like you but in my game its where the player goes back as he tries to move inside the Wall and it works, it might work with you.
          public void act(){
                move(movespeed);
                checkBoundryWalls();
          } 
          public void checkBoundryWalls(){
           if(getOneObjectAtOffset(0, 0, RightWall.class) != null) {  
               move(movespeed);
           }else if(getOneObjectAtOffset(0, 0, LeftWall.class) != null &&) {  
               move(-movespeed);
           }
valdes valdes

2016/5/17

#
your variable movespeed, looks something like this:
private int movespeed = -5; // initially to the left, negative value
int your act() method or in other method called by act()
        if (this.isAtEdge()) {
            movespeed *= -1;
            getImage().mirrorHorizontally();
        }
        setLocation(getX() + movespeed, getY());
You need to login to post a reply.