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

2017/5/7

How can I assign a method to One Specific Object of one class?

LoopingSample LoopingSample

2017/5/7

#
Hey there, I'm very new to Greenfoot and have a very basic problem that surely everyone of you will be able to solve in a minute: In my game you are able to push crates (called Crate.class) so that you can safely exit a labyrinth of those. Some rules are that you can only push one crate at once, if there are two behind each other it won't be possible to push them. Furthermore if there's a brick (Cobble.class) which is completely immovable. Means that you can't push a crate if there's a brick behind it. Now my problem is pushing the crates. What I've done so far is:

public class Player extends Actor
{
    int rotation;
    public static int delay;
    public static int hori;
    public static int verti;
    int count;
    

    
    
    // Methoden der Klasse Player (yeah I'm German)
    
    
    
    void moveRight() {
        if (Greenfoot.isKeyDown("right")) {
            setRotation(90);
            hori = 1;
            verti = 0;
            blocked();
            pushCrate();
            setLocation(getX() + hori, getY());
            
            for (count = 1; count <= 20; count++)
            {
            
                if (!Greenfoot.isKeyDown("right")) {
                    count = 120;
                }
                
                Greenfoot.delay(1);
                
            }
        }
    }

    
    void moveLeft() {
        if (Greenfoot.isKeyDown("left")) {
            
            
            setRotation(270);
            hori = -1;
            verti = 0;
            blocked();
            pushCrate();
            setLocation(getX() + hori, getY() + verti);
            
            
            
            for (count = 1; count <= 20; count++)
            {
                
                if (!Greenfoot.isKeyDown("left")) {
                    count = 120;
                } 
                
                Greenfoot.delay(1);
                
            }
        }
    }
    
    
    void moveUp() {
        if (Greenfoot.isKeyDown("up")) {
            setRotation(0);
            hori = 0;
            verti = -1;
            blocked();
            pushCrate();
            setLocation(getX(), getY() + verti);
            
            for (count = 1; count <= 20; count++)
            {
            
                if (!Greenfoot.isKeyDown("up")) {
                    count = 120;
                }
                
                Greenfoot.delay(1);
                
            }
        }
    }
    
    
    void moveDown() {
        if (Greenfoot.isKeyDown("down")) {
            setRotation(180);
            hori = 0;
            verti = 1;
            blocked();
            pushCrate();
            setLocation(getX(), getY() + verti);
            
            for (count = 1; count <= 20; count++)
            {
            
                if (!Greenfoot.isKeyDown("down")) {
                    count = 120;
                }
                
                Greenfoot.delay(1);
                
            }
        }
    }
   
    
    void blocked() {
        
        if (getOneObjectAtOffset(hori, verti, Cobble.class) !=null) { 
            hori = 0;
            verti = 0;
        

            
        }
    }
    

    
    
    public void pushCrate() {
        Actor crate = getOneObjectAtOffset(hori, verti, Crate.class);
        
        int var = crate;
        
        if  (crate != null) {
            
            
            if (getOneObjectAtOffset(hori*2, verti*2, Cobble.class) !=null) {
                hori = 0;
                verti = 0;
            }
            
            else {
                
                ((Crate)getWorld().getObjects(Crate.class).get(0)).pushCrate(); //STUCK HERE! Got this one from the internet and it doesn't work out properly!
                
                
                
                
                
                
                
                

                
            }
             
        }
    }
    
    
    
    // ACT-METHODE
    
    
    public void act() 
    {
        
        moveRight();
        moveLeft();
        moveUp();
        moveDown();
    
    }
and going on to the class Crate:


public class Crate extends Actor
{
    public static boolean blocked;
    
    
    
    void pushCrate() {
         if (getOneObjectAtOffset(Player.hori, Player.verti, Crate.class) !=null) {
             Player.hori = 0;
             Player.verti = 0;
            }
         
         
         setLocation(getX() + Player.hori, getY() + Player.verti);
                
    }
    
    
    
    
    
    
    void blockedCrate() {
        if (getOneIntersectingObject (Cobble.class) != null) {
            
            
            setLocation(getX() - Player.hori, getY() - Player.verti);
            blocked = true;
            
            
            ((Player)getWorld().getObjects(Player.class).get(0)).moveBack();
            
        }
    }
    
    
    public void act() 
    {
        
        blockedCrate();
        
    }
}

That piece of code up there where I commented "STUCK HERE" should help me referring to the method of another class, in our case Crate.class. Unfortunately I don't really know how to greenfoot and what it really means. Now I found out that the get(0) refers to the first Object of Cobble.class which has been created. When I insert a second crate and I try pushing that one, the first one moves while my Player is going inside the second crate. So it detects the object as one of the class Crate, but moves the first one created. When I change the number of get() to 1, only the second crate spawned is movable. Is there a way of referring to the latest detected object from Crate.class? The method getOneObjectAtOffset() should return one specific object, shouldn't it? Can I somehow refer to this detected object and make it move? I hope I could explain properly what my issue is; I'm neither good at English nor at Greenfoot. If you need more detail please tell me. Greetings LoopingSample
danpost danpost

2017/5/7

#
You already got a reference to the crate that the player will attempt to push at line 127. So, you do not need to get a list of crates in the world and get one that you are not sure is the one that the player will attempt to push. Before fixing the player movement, I think we should fix the Crate class. A crate is an object that does nothing by itself. It just sits there in the world until something else does something to it (that is until being pushed by a player). It is the player that performs the action and re-locates a crate that can move. Therefore, the Crate class should be emptied of all fields and methods:
import greenfoot.*;

public class Crate extends Actor
{
}
The class can be simplified even more -- to this:
public class Crate extends greenfoot.Actor {}
Now, the 'blocked' method is not yet covering all the reasons to not move (or, to reset the values of 'hori' and 'verti' back to zero). Let's add the other conditions for doing that to it:
void blocked()
{
    if (getOneObjectAtOffset(hori, verti, Cobble.class) != null || (getOneObjectAtOffset(hori, verti, Crate.class) != null &&
          (getOneObjectAtOffset(hori*2, verti*2, Cobble.class) != null || getOneObjectAtOffset(hori*2, verti*2, Crate.class) != null)))
    {
        hori = 0;
        verti = 0;
    }
}
This says the player is blocked if it hits brick or an immovable crate (a crate that hits brick or another crate). Now, the 'pushCrate' method can simply be this:
void pushCrate()
{
    Actor crate = getOneObjectAtOffset(hori, verti, Crate.class);
    if (crate != null) crate.setLocation(crate.getX()+hori, crate.getY()+verti);
}
LoopingSample LoopingSample

2017/5/8

#
Ok thanks already. I'll try it out in a couple of hours and post whether it worked. I appreciate the time you take yourself to answer all these questions here - respect!
LoopingSample LoopingSample

2017/5/8

#
OMG I love you right now - it worked! Thanks so much, saved me and my friend a couple of hours of thinking. Sometimes it's easier than it seems :D
You need to login to post a reply.