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

2015/3/21

Run a method from another class?

h123n h123n

2015/3/21

#
Hi, I have created a scrolling game in which there is a person(called Toby) and a wall block(Block) I am trying to make it so that upon collision of the block and Toby, the Block tells Toby they have collided, so Toby moves backward. The problem is that Toby is a subclass of Scroll-actor, so when I try and call a method from the Block, it gives me an error. Here is the Block code:
import greenfoot.*;

/**
 * Write a description of class Block here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Block extends ScrollActor
{
    /**
     * Act - do whatever the Block wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       Toby toby;
       toby = getOneIntersectingObject(ScrollActor.class);
       if (toby != null
       {
           toby.Collide();
       }     
    }
I don't know if you would need this, but here is the Toby code:
import greenfoot.*;

/**
 * An demo class which is meant to be a camera follower.
 * It moves to face your mouse cursor, and it can move
 * back and forward.
 * 
 * @author Sven van Nigtevecht
 * @version 1.0
 */
public class Toby extends ScrollActor
{
    /** The number of cells we move forward and backword */
    int speed = 5;
    
    /**
     * Move to face the mouse,
     * and listen to the up and down keys.
     */
    public void act()
    {
        if (Greenfoot.isKeyDown("a")) {
            getWorld().setCameraLocation(getWorld().getCameraX() -speed,getWorld().getCameraY());
        }
        if (Greenfoot.isKeyDown("d")) {
            getWorld().setCameraLocation(getWorld().getCameraX() +speed,getWorld().getCameraY());
        }        
        if (Greenfoot.isKeyDown("w")) {
            getWorld().setCameraLocation(getWorld().getCameraX(),getWorld().getCameraY() -speed);
        }
        if (Greenfoot.isKeyDown("s")) {
            getWorld().setCameraLocation(getWorld().getCameraX(),getWorld().getCameraY() +speed);
        }
    }
    public void Collide()
    {
        speed-=5;
    }
Please tell me how I can do this, or if there is an easier way of executing the method.
Super_Hippo Super_Hippo

2015/3/21

#
Your code has (at least) two errors. First, the closing bracket in line 19 is missing. Second, 'getOneIntersectingObject(ScrollActor.class);' returns a ScrollActor object, but toby is a variable of type Toby. A Toby is always a ScrollActor, but a ScrollActor is not always a Tobi.
Toby toby = getOneIntersectingObject(Toby.class);
if (toby != null)
{
    toby.Collide();
}
By the way, the collide method probably doesn't do what it should. Right now, it reserves the movement. Usually it is better to check for collision in the moving class (in your case Toby) after it moved and set it back if it did.
h123n h123n

2015/3/21

#
Thank you for telling me how to make it work. With the brackets, I fixed that and with the scroll actor, I changed it but now it says: incompatible types: greenfoot.Actor cannot be converted to Toby. Toby is a subclass if scroll actor. How do I fix this?
Super_Hippo Super_Hippo

2015/3/21

#
What is the code you use right now?
h123n h123n

2015/3/21

#
It is this:
import greenfoot.*;

/**
 * Write a description of class Block here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Block extends ScrollActor
{
    /**
     * Act - do whatever the Block wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       Toby toby;
       toby = getOneIntersectingObject(Toby.class);
       if (toby != null)
       {
           toby.Collide();
       }     
    }
}
danpost danpost

2015/3/21

#
Line 1 of the code Super_Hippo provided should be:
Toby toby = (Toby)getOneIntersectingObject(Toby.class);
h123n h123n

2015/3/21

#
Thanks so much for your support, it works fine now!
Super_Hippo Super_Hippo

2015/3/21

#
Right. The error just confused me. Didn't know 'getOneIntersectingObject' is already casted to an Actor.
danpost danpost

2015/3/21

#
@Super_Hippo, what bracket was missing? which code set were you referring to? I do not see it (although, I see that neither class has been closed out at the end with a closing curly bracket).
h123n h123n

2015/3/21

#
He is referring to the bracket in my clock class. it is a ) that is missing at the end of (toby !=null
h123n h123n

2015/3/21

#
sorry my block class - typo
danpost danpost

2015/3/21

#
Wow, I totally overlooked that. I was looking for a missing curly bracket (I though the '(' and ')' were referred to as braces -- not brackets).
Super_Hippo Super_Hippo

2015/3/21

#
Interesting, I didn't know that there is a difference between brace and bracket, nice to know. :)
danpost danpost

2015/3/21

#
According to Wikipedia, I had it backwards; however, it does state that 'brackets' typically refer to different symbols depending on the geographic location of use. The only one it, as a standard, considers a 'brace' IS the curly bracket; and 'bracket' is a general term for all four sets of punctuation: (1) '( )' - round brackets or parentheses; (2) '??' - square brackets (cannot seem to show in this content pane); (3) '{ }' - curly brackets or braces; (4) '< >' - angle brackets or chevrons.
You need to login to post a reply.