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

2011/10/29

Game question and Public Variables

darkmist255 darkmist255

2011/10/29

#
I am making a game where the player is in the bottom center and can only rotate. They shoot to destroy object that will be launched at them. I am having two problems: 1. I know that if i just say if (Greenfoot.isKeyDown("space")) { Shoot(getRotation()); //ignore the getRotation() parameter until question 2 } then it will keep shooting if I hold the space bar. I have a few ideas but they all involve public variables which seems very difficult (oddly) to do in Java. How are public variables done? 2. Class "Shoot" goes like this: public void Shoot(int castleRotation) { Rock rock = new Rock(); getWorld().addObject(rock, 302, 399); setRotation(castleRotation); } I would think that that would create a new Rock right where my castle is, then add it to the world and rotate it to match what angle the Castle shot it at. All of the shots are going 0 degrees though. Lots to look through, please give some advice. Public Variables are the most crucial thing I need help with.
darkmist255 darkmist255

2011/10/29

#
Just reading this. Hopefully that helps with the Variable end of things a bit.
darkmist255 darkmist255

2011/10/29

#
Nevermind I believe I have figured out a way that doesn't use public variables (it's much more elegant too). Some help with public variables is still appreciated though. I got the shoot delay working, but still can't rotate it. Is there a way for setRotation() to set the rotation of another object?
darkmist255 darkmist255

2011/10/29

#
Got it! I didn't have to use any public variables just by making it this: public void Shoot() { Rock rock = new Rock(); getWorld().addObject(rock, 302, 399); rock.setRotation(getRotation() - 90); } Both problems solved. Here's the finished section that worked: private int shotDelay = 0; public void checkShoot() { if (shotDelay > 0) { shotDelay = shotDelay - 1; } else if (Greenfoot.isKeyDown("space")) { Shoot(); shotDelay = 30; } } public void Shoot() { Rock rock = new Rock(); getWorld().addObject(rock, 302, 399); rock.setRotation(getRotation() - 90); }
kiarocks kiarocks

2011/10/29

#
do you want it to keep adding rocks? just a question
darkmist255 darkmist255

2011/10/30

#
Yeah, but only 1 rock at a time. Here's the finished project (modified to more of a space-like thing). Click here for game
You need to login to post a reply.