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

2020/1/15

How can I move all Objects in World except for the Player

alkwmd21ij alkwmd21ij

2020/1/15

#
Just as in the title, I wish to move everything but the Player, so that it stays centered- just like 2D Zelda or Pokémon. I tried using the getObjectsInRange() method for this, but can't manage to move each of the returned objects.
 
   for(Object o : getObjectsInRange(640, null)) {
        o.setLocation(o.getX(), o.getY() - 1);
    }
It says that it cannot find the methods getX(), getY() and setLocation(), which is understandable because java.lang.Object does not have those methods. However, if I try to change the type of o to something other than "Object" (e.g. "Actor o : getObjectsInRange(640, null)), the problem is that getObjectsInRange() returns a list of type Object.
Super_Hippo Super_Hippo

2020/1/15

#
Try this:
        int dx=0, dy=0;
        if (Greenfoot.isKeyDown("up")) dy--;
        if (Greenfoot.isKeyDown("down")) dy++;
        if (Greenfoot.isKeyDown("left")) dx--;
        if (Greenfoot.isKeyDown("right")) dx++;
        
        if (dx != 0 || dy != 0)
        {
            for (Actor a : getWorld().getObjects(Actor.class))
            {
                if (a != this) a.setLocation(a.getX()-dx, a.getY()-dy);
            }
        }
alkwmd21ij alkwmd21ij

2020/1/15

#
Thanks!
You need to login to post a reply.