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

2017/2/22

How to send an actor to another actor

1
2
Super_Hippo Super_Hippo

2017/2/23

#
Edit: In the shooting lines in my code, the ) right after 0/1 have to be removed.
DPK DPK

2017/2/24

#
danpost wrote...
DPK wrote...
My rules are 2 portals is the limit, one spawned from left mouse click and the other from right mouse click.
Do not create and add portals from the Portal class. Create the portals in your world constructor and keep references to them in the World object:
// instance field in World subclass
private Portal portal;

// in World subclass constructor
portal = new Portal();
new Portal(portal);

// in 'act' method of World subclass
if (Greenoot.mouseClicked(null))
{
    MouseInfo mouse = Greenoot.getMouseInfo();
    if (mouse.getButton() == 1)
    {
        if (portal.getWorld() != this)
        {
            addObject(portal, mouse.getX(), mouse.getY());
        }
        else // (optional)
        {
            portal.setLocation(mouse.getX(), mouse.getY());
        } // (end optional)
    }
    if (mouse.getButton() == 3)
    {
        Portal p = portal.getLinkedPortal();
        if (p.getWorld() != this)
        {
            addObject(p, mouse.getX(), mouse.getY());
        }
        else // (optional)
        {
            p.setLocation(mouse.getX(), mouse.getY());
        } // (end optional)
    }
}
One problem, my world class doesn't have getLinkedPortal(), and when inserted it doesn't have counterpart, since counterpart is in the portal's constructor.
Nosson1459 Nosson1459

2017/2/24

#
What do you mean doesn't have getLinkedPortal? If the method is in Portal or wherever it's supposed to be by now, as long as it's in portal or one of it's parent classes the subclass of World should be able to find it.
DPK DPK

2017/2/24

#
Nope, the world subclass doesn't seem to find it. I put getLinkedPortal into the world subclass along with the portal subclass, but of course it still doesn't work.
danpost danpost

2017/2/24

#
DPK wrote...
Nope, the world subclass doesn't seem to find it. I put getLinkedPortal into the world subclass along with the portal subclass, but of course it still doesn't work.
Please show what you did, instead of trying to explain it.
Nosson1459 Nosson1459

2017/2/24

#
The method getLinkedPortal() should not be in your World subclass because your World subclass doesn't have a linked portal, the getLinkedPortal method should go in the class that has a linked portal (Portal).
danpost wrote...
Please show what you did, instead of trying to explain it.
DPK DPK

2017/2/24

#
In the World Subclass (MyWorld):
public MyWorld()
    {    
        super(800, 800, 1); 
        testChamber1();
        showScore();
        endGame();
        portal = new Portal1();
        new Portal1(portal);
    }
    
    
    public void act()
    {
        if(Greenfoot.mouseClicked(null))
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            if(mouse.getButton() == 1)
            {
                if(portal.getWorld() != this)
                {
                    addObject( portal, mouse.getX(), mouse.getY());
                }
            }
            if(mouse.getButton() == 3)
            {
                Portal1 p = getLinkedPortal();
                if(p.getWorld() != this)
                {
                    addObject( p, mouse.getX(), mouse.getY());
                }
            }
        }
    }

public Portal1 getLinkedPortal()
    {
        return linkedPortal;
    }
In the Portal subclass (Portal1):
public Portal1(Portal1 counterpart)
    {
        linkedPortal = counterpart;
        counterpart.linkedPortal = this;
    }
    
    public Portal1 getLinkedPortal()
    {
        return linkedPortal;
    }
danpost danpost

2017/2/24

#
Your line 26 is not what I supplied to you. Recheck what I gave -- and remove lines 35 through 38 from the class.
DPK DPK

2017/2/24

#
So now, I'm receiving the same error as before
if (isTouching(Portal1.class))
        {
            if ( ! transferred ) 
            {
                Portal1 portal = ((Portal1)getOneIntersectingObject(Portal1.class)).getLinkedPortal();
                setLocation(portal.getX(), portal.getY());
                transferred = true;
            }
        }
This is in my character class (TestSubject), the error is line 6, which is a NullPointerException error. Greenfoot accepted your code from earlier:
if(Greenfoot.mouseClicked(null))
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            if(mouse.getButton() == 1)
            {
                if(portal.getWorld() != this)
                {
                    addObject( portal, mouse.getX(), mouse.getY());
                }
            }
            if(mouse.getButton() == 3)
            {
                Portal1 p = portal.getLinkedPortal();
                if(p.getWorld() != this)
                {
                    addObject( p, mouse.getX(), mouse.getY());
                }
            }
        }
This is in the world subclass (MyWorld)
DPK DPK

2017/2/24

#
Wait! I got the code to work, now the only problem is it only teleports once. Can I change this?
DPK DPK

2017/2/24

#
Wait, I got that to work too.
DPK DPK

2017/2/27

#
Ok, the game is just about finished, thank you all for helping me!
Nosson1459 Nosson1459

2017/2/27

#
Waiting to see it uploaded... .
You need to login to post a reply.
1
2