Video - Show Enemy Character at Random Position
So far we have introduced 4 of the 5 characters to our game. We will soon finish it.
A little more patience, but I ask you to pay great attention because we have a few but very important issues to deal with.
Let's get back to my computer screen and continue.
It is time to place the last object - bananas into our game, the bananas
So follow the now familiar process and enter the bananas sprite into our game.
We want the bananas, initially, to disappear as soon as the monkey touches them and appear at some other random spot on the stage.
We also want to count scores, but we'll do it right afterwards.
Let's concentrate on the appearance of the object elsewhere in the scene as soon as the monkey makes contact.
In the banana scenario, once the green flag is clicked, we want them to be smaller since they are pretty big, if they touch the monkey, they will move to another place.
The IF we shall use is obvious. Let's enter it into our scenario.
In the IF statement we definitely need an instruction from the command palette "Motion" given the fact that bananas will move somewhere else.
If we carefully explore the commands of this palette, we will locate the instruction:
Go to x and y. Where x and y are specific values that express a particular position on the stage.
Here I will need a little help from the specialist to explain it a bit better...
The positions of the sprites at Scratch are expressed with 2 numbers.
The first number is known as X and expresses the horizontal position of the object.
The values that it can take are from -240 to 240.
If the value of X is equal to 0, then the object is in a central position horizontally on the stage.
That is, the distance from the left edge of the stage is definitely equal to the distance from the right edge of the stage.
If X is equal to -240 then the sprite is definitely somewhere on the left edge of the stage while 240 is at the right edge of the stage.
Similarly, the value of Y can takes values from -180 to 180.
If it is 0 then the distance of the sprite from the upper end of the stage is equal to the distance from its lower end.
If Y has a value of 180 then it is, depending on the value of X, somewhere in the upper end of the Scratch stage, while with -180 at the bottom.
The precise position of a sprite is determined by the value of both X for its horizontal position and Y for the vertical.
Every time the banana touches the monkey, it moves to another location. What we want is each time this position to be different and random.
Scratch has a process to generate random numbers within limits set by the programmer.
From the Operators palette, drag into the script area the instruction "pick random number from 1 to 10"
We will use this instruction to create both the horizontal position of the banana expressed with X and the vertical expressed with Y.
I must not forget to incorporate all this IF in a repetition order "forever" as the banana movement after contact with the monkey - we want this to happen throughout the game and not just once.
Let me first create the command "go to X and Y" in combination with the command "pick random number" and then after integrating it into my script let me try the outcome.