I'm making a Portal game and I couldn't figure out how to do what is titled. For example if the character touches Portal1, they're sent to the X and Y of Portal2.
// instance field for paired Portal object
private Portal linkedPortal;
public Portal() {} // for creating the first of two linked portals
public Portal(Portal counterpart) // for creating the second of two linked portalss
{
linkedPortal = counterpart;
counterpart.linkedPortal = this;
}
public Portal getLinkedPortal()
{
return linkedPortal;
}Portal portal = new Portal(); addObject(portal, 50, 50); addObject(new Portal(portal), 550, 350);
/** in class of character */
// instance field for transfer flag
private boolean transferred;
// in act (or method called by act)
if (isTouching(Portal.class))
{
if ( ! transferred ) // (read as 'if not transferred' -- same as ' if (transferred == false) '
{
Portal portal = ((Portal)getOneIntersectingObject(Portal.class)).getPairedPortal();
setLocation(portal.getX(), portal.getY());
transferred = true;
}
}
else transferred = false;private Portal1 linkedPortal;
private boolean transferred;
public void act()
{
shootPortal1();
limit();
}
public Portal1()
{
}
public Portal1(Portal1 counterpart)
{
linkedPortal = counterpart;
counterpart.linkedPortal = this;
}
public Portal1 getLinkedPortal()
{
return linkedPortal;
}
private void limit()
{
int n = getWorld().numberOfObjects();
if(n > 16)
{
getWorld().removeObject(this);
}
}
public void shootPortal1()
{
Portal1 portal = new Portal1();
MouseInfo mouse = Greenfoot.getMouseInfo();
if(Greenfoot.mouseClicked(this))
{
getWorld().addObject(portal, Greenfoot.getMouseInfo().getX(), Greenfoot.getMouseInfo().getY());
if (mouse.getButton() == 3)
{
getWorld().addObject(new Portal1(portal), Greenfoot.getMouseInfo().getX(), Greenfoot.getMouseInfo().getY());
}
}
}if (isTouching(Portal1.class))
{
if ( ! transferred )
{
Portal1 portal = ((Portal1)getOneIntersectingObject(Portal1.class)).getLinkedPortal();
setLocation(portal.getX(), portal.getY());
transferred = true;
}
}
else transferred = false;public void shootPortal1()
{
Portal1 portal = new Portal1();
MouseInfo mouse = Greenfoot.getMouseInfo();
if(Greenfoot.mouseClicked(this))
{
if (mouse.getButton() == 1)
{
getWorld().addObject(portal, Greenfoot.getMouseInfo().getX(), Greenfoot.getMouseInfo().getY());
}
}
if(Greenfoot.mouseClicked(this))
{
if (mouse.getButton() == 3)
{
getWorld().addObject(new Portal1(portal), Greenfoot.getMouseInfo().getX(), Greenfoot.getMouseInfo().getY());
}
}
}private Portal[] portals = {new Portal(0), new Portal(1)};
public void addPortal(int i, int x, int y)
{
addObject(portals[i], x, y);
}
public Portal getPortal(int i) {return portals(i);}private int index;
public Portal(int i)
{
index = i;
}
public Portal getLinkedPortal()
{
return ((MyWorld) getWorld()).getPortal((index+1)%2);
}if (Greenfoot.mousePressed(null))
{
MouseInfo me = Greenfoot.getMouseInfo();
if (me.getButton() == 1) ((MyWorld) getWorld()).addPortal(0), me.getX(), me.getY());
else if (me.getButton() == 3) ((MyWorld) getWorld()).addPortal(1), me.getX(), me.getY());
}Portal portal = (Portal) getOneIntersectingObject(Portal.class);
if (portal != null)
{
if (!transferred)
{
Portal linked = portal.getLinkedPortal();
if (linked.getWorld() != null)
{
setLocation(linked.getX(), linked.getY());
transferred = true;
}
}
}
else transferred = false;// 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)
}
}