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

2016/9/24

Making objects stay on top of other objects

ymym ymym

2016/9/24

#
So my game is basically this penguin (actor) going to another side by hopping on moving clouds (those that move to the right are Cloud and those that move to the left are LeftCloud). I have been trying to make the penguin stay on the cloud once it's on it without me having to control it using type keys, but nothing seems to work. Any ideas?
public void act() 
    {
        checkKeys();        
        checkFall();
        checkCloud();
} 


public void checkCloud() {
        for(int i = 0; i < 30; i++) {
            Actor cloudyR = getOneObjectAtOffset(0,i-5, Cloud.class);
            if(cloudyR != null) { 
                setLocation(cloudyR.getX(), cloudyR.getY());
                break;
            }
            
            Actor cloudyL = getOneObjectAtOffset(0,i-5, CloudLeft.class);
            if(cloudyL != null) {
                setLocation(cloudyL.getX(), cloudyL.getY());
                break;
            }
        }
    
}
            
            Actor cloudyL = getOneObjectAtOffset(0,i-5, CloudLeft.class);
            if(cloudyL != null) {
                setLocation(cloudyL.getX(), cloudyL.getY());
                break;
            }
        }
Super_Hippo Super_Hippo

2016/9/24

#
I guess both Cloud classes are pretty much the same (only moving to the opposite site). If this is the case, then only use one class for both. You could try this in the Cloud class:
private int direction; //1=moving right, -1=moving left

public Cloud(int dir)
{
    direction = dir;
}

public void act()
{
    setLocation(getX()+dir, getY());
    Actor penguin = getOneIntersectingObject(Penguin.class);
    if (penguin != null)
    {
        penguin.setLocation(penguin.getX()+dir, penguin.getY());
    }
}
Pass the direction to the Clouds when creating them: e. g. in addObject(...
//right moving cloud
new Cloud(1)

//left moving cloud
new Cloud(-1)
ymym ymym

2016/9/25

#
I tried, but I get an error under 'dir' in the Act method in this code, saying 'cannot find variable' - why does this happen? I'm not quite sure about the new public Cloud method either.
private int speed = 3;
    private int leftTurn = 50;
    private int rightTurn = 700;
    private int direction;//1= moving right, -1= moving left
    
    GreenfootImage Cloud = new GreenfootImage("cloud2.png");
    
    public Cloud() {
        Cloud.scale(150,70);
        setImage(Cloud);
    } 
    
    public Cloud(int dir){
        direction=dir;
    }
      
    public void act() 
    {
        setLocation(getX()+dir, getY());
        Actor penguin = getOneIntersectingObject (Penguin.class);
        
        if (penguin!= null) {
            penguin.setLocation(penguin.getX()+dir, penguin.getY());
        }
    
    }
Super_Hippo Super_Hippo

2016/9/25

#
It should be 'direction' and not 'dir' in all cases in the act-method. Looks like I messed this up.
You need to login to post a reply.