Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Coding enemies for a platformer?
- pngs
-
Scratcher
76 posts
Coding enemies for a platformer?
Hi!
I'm attempting to make a platformer. My first level is divided into multiple parts. When the player sprite goes to either edge of the screen, the ground sprite changes to indicate the next part of the level.


I am currently trying to implement enemies into my game, and I'm having a hard time figuring out how to get them to work. How could I code them that so they appear only when the ground's costume is set to one that I want them to appear in? How would I program their movement? (I have a lot of different ideas for enemies, so generic code rather than exact numbers is fine movement-wise)
I'm attempting to make a platformer. My first level is divided into multiple parts. When the player sprite goes to either edge of the screen, the ground sprite changes to indicate the next part of the level.


I am currently trying to implement enemies into my game, and I'm having a hard time figuring out how to get them to work. How could I code them that so they appear only when the ground's costume is set to one that I want them to appear in? How would I program their movement? (I have a lot of different ideas for enemies, so generic code rather than exact numbers is fine movement-wise)
- -ShadowOfTheFuture-
-
Scratcher
1000+ posts
Coding enemies for a platformer?
If you need to have multiple enemies that function the same, I'd suggest using clones. Make the code in the beginning to create the clones, and then use a local variable named “clone_id” that's set to a different value each time the clone is created.
Making them only show when the ground's costume is set to a certain one:
Having the enemy move from side-to-side is a pretty common, generic motion, so that's what I'll demonstrate.
Create more local variables to specify the range, starting direction, and speed of the enemies.
// all of these are local variables
Now using if statements and the clone_id variable, make it initially set the range, direction, and speed variables to different amounts, depending on its clone_id.
Then program in the code for the enemies to change their x positions by the speed times the direction, and then when it's x position is greater than its original x plus the range variable, reverse its direction.
I'm sorry if this is hard to understand, I'm not that good at explaining things
(clone_id) // make sure this is a local variable, set to "this sprite only"
Making them only show when the ground's costume is set to a certain one:
when I start as a clone
forever
if <(clone_id) = [1]> then
if <([costume # v] of [Sprite1 v]) = []> then // makes it show only when sprite1 is a specific costume
show
else
hide
end
end
if <(clone_id) = [2]> then
...
end
...
Having the enemy move from side-to-side is a pretty common, generic motion, so that's what I'll demonstrate.
Create more local variables to specify the range, starting direction, and speed of the enemies.
(range)
(direction :: variables)
(speed)
// all of these are local variables
Now using if statements and the clone_id variable, make it initially set the range, direction, and speed variables to different amounts, depending on its clone_id.
when I start as a clone
if <(clone_id) = [1]> then
set [ v] to []
end
if <(clone_id) = [2]> then
...
end
forever
...
Then program in the code for the enemies to change their x positions by the speed times the direction, and then when it's x position is greater than its original x plus the range variable, reverse its direction.
I'm sorry if this is hard to understand, I'm not that good at explaining things

- pngs
-
Scratcher
76 posts
Coding enemies for a platformer?
…Thank you for answering about the movement, though I don't think clones would be particularly helpful in this situation as I only need one of a particular enemy to appear per section. Do you know how would I code the enemies so that they only appear when the ground's costume is set to a certain number?
Last edited by pngs (Dec. 8, 2017 14:21:06)
- Uniquename1
-
Scratcher
100+ posts
Coding enemies for a platformer?
For that part use
You'll want a forever or something similar around that too so it's always on. You may want the movement in a separate script so you aren't stuck in an unending or time consuming loop when you switch ground costumes.
if <([costume number v] of [ground sprite v]) = [#]> then
show
and move/ do stuff here or put movement elsewhere
else
hide
end
You'll want a forever or something similar around that too so it's always on. You may want the movement in a separate script so you aren't stuck in an unending or time consuming loop when you switch ground costumes.
Last edited by Uniquename1 (Dec. 8, 2017 14:28:12)
- deck26
-
Scratcher
1000+ posts
Coding enemies for a platformer?
If there's only one per backdrop you don't need the extra complication of clones, just have a script (and costume if necessary) for each different behaviour. If the backdrops appear in a known order you can then have a control script for the sprite which detects when the backdrop changes.
eg
eg
broadcast [level1 v]and so on.
wait until <> // whatever indicates next level
stop [other scripts in sprite v] // to stop level1 receiver
broadcast [level2 v]
wait until <> // whatever indicates next level
stop [other scripts in sprite v] // to stop level2 receiver
- pngs
-
Scratcher
76 posts
Coding enemies for a platformer?
…Thank you!!! I had no idea that a sprite could sense another like that… This in combination with lists makes things very simple. Thank you very much.
- codentag
-
Scratcher
100+ posts
Coding enemies for a platformer?
what does the … block meanIt's just a placeholder, kind of like saying “et cetera”, or “and the rest”.
Also, please post your own topic next time.
- Discussion Forums
- » Help with Scripts
-
» Coding enemies for a platformer?