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.
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | 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; } } */ } |