Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Moving Room To Room
- howbouteggs
-
2 posts
Moving Room To Room
Im making a game which i want my sprite to enter a room when it reaches the top part of the screen. Please help!
- CurtisThePotato
-
34 posts
Moving Room To Room
This kind of thing takes quite a few scripts and requires the use of variable blocks and operator blocks. If you don't know what those blocks are, let me introduce you. Variables are used to store numbers that can be changed at any time. Operator blocks are used to do math and to test whether something is true or not. An example of an operator block and a variable would look like this:
To start off solving your problem, you need to make a sensor that detects if the player is touching the top of the screen, so that you can switch maps. Make a sprite that is a straight line across the screen and move it somewhere at the top of the screen. Next, make a variable by going into the variables tab and clicking “create a variable.” It will ask you what you want to name your new variable. Name the variable something like “map” and make sure you check “For all sprites” so that this variable can be used anywhere in the project. From now on, I will refer to this variable as “map.”
The first script in this sensor sprite that you will make will be this:
Next, we have to make sure that this script can't be activated again if “map” already equals 1, so we will add onto this script by saying if “map” does not equal 1, then run the rest of the script:
Hope this helps
foreverThis script takes a variable and tests whether or not the variable is less than 10. If it is less than 10, 1 is added to the variable. This continues until the variable is not less than 10, at which point the script is told to stop.
if <(variable) < [10]> then
change [ variable] by (1)
else
stop [this script]
end
end
To start off solving your problem, you need to make a sensor that detects if the player is touching the top of the screen, so that you can switch maps. Make a sprite that is a straight line across the screen and move it somewhere at the top of the screen. Next, make a variable by going into the variables tab and clicking “create a variable.” It will ask you what you want to name your new variable. Name the variable something like “map” and make sure you check “For all sprites” so that this variable can be used anywhere in the project. From now on, I will refer to this variable as “map.”
The first script in this sensor sprite that you will make will be this:
when green flag clickedThis script resets the “map” variable when you restart the game. It also senses if it is touching the player, and if it is, then change the map to 1.
set [ map] to [0]
forever
if <touching [ player] ?> then
set [ map] to [1]
end
end
Next, we have to make sure that this script can't be activated again if “map” already equals 1, so we will add onto this script by saying if “map” does not equal 1, then run the rest of the script:
when green flag clickedWe will also make the sprite invisible now. It is important that you don't use the “hide” block for this, since it will not detect if the sprite is touching something while hidden. Instead we will use the ghost graphical effect. The script should look like this now:
set [ map] to [0]
forever
if <not <[map] = [1]>> then
if <touching [ player] ?> then
set [ map] to [1]
end
end
end
when green flag clickedWe also have to remember to move the player from the top of the screen to the bottom once the map is changed. We can do this using broadcasts. We will have to add a broadcast which I will call “map up” into the script:
set [ map] to [0]
set [ ghost] effect to (100)
forever
if <not <[map] = [1]>> then
if <touching [ player] ?> then
set [ map] to [1]
end
end
end
when green flag clickedNow, go into your player sprite. We have to tell the player to move from the top of the screen to the bottom. This script will do this once it receives the “map up” broadcast. Note that this script only moves the player down, and does not change the x coordinate of the player.
set [ map] to [0]
set [ ghost] effect to (100)
forever
if <not <[map] = [1]>> then
if <touching [ player] ?> then
set [ map] to [1]
broadcast [map up]
end
end
end
when I receive [ map up]The next step is to make the bottom sensor that senses if the player is touching the bottom of the screen to go back to the original map. Make another sprite that is a straight line and position it at the bottom of the screen. We basically have to copy the code from the previous sensor but change some stuff up. It should look like this:
set y to (-160)
when green flag clickedNow we have to go back into the player sprite and tell it to move from the bottom of the screen to the top when it receives the “map down” broadcast. The next script in the player sprite should look like this:
set [ ghost] effect to (100)
forever
if <not <[map] = [0]>> then
if <touching [ player] ?> then
set [ map] to [0]
broadcast [map down]
end
end
end
when I receive [ map up]Now for the most important part, which is actually changing the screens. Go to your background sprite. We have to tell the background if the “map” variable equals 0, then change the background's costume to costume #1 and if the “map” variable equals 1, then change the background's costume to costume #2. The script for this should look like this:
set y to (160)
when green flag clicked
switch costume to [ costume #1]
forever
if <[map] = [0]> then
switch costume to [ costume #1]
else
if <[map] = [1]> then
switch costume to [ costume #2]
end
end
Hope this helps
- DipLeChip
-
1000+ posts
Moving Room To Room
snip
Or just use a while level = “Level” and an if for the y and x cords, that would be much for easier
Blocks:
Repeat until {Not level = 1>}
if {{ y > (Y) and x > (X) } and x < (X)} then
Next level
- deck26
-
1000+ posts
Moving Room To Room
@CurtisThePotato seems to be making it far more complicated than necessary.
- deck26
-
1000+ posts
Moving Room To Room
So do you want the sprite to then move to the bottom of the screen and a new room appear or is the room scrolling?
If the former just have one variable that is the current room number/name. To detect the door either check the sprite's x and y coordinates each time you move it or have a sprite for the doorway and detect touching that. When you hit the door move the sprite the bottom of the screen and change the backdrop or broadcast to whatever sprite represents the room.
If the former just have one variable that is the current room number/name. To detect the door either check the sprite's x and y coordinates each time you move it or have a sprite for the doorway and detect touching that. When you hit the door move the sprite the bottom of the screen and change the backdrop or broadcast to whatever sprite represents the room.
- dragonslayer1387
-
1 post
Moving Room To Room
can you please tell me how to make a scroling room
- ametrine_
-
1000+ posts
Moving Room To Room
please make a new topic for this can you please tell me how to make a scroling room
- Discussion Forums
- » Help with Scripts
-
» Moving Room To Room