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

2012/5/24

Teleporting problem?

PiercingGoblin PiercingGoblin

2012/5/24

#
I have 2 portals, one red, one blue, and when you touch one it teleports you to the other and removes both portals (otherwise you would get stuck in a loop of teleports). Any way to fix this?
ttamasu ttamasu

2012/5/24

#
assuming that you are using something like getOneIntersectingObject() and your teleport object is called Teleport.class you would introduce a boolean variable like: private boolean teleported = false ; // declared as a class variable .... if (getOneIntersectingObject(Teleport.class) && ! teleported){ teleported = true; // do the action of teleporting to the other teleporter } if ( teleported && !getOneIntersectingObject(Teleport.class)) // player is no longer touching it teleported = false;
ttamasu ttamasu

2012/5/24

#
deleted... duplicate message
PiercingGoblin PiercingGoblin

2012/5/24

#
Gah! I should've thought of that! Thank you very much!
PiercingGoblin PiercingGoblin

2012/5/24

#
Hmm... actually, trying this results in my character getting stuck at the teleporter she is teleporting to. This is the code, which is slightly modified from yours as I use 'can_use_teleporter' instead of 'teleported' (so its essentially opposite)
    public void act() 
    {
        GameWorld board = (GameWorld) getWorld();
        MouseInfo mouse = Greenfoot.getMouseInfo();
        Actor wall;
        boolean can_use_portal = true;
        if (Greenfoot.mousePressed(null))
        {
            if (mouse.getButton() == 1)
            {
                shootportal(1); //Shoots Red Portal
            }
            else
            {
                shootportal(2); //Shoots Blue Portal
            }
        }

        if(Greenfoot.isKeyDown("d") && canMove(1) == true)
        {
            setLocation(getX() + 5, getY()); //Move Right
        }

        if(Greenfoot.isKeyDown("a") && canMove(3) == true)
        {
            setLocation(getX() - 5, getY()); //Move Left
        }

        if ((getY() < (board.getHeight()-30)) && canMove(4) == true)
        {
            setLocation(getX(), getY() + 3); //Move Down
        }

        Actor redportal;
        Actor blueportal;
        blueportal bportal;
        redportal rportal;
        if (getObjectsInRange(1000, redportal.class).size() > 0 && getObjectsInRange(1000, blueportal.class).size() > 0)
        {
            bportal = (blueportal) getObjectsInRange(1000, blueportal.class).get(0);
            rportal = (redportal) getObjectsInRange(1000, redportal.class).get(0);
            if (getOneObjectAtOffset(0, 0, redportal.class) != null && can_use_portal)
            {
                can_use_portal = false;
                setLocation(bportal.getX(), bportal.getY());
            }
            
            if ((getOneObjectAtOffset(0, 0, redportal.class) == null) || (getOneObjectAtOffset(0, 0, blueportal.class) == null))
            {
                if (can_use_portal == false)
                {
                    can_use_portal = true;
                }
                can_use_portal = true;
            }
        }   
        if (getObjectsInRange(1000, redportal.class).size() > 0 && getObjectsInRange(1000, blueportal.class).size() > 0)
        {
            bportal = (blueportal) getObjectsInRange(1000, blueportal.class).get(0);
            rportal = (redportal) getObjectsInRange(1000, redportal.class).get(0);
            if (getOneObjectAtOffset(0, 0, blueportal.class) != null && can_use_portal)
            {
                can_use_portal = false;
                setLocation(rportal.getX(), rportal.getY());
            }
            
            if ((getOneObjectAtOffset(0, 0, redportal.class) == null) || (getOneObjectAtOffset(0, 0, blueportal.class) == null))
            {
                if (can_use_portal == false)
                {
                    can_use_portal = true;
                }

            }
        }     

        Actor finish_area;
        if (getOneObjectAtOffset(0, 0, finish_area.class) != null)
        {
            board.nextLevel();
        }
    }
danpost danpost

2012/5/25

#
I am not sure if this will correct the functionality of it, but it appears that line 48 should be '&&', not '||'. Same with line 67. I think the coding had been made more complicated than need be because of seperate classes (redportal/blueportal) for the portals. You can make them both the same class and add the Color of the portal as a parameter in the constructor of the 'Portal' class -- public Portal(Color color). You can add an instance variable of Portal class to the code declared with -- Portal portalLink; -- and add the methods -- public void setPortalLink(Portal portal) -- and -- public Portal getPortalLink() -- to facilitate setting and retrieving the value of the variable of the objects. When creating the portals, create both -- Portal rPortal = new Portal(Color.red); Portal bPortal = new Portal(Color.blue); rPortal.setPortalLink(bPortal); bPortal.setPortalLink(rPortal) -- then add them to the world. Once done, you can easily find out where to teleport to when intersecting any portal using one code-set. Also, you only need one code-set to check if teleport complete (not intersecting any portal object to reset the can_use_portal variable).
danpost danpost

2012/5/25

#
I believe lines 34 and 35 can be deleted with no ill effects.
danpost danpost

2012/5/25

#
Happened to me! A double-post with unknown reason.
ttamasu ttamasu

2012/5/26

#
I didn't realize you had two different classes for the teleports you would probably need two boolean variables and do something like: private canUseRedPortal = true; private canUseBluePortal = true; ..... if (canUseRedPortal && getOneIntersectingObject(redportal.class)!= null){ canUseBluePortal = false; // since you are teleported to blue portal setLocation(bportal.getX(), bportal.getY()); } // just teleported to red portal so it is availabe as soon as you step off it if (!canUseRedPortal && getOneIntersectingObject(redportal.class)== null) canUseRedPortal = true; // set true when you are not touching teleporter if (canUseBluePortal && getOneIntersectingObject(blueportal.class) != null){ canUseRedPortal = false; //since you are teleporting to red portal setLocation(rportal.getX(),rportal.getY()); } // just teleported to blue portal so it is available as soon as you step off it if (!canUseBluePortal && getOneIntersectingObject(blueportal.class) == null) canUseBluePortal = true;
danpost danpost

2012/5/26

#
If you want to see a Demo using portals in the way I suggested, just say so.
PiercingGoblin PiercingGoblin

2012/5/26

#
For some reason I have a feeling that I should be able to use just 1 boolean that states whether or not I am allowed to use a portal of any kind (if colliding with red or blue). This is my current code for the player (named chell) in it's current state. At the moment when chell touches either portal she becomes 'stuck' to the red portal as if its constantly teleporting chell to the red portal. Thank you for all the help so far guys! I linked it to pastie as it's quite a bit of code to put in the post itself.
davmac davmac

2012/5/26

#
Lines 50 and 65, change '||' to '&&'. You want to set canUsePortal true if you can't currently see any portal. At the moment you're setting it true if you can't see the blue portal OR you can't see the red portal, which is always going to be the case (you'll never be able to see them both at the same time).
PiercingGoblin PiercingGoblin

2012/5/27

#
Thanks a million davmac! Sometimes I fail to see the logic in my programming.
danpost danpost

2012/5/27

#
danpost wrote...
I am not sure if this will correct the functionality of it, but it appears that line 48 should be '&&', not '||'. Same with line 67.
I had mentioned this two days ago. The same post continued
danpost wrote...
I think the coding had been made more complicated than need be because of seperate classes (redportal/blueportal) for the portals. You can make them both the same class and add the Color of the portal as a parameter in the constructor of the 'Portal' class -- public Portal(Color color). You can add an instance variable of Portal class to the code declared with -- Portal portalLink; -- and add the methods -- public void setPortalLink(Portal portal) -- and -- public Portal getPortalLink() -- to facilitate setting and retrieving the value of the variable of the objects. When creating the portals, create both -- Portal rPortal = new Portal(Color.red); Portal bPortal = new Portal(Color.blue); rPortal.setPortalLink(bPortal); bPortal.setPortalLink(rPortal) -- then add them to the world. Once done, you can easily find out where to teleport to when intersecting any portal using one code-set. Also, you only need one code-set to check if teleport complete (not intersecting any portal object to reset the can_use_portal variable).
which is illustrated in the 'Teleport Demo' that I am uploading now.
You need to login to post a reply.