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

2013/2/28

how to make an actor shoot stuff and kill others

Velociraptor06 Velociraptor06

2013/2/28

#
Hi, I'm making a french game where the actor has to enter a correct word before he can press the spacebar and shoot his enemy. Basically so far I've got an actor that the player controls walking around, and I want a little box at the bottom where I type out a word and then hit space. If the word is correct, the actor will shoot something and kill the others when I press space bar. Is that possible? To type and then shoot? Does anyone have any idea as to how to do that? Thank you.
danpost danpost

2013/2/28

#
If the other actors (the ones that get shot at by the player) move around on their own, it would be best to create a new world for the 'question'/'prompt'/'answer entry' part of the scenario. If not, a boolean is all that is needed to signify 'in question mode' or not to determine what actions to perform. If using a seperate world to perform the question/answer part, then (with MainWorld as the main world, QWorld as the world to perform the question/answer part and Player as the player-controlled actor with a 'shoot' method to perform the shooting:
// in Player class, when question needs asked
getWorld().setWorld(QWorld((MainWorld)getWorld()));
// in QWorld class:
// instance field to hold the main world
private MainWorld mainWorld;
// QWorld constructor
public QWorld(MainWorld world)
{
    super(world.getWidth(), world.getHeight(), world.getCellSize());
    mainWorld = world;
    // set background/add objects
}
// if answer was correct
((Player) mainWorld.getObjects(Player.class).get(0)).shoot();
// whether right or wrong
setWorld(mainWorld);
return;
The act method of QWorld should get and display valid keystrokes; and when the 'space' key is returned, check the answer, calling shoot in the player class if correct; and then return to the main world. The whole time that QWorld is active, the main world will be in a 'paused' state, without being displayed. I have a PauseWorld class that can be used to show the current state of the main world without actually being in the main world, if you wish for the current state to be displayed while the question is being dealt with. You can either sub-class it with QWorld or make it your QWorld by adding the appropriate code for dealing with the question in it.
Velociraptor06 Velociraptor06

2013/3/5

#
thank you so much for your reply. does that mean the player will be able to actually type out the answer first? i don't understand what you mean by keystrokes.
danpost danpost

2013/3/5

#
You said that a word is typed out and the spacebar is then pressed. Each character, the letters of the word and the space, constitutes one keystroke. And yes, I was saying that it will be possible to allow the player to type the answer first, no matter which way you decide to go as far as either using a new world or working in the same world.
Velociraptor06 Velociraptor06

2013/3/7

#
so basically what you're saying is that i create a separate world, which will be the box where the player answers the questions. and then to see if the answer is right or wrong i will use a boolean method? thx
danpost danpost

2013/3/7

#
You do not have to create a seperate world. You can go either way; however, the deciding factor, if you are not sure which way that you want to go, should be in how your actors movements are contolled. That is, if you have actors that move randomly or continuously (without any user control), you probably would want to use a seperate world, otherwise using an actor in the current world or using a seperate world would be equally viable. If you prefer to have the little box at the bottom of the screen for entering the word, then you may want to draw the box onto the background of the world and use an actor for the cursor within the box. Put the keyboard detection code in the class of the cursor. Store the input in a static string field in the cursor class and add a public static method to return a copy of the string, clearing its value to an empty string when returned. The cursor should have a non-static string field to be used during input and when the space key is pressed, move the value to the static field and remove the cursor from the world. In the class of the main actor, have the first thing done in the act method be to get the value of the static field in the cursor class. If it returns an empty string process the code that you currently have in that act method. If not, check the string returned to the correct answer and determine the action to take from that. If using the little box and other actors move randomly or continuously, you can put a condition on their movement: if(getWorld().getObjects(Cursor.class).isEmpty()) { move code here} Sorry, if this sounds a little complicated. But, for what you are trying to do, it does take a little know-how. I may post a Cursor class for you later and explain how to have it work with your main actor.
Velociraptor06 Velociraptor06

2013/3/9

#
thank you very much. I understand it a lot more now. :) you are a really awesome programmer.
Velociraptor06 Velociraptor06

2013/4/17

#
@danpost what is the keyboard detection code?
Velociraptor06 Velociraptor06

2013/4/17

#
is there a cursor class now? thx
danpost danpost

2013/4/17

#
What is the format of the input string? will you be using all uppercase (or lowercase; or a mix)? what characters are allowed (alpha, numeric, symbols)? how long can the string be? what text editing control are you allowing? As far as the cursor, you can use a seperate class for the cursor OR you can have a class for edit-able text that will show the cursor on its image with the text when it has focus.
Velociraptor06 Velociraptor06

2013/4/24

#
@danpost the format of the input string? I'm using lowercase only, alphabets only. what do you mean by text editing control? please, i still do not understand what you wrote about the cursor. could you explain agin?
Velociraptor06 Velociraptor06

2013/4/24

#
How do you think I can make a game like this: http://www.pembinatrails.ca/fortrichmondcollegiate/compsci/greenfoot/typing-tutor/typing-tutor-export/typing-tutor.html I want to learn how the programmer made the box for typing.
riorex18 riorex18

2013/11/13

#
That was helpful. Thanks.
You need to login to post a reply.