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

2020/1/17

Deleting an object if two things are touching in the world class

StudyNerd StudyNerd

2020/1/17

#
Basically, I am trying to find a way to see if I can delete an object in the world class. Something like this if (Player.class isTouching(Key.class) { removeObject(new Key()); }
Super_Hippo Super_Hippo

2020/1/18

#
Why do you want to do it in the world class?
StudyNerd StudyNerd

2020/1/18

#
Super_Hippo wrote...
Why do you want to do it in the world class?
This is why because I have my timer in the world class. So there is a key that spawns after 5 seconds in the world class. If a player touches that key, it should despawn without effecting the timer. So when it despawns in the world class, I can make it so the timer resets if the time runs out or if the player touches the key
Archerfish Archerfish

2020/1/18

#
Hey StudyNerd! If you want the timer to reset when the player touches the key, you can use a global variable for the timer. In this way, it is possible for the player class to access and alter the variable. This can be achieved by declaring the timer like so (in the World class): public static int timer; To reference the timer in the player code, do this: World.timer (Where World is the name of your world) If you want to reset the timer in the player code, do this: World.timer = 0;
Super_Hippo Super_Hippo

2020/1/18

#
So you want that a new key spawns every five seconds? I think I didn’t understand how collecting or not collecting is affecting the timer. Using a static variable for it seems pretty unnecessary though.
You need to login to post a reply.