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

2018/1/25

Problem Accessing Object from another actor

Yister Yister

2018/1/25

#
I tried accessing a boolean method from one of my actor classes 'P1Sword' to use in my actor 'P1'. However when I followed the tutorial #6, I am having trouble adding the new object I created from the P1Sword class into the world. Any Suggestions?
import greenfoot.*;

public class Yeggland extends World
{
    
    private P1Sword swordTouch;
    
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public Yeggland()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1400, 800, 1);
        addObject (new Ground(), 700, 740);
        swordTouch = new P1Sword();
        addObject (new swordTouch, 480, 675);
        addObject (new P2Sword(), 920, 675);
        addObject (new P1(), 400, 675);
        addObject (new P2(), 1000, 675);
    }
    
    public P1Sword checkTouch()
    {
        return swordTouch;
    }
}
Here is also the code for the actor 'P1' for which im trying to get the 'P1Sword' method into
public void checkTouch()
    {
        Yeggland yegglandWorld = (Yeggland) getWorld();
        P1Sword p1Sword = yegglandWorld.checkTouch();
        p1Sword.swordTouch();
    }
    
    public void swordBump()
    {
        checkTouch();
    }
Vercility Vercility

2018/1/25

#
What exactly is your problem lol. "Having trouble" is not an accurate description.
xbLank xbLank

2018/1/25

#
Are you maybe looking to pass the boolean value from one actor to another?
danpost danpost

2018/1/25

#
Remove 'new' from line 18.
You need to login to post a reply.