I created 2 teleports, the Actor should come to one and appear at the other, the same reverse. One port works as expected, but the other doesn't let the Actor move anywhere. I noticed that if the port works depends on if I create it first. Maybe anyone knows what might be the problem? This is my game: https://www.greenfoot.org/scenarios/27113
Here is the code of the Portal
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
This is how I initialize the portals in the World:
public class Portal extends Actor
{
Portal otherPortal;
public Portal (){
this.setImage(
new GreenfootImage("c:\\User\\Documents\\Детские уроки\\Hole2.png"));
this.getImage().scale(40, 40);
}
public Portal (Portal another)
{
this();
this.otherPortal = another;
this.otherPortal.setPortal(this);
}
public void act(){
Actor m = this.getOneIntersectingObject(Mouse.class);
if(m != null){
m.setLocation (this.otherPortal.getX()+10 , this.otherPortal.getY() + 10);//(Math.abs(m.getX() - getWorld().getWidth()), Math.abs(m.getY() - getWorld().getHeight()));
m.move(25);
}
}
public void setPortal(Portal portal){
this.otherPortal = portal;
}
} private void prepare()
{ // one version
// Portal portal2 = new Portal();
// addObject(portal2,44,61);
// Portal portal1 = new Portal(portal2);
// addObject(portal1,700,565);
//another version
Portal portal = new Portal();
this.addObject(portal, 80, 300); // 'this' refers to the world instance being created and is optional
// add second portal to world using second constructor to link the portals together
portal = new Portal(portal);
this.addObject(portal, 720, 300);
