Page 1 of 1

[RESOLVED]how to reduce the number of lives

PostPosted: Thu Jan 05, 2012 11:13 am
by castnoshadow
hi to all,

I hope this is the right section for my topic.

I'm working on "Space Invaders" with some little personalizations like animation, score...but there are few things that I have no idea how to solve.

When the gamer's ship is hit by a enemy shot, the gamer's ship is destroyed, how can I make the ship reappear decreasing the numbers of life? ( for example 3 )
How can I stop the game after that all lives where used with a "game over" message. I tryed to find the solution to this questions, but I was not able to find it.


Thanks to all and sorry for my english.

Re: how to reduce the number of lives

PostPosted: Thu Jan 05, 2012 6:32 pm
by SuperSonic
The first step would be to make a variable called "PlayerLives". Then whenever your character dies, you could do this:
Code: Select all
PlayerLives--;//Subtracts one from player lives

Does that help at all? :P

Re: how to reduce the number of lives

PostPosted: Thu Jan 05, 2012 8:11 pm
by castnoshadow
Hi SuperSonic thank you for your reply.

I'm not a programmer, all the script that I've added to the game where very simple, so I don't know exactly how to use your script :(

I tell you what I tried.

I tried to use the same method used for score point as i saw on a tutorial, but instead of using addition I tried with subtraction ( lives.textnumber = lives.textnumber -1; ) but it not work ( textnumber was 3).

meybe the variable ( in the script ) is the numbers of lives, can you show me step by step how to use your script?

thank you a lot :D

Re: how to reduce the number of lives

PostPosted: Thu Jan 05, 2012 8:30 pm
by skydereign
Okay, first thing to know, you shouldn't use textNumber like that. I know that is how it is used in the caveman ged, but using it like that has has been known to create problems. So instead you should create your own variable for lives and your own variable for score. To do add the this script to global code (note if your text display actors have the names score and lives you'll need to destroy the actors, or choose different names for the variables). Just to make sure, you can go to global code by clicking the [Global Code] button.
Global Code
Code: Select all
int score; // creates a variable called score
int lives; // creates a variable called lives

When you add that script, give the script a name, and click [Add]. Now you have two variables, one for score and one for lives.

Whenever you want to increase score, you do this (same idea as what you were using before, but it uses a real variable that you can rely on).
Code: Select all
score = score + 1;

And one thing you have to add to actually make the score display is the following.
score_actor -> Draw Actor -> Script Editor
Code: Select all
textNumber = score; // this will display the score


Now for lives it will work exactly the same way. But, since you want to start with a certain number of lives, you can add this to the player's create actor event.
Code: Select all
lives = 3; // this sets the player's life to 3

And just to reiterate, when you want to reduce the player's life, you use this.
Code: Select all
lives = lives-1;

And to have it displayed, use this.
lives_actor -> Draw Actor -> Script Editor
Code: Select all
textNumber=lives;

Re: how to reduce the number of lives

PostPosted: Thu Jan 05, 2012 9:03 pm
by SuperSonic
@castnoshadow: I was going to explain my code in more detail but sky basically covered it :P

Re: how to reduce the number of lives

PostPosted: Fri Jan 06, 2012 5:31 pm
by castnoshadow
hi skydereign, thank you for your help,

there is something that I'm doing wrong.

can you tell me where have I to add the script
Code: Select all
int lives; // creates a variable called lives

I can open the global code if I select une actor, click on "add" button and select an option > script editor,but I don't know for this script witch actor to choose and what option to select. I tried different things without result.

Than I tried this:

"Now for lives it will work exactly the same way. But, since you want to start with a certain number of lives, you can add this to the player's create actor event.

Code: Select all
lives = 3; // this sets the player's life to 3"

I selected my actor >add> create actor>script editor and here I added your script.

After this I added this script "lives = lives-1;" to the lives_actor and after this
Code: Select all
textNumber=lives;
here: "score_actor -> Draw Actor -> Script Editor"

I'm doing something wrong, but I don't know what

Re: how to reduce the number of lives

PostPosted: Fri Jan 06, 2012 6:41 pm
by skydereign
skydereign wrote: Just to make sure, you can go to global code by clicking the [Global Code] button.

When you are in the script editor, you can click the button on the bottom called [Global code]. From there another script editor type window should appear that is called Global Code Editor. In this script area, you type the code I gave. That will declare the two variables globally.

Now your create actor event is right, but chances are you won't put the lives=lives-1 script into the lives_actor. That code is for when your player dies. So in your case in the player's collision event with the bullet (since that is when you want to decrease the number of lives the player has). You'll also be using lives to destroy the actor most likely (when lives equals zero).

Re: how to reduce the number of lives

PostPosted: Sun Jan 08, 2012 9:41 am
by castnoshadow
It works!!!!!!!!!

I thank you so mutch!!! it is not so easy to understand how the programm works and it's not easy because I don't know nothing about scripts.

Re: [RESOLVED]how to reduce the number of lives

PostPosted: Sun Jan 08, 2012 10:12 am
by castnoshadow
now that this works, it's better to open another topic to ask hot to stop the game with a game over message? ( i can't believe that there is no tutorial about)