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

2012/11/15

Creating and then removing a "tongue" for a frog

LiteralSkeleton LiteralSkeleton

2012/11/15

#
Hey everyone. First time posting so apologies if I commit a snafu. Anyway, I've got a game where you play as a frog. The frog is immobile but spins. Essentially my goal is to make a "tongue" that extends from the mouth of the frog. That "tongue" would eat any flies (which are flying around) that it comes in contact with. My problem is I'm not sure how I would go about creating the tongue, letting it pause, then removing it from the world. I want it to be able to do this every time I press the space button. I'm happy to upload code if this would make things any easier but as of now I have three main actors: Frog; Fly; and Tongue. The flies move about just fine and the frog spins as desired. Now for the tongue. Any ideas folks? Thanks in advance for any help you can offer. I'll keep looking in the mean time.
danpost danpost

2012/11/16

#
Have the code to detect the spacebar keypress in the Frog class with a boolean to track the state of the space key (call it 'spaceDown'). If you detect the key down when 'spaceDown' is 'false', set 'spaceDown' to 'true' and add the tongue into the world. If you do not detect the key down when 'spaceDown' is 'true', remove the tongue and clear 'spaceDown' back to 'false'. SECOND THOUGHT: without the use of the boolean variable to track the state of the 'space' key, you could say: if no tongue object is in the world and the 'space' key is down, then add the tongue; and if the tongue is in the world and the key is not down, remove the tongue. The Frog class should also control the location and rotation of the tongue. When adding the tongue into the world, the frog's rotation and location should be used. You can use, in the Tongue class, the 'addedToWorld(World)' method to 'move' the tongue the proper offsets (you may have to 'turn(90)' and 'move' some, then 'turn(-90)' and move some if the mouth of the frog is not along either central axis of the image of the frog). Each time the frog moves or turns, the location and/or rotation of the tongue must be adjusted. It would be easiest to have an instance variable hold the Tongue object from the creation of the frog; then add, adjust in world, and remove that same object at will in the Frog class. You may have to write an 'adjustPosition' method in the Tongue class which repositions the tongue from the frog's location. This method can be called from the 'addedToWorld(World)' method; instead of writing the actual location adjustment code there. That would mean: when the frog moves or turns, relocate the tongue, if present, to the frog's location, set its rotation to that of the frog, and call the method to adjust its position.
LiteralSkeleton LiteralSkeleton

2012/11/16

#
Hmm. Okay thanks a lot for the reply. I think this is gonna be something I'll have to mull over for a while. I don't have a whole ton of experience with it so I think for now I'm going to have a more rudimentary system (no "tongue" but the frog eats flies if it encounters them). However, I think that, given enough time, this is something I can implement. Thanks a lot for the help, man!
You need to login to post a reply.