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.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | // 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; } |
1 2 3 | Portal portal = new Portal(); addObject(portal, 50 , 50 ); addObject( new Portal(portal), 550 , 350 ); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /** 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 ; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | 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()); } } } |
1 2 3 4 5 6 7 8 9 10 | if (isTouching(Portal1. class )) { if ( ! transferred ) { Portal1 portal = ((Portal1)getOneIntersectingObject(Portal1. class )).getLinkedPortal(); setLocation(portal.getX(), portal.getY()); transferred = true ; } } else transferred = false ; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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()); } } } |
1 2 3 4 5 6 7 8 | 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);} |
1 2 3 4 5 6 7 8 9 10 | private int index; public Portal( int i) { index = i; } public Portal getLinkedPortal() { return ((MyWorld) getWorld()).getPortal((index+ 1 )% 2 ); } |
1 2 3 4 5 6 | 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()); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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 ; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | // 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) } } |