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

2014/11/3

Move Object

ds06 ds06

2014/11/3

#
Hello, im failry new to java and greenfoot and want to know move an Object. I have one Worldclass: Level1 and 2 Actor subclasses: Hero and Ground. This is my code: World:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class Level1 extends World
{
    public Actor player = new Hero();
    public Actor mainground = new Ground();
    
    public Level1()
    {    
        super(600, 400, 1); 
        addObject (player, 60, 336);
        addObject(mainground, 300, 400);
    }
}
Hero:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class Hero extends Actor
{
   
    
    public void act() 
    {
      
      if (Greenfoot.isKeyDown("left")) {
           move(-5);
           int playerx= getX();
           if (playerx<=100)
            {
               //HOW TO MOVE THE OBJECT MAINGROUND FROM HERE?
                
            }
           
           
        }else if (Greenfoot.isKeyDown("right")) {
           move(5);
           int playerx= getX();
           if(playerx>=500){
               
               //HOW TO MOVE THE OBJECT MAINGROUND FROM HERE?
               
               
               
            }
           
        } 
    }    
}
You see my comments? At this place i want to change the x position of the object mainground which was created at the beginning in the Lelvel1 worldclass. thanks in advance
Super_Hippo Super_Hippo

2014/11/3

#
Actor g = ((Level1)getWorld()).mainground;
g.setLocation(g.getX()+dx,g.getY());
Change 'dx' to the value you want to move.
ds06 ds06

2014/11/3

#
Wow great, thank you very much:) It works now.
You need to login to post a reply.