I'm one of those people that will spend hours trying to learn by myself (and I have) while trying to wrap my head around why adding more than two "Tele" methods cause all of the Tele methods to not work (the mouse gets stuck in a teleport loop). I hate asking for help. On top of that I'm also one of those people that will want to know WHY something doesn't work and not just, "That doesn't work, try something else." Which is another reason I hate asking for help. I know a lot of people that respond negatively to the question, "Why?" The first two methods (hitTeleOne & hitTeleTwo) work exactly as I would expect them to when they are the only two methods that are active. However, the moment I un-comment out TeleThree through TeleSix it all goes crazy, and the poor mouse gets stuck in space-time continuum.
The rest of the code (classes and subclasses) are all set up in a very basic "I'm learning" style and I fully understand that there is a better way to go about it.
credit to Danpost for the Tele code I found while Googling my heart out trying to figure this out.
Setup:
World
--MyWorld
Actor
--Cheese(pizza bc Greenfoot doesn't have a cheese image lol)
--Mouse
--Teleporter
...........^-- TeleOne
...........^--TeleTwo
...........^--TeleThree
...........^--TeleFour
...........^--TeleFive
...........^--TeleSix
--Walls
..........^-- *a bunch of WallHeightSmall/XLarge and WallWidthSmall/XLarge
There is no out of the ordinary code in any other area other than the Mouse class (which is posted below). Everything else is your basic Scaling images down and obviously all the addObject code in the MyWorld class.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Mouse here. * * @author (your name) * @version (a version number or a date) */ public class Mouse extends Actor { /** * Act - do whatever the Mouse wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public boolean teleporting = false; public Mouse() { //getting image of Mouse actor and scaling the width and height by 3 getImage().scale(getImage().getWidth() / 3 , getImage().getHeight() / 3); } public void act() { // Add your action code here. moveAround(); hitFood(); hitTeleOne(); hitTeleTwo(); //hitTeleThree(); //hitTeleFour(); //hitTeleFive(); //hitTeleSix(); } public void moveAround() { //checking if the right key is down if (Greenfoot.isKeyDown("right")) { //setting the location of the Mouse actor to the right 3 pixels setLocation(getX() + 3 , getY()); //checking if our hitWall method is returning true if (hitWall() == true) { //if method returns true and the Mouse actor is hitting a wall then it does not move to the right setLocation(getX() -3 , getY()); } } if (Greenfoot.isKeyDown("left")) { setLocation(getX() - 3 , getY()); if (hitWall() == true) { setLocation(getX() + 3 , getY()); } } if (Greenfoot.isKeyDown("up")) { setLocation(getX() , getY() - 3); if (hitWall() == true) { setLocation(getX() , getY() + 3); } } if (Greenfoot.isKeyDown("down")) { setLocation(getX() , getY() + 3); if (hitWall() == true) { setLocation(getX() , getY() - 3); } } } public boolean hitWall() { //checking if the Mouse actor is touching an actor/object in the Walls class if (isTouching(Walls.class)) { return true; } else { return false; } } public void hitFood() { //checking if the Mouse actor is intersecting with an object in the Cheese class and if that something does not equal to nothing if(getOneIntersectingObject(Cheese.class) != null) { //retrieving said object from the Cheese class that is being intersected from the World and removing it getWorld().removeObject(getOneIntersectingObject(Cheese.class)); } } public void hitTeleOne() { //checking if teleporting is NOT true & if TeleOne is NOT empty if(!teleporting && !getIntersectingObjects(TeleOne.class).isEmpty()) { //initiating the teleporter by now declaring it as true teleporting = true; //get teleporter two object from world and assign to a variable TeleTwo teletwo = (TeleTwo) getWorld().getObjects(TeleTwo.class).get(0); //move mouse to teleporter two setLocation(teletwo.getX() , teletwo.getY()); } //checking if teleporting is true & if TeleTwo is clear of intersecting objects & if TeleOne is clear of intersecting objects if(teleporting ==true && getIntersectingObjects(TeleTwo.class).isEmpty() && getIntersectingObjects(TeleOne.class).isEmpty()) { teleporting = false; } } public void hitTeleTwo() { //checking if teleporting is NOT true & if TeleTwo is NOT empty if(!teleporting && !getIntersectingObjects(TeleTwo.class).isEmpty()) { //initiating the teleporting by now declaring it as true teleporting = true; //get teleporter one object from world and assign to a variable TeleOne teleone = (TeleOne) getWorld().getObjects(TeleOne.class).get(0); //move mouse to teleporter one setLocation(teleone.getX() , teleone.getY()); } //checking if teleporting is true & if TeleOne is clear of intersecting objects & if TeleTwo is clear of intersecting objects if(teleporting ==true && getIntersectingObjects(TeleOne.class).isEmpty() && getIntersectingObjects(TeleTwo.class).isEmpty()) { teleporting = false; } } /* public void hitTeleThree() { //checking if teleporting is NOT true & if TeleThree is NOT empty if(!teleporting && !getIntersectingObjects(TeleThree.class).isEmpty()) { //initiating the teleporting by now declaring it as true teleporting = true; //get teleporter four object from world and assign to a variable TeleFour telefour = (TeleFour) getWorld().getObjects(TeleFour.class).get(0); //move mouse to teleporter four setLocation(telefour.getX() , telefour.getY()); } //checking if teleporting is true & if TeleFour is clear of intersecting objects & if TeleThree is clear of intersecting objects if(teleporting ==true && getIntersectingObjects(TeleFour.class).isEmpty() && getIntersectingObjects(TeleThree.class).isEmpty()) { teleporting = false; } } public void hitTeleFour() { //checking if teleporting is NOT true & if TeleFour is NOT empty if(!teleporting && !getIntersectingObjects(TeleFour.class).isEmpty()) { //initiating the teleporting by now declaring it as true teleporting = true; //get teleporter three object from world and assign to a variable TeleThree telethree = (TeleThree) getWorld().getObjects(TeleThree.class).get(0); //move mouse to teleporter three setLocation(telethree.getX() , telethree.getY()); } //checking if teleporting is true & if TeleThree is clear of intersecting objects & if TeleFour is clear of intersecting objects if(teleporting ==true && getIntersectingObjects(TeleThree.class).isEmpty() && getIntersectingObjects(TeleFour.class).isEmpty()) { teleporting = false; } } public void hitTeleFive() { //checking if teleporting is NOT true & if TeleFive is NOT empty if(!teleporting && !getIntersectingObjects(TeleFive.class).isEmpty()) { //initiating the teleporting by now declaring it as true teleporting = true; //get teleporter six object from world and assign to a variable TeleSix telesix = (TeleSix) getWorld().getObjects(TeleSix.class).get(0); //move mouse to teleporter six setLocation(telesix.getX() , telesix.getY()); } //checking if teleporting is true & if TeleSix is clear of intersecting objects & if TeleFive is clear of intersecting objects if(teleporting ==true && !getIntersectingObjects(TeleSix.class).isEmpty() && getIntersectingObjects(TeleFive.class).isEmpty()) { teleporting = false; } } public void hitTeleSix() { //checking if teleporting is NOT true & if TeleSix is NOT empty if(!teleporting && !getIntersectingObjects(TeleSix.class).isEmpty()) { //initiating the teleporting by now declaring is as true teleporting = true; //get teleporter five object from world and assign to variable TeleFive telefive = (TeleFive) getWorld().getObjects(TeleFive.class).get(0); //move mouse to teleporter five setLocation(telefive.getX() , telefive.getY()); } //checking if teleporting is true & if TeleFive is clear of intersecting objects & if TeleSix is clear of intersecting objects if(teleporting ==true && !getIntersectingObjects(TeleFive.class).isEmpty() && !getIntersectingObjects(TeleSix.class).isEmpty()) { teleporting = false; } } */ }