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

2021/4/26

Stuck near the teleport instead of transportation to a new location

Olexandra Olexandra

2021/4/26

#
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)
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;
    }
}
This is how I initialize the portals in the World:
    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);
        
danpost danpost

2021/4/26

#
You do not have anything to stop the mouse from going back and forth between the portals. Every act, the first portal to act will send mouse to the second one (if at first portal location). Then, when second one acts, it will send mouse to the first one (if at second portal location -- by movement or by teleport on same act cycle). That is why I added an active state to my portals in my demo. As far as your images, copy all images used ("Hole2.png", etc.) into the scenario's images folder and adjust the setImage lines to suit. For example, line 6-7 above would then be:
setImage(new GreenfootImage("Hole2.png"));
Olexandra Olexandra

2021/4/27

#
Thanks! I thought that stepping away should prevent the cycle. But it doesn't work.
 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);
You need to login to post a reply.