Discuss Scratch

Policeguy44
Scratcher
24 posts

How to make a enemy in a platformer game move by itself?

The title says my question: how do you make a enemy in a platformer game move by itself?
ItchyCatIII
Scratcher
500+ posts

How to make a enemy in a platformer game move by itself?

Could you be a little bit more specific?

If you want him to move anywhere on the screen, try

when green flag clicked
forever
glide (x) secs to x: (pick random (-300) to (300)) y: (pick random (-300) to (300))
end

The x in the glide x secs can be whatever number you want. The lower the number the faster he will go. However, it will always take him x seconds to arrive at x: chosen y: chosen (which it will randomly choose). If it chooses coordinates further away from its current location, it will move faster in order to get to its chosen coordinates in the given amount of time.

If you want it just horizontal, then
when green flag clicked
forever

set x or y to (pick random (-300) to (300))
end

The red block in that script is supposed to be dark blue. If you want it to move only horizontally, choose the
set x to ()
block.

If you want it to move vertically, choose the
set y to ()

block.



If you want it to move towards something

when green flag clicked
forever

point towards [thing you want it to move towards]
move (x) steps
end

Substitute the x steps with any number, depending on how much you want it to move at once. 1 step would move very slowly, but look smoother. The higher you get, the faster it moves, but the less smooth it looks. I usually choose a number 10-20, because that looks smooth enough, but doesn't move super slowly.



All of these suggestions will have it moving forever, though. To prevent this, replace the
forever

end
around the script with either nothing or something else, but make sure to keep what's inside of it!



If you could be more specific, I could try to tell you exactly the script you need.
Policeguy44
Scratcher
24 posts

How to make a enemy in a platformer game move by itself?

Thanks, but I should have been more specific. I meant that I want to have a enemy move around randomly and cannot go through walls.
deck26
Scratcher
1000+ posts

How to make a enemy in a platformer game move by itself?

I'd use a grid system. You store the grid in a list and use it to store values representing what's in that position - eg a wall or another sprite or just space. Then when trying to move you can check the position in the grid to see if it would be a valid move.
CodeySnail
Scratcher
21 posts

How to make a enemy in a platformer game move by itself?

ItchyCatIII wrote:

If you want him to move anywhere on the screen, try

when green flag clicked
forever
glide (x) secs to x: (pick random (-300) to (300)) y: (pick random (-300) to (300))
end


Use this code, but then on the background, draw black or whatever (I'm going with black) and (I got code from here https://scratch.mit.edu/projects/54406124/#editor ) do this:
when green flag clicked
forever
glide (x) secs to x: (pick random (-300) to (300)) y: (pick random (-300) to (300))
if <touching color [#000000] ?> then

change y by (1)
if <touching color [#000000] ?> then

change y by (1)

if <touching color [#000000] ?> then

change y by (1)

if <touching color [#000000] ?> then

change y by (1)

if <touching color [#000000] ?> then

change y by (1)
if <touching color [#000000] ?> then
change x by ((x) * (-1))
change x by (-1)
change y by (-5)
change x by (1)


end

end
end
end
end

end

end

Hope this helps!
Sigton
Scratcher
1000+ posts

How to make a enemy in a platformer game move by itself?

deck26 wrote:

I'd use a grid system. You store the grid in a list and use it to store values representing what's in that position - eg a wall or another sprite or just space. Then when trying to move you can check the position in the grid to see if it would be a valid move.
I'm intrigued, could you please expand? Would you have an ID for each place on the grid and a item on the list equal to it's ID number and it would replace it's own item on the list with whatever was in the space it's currently in. Like a sort of path-finding system? Interesting…!

Sigton
deck26
Scratcher
1000+ posts

How to make a enemy in a platformer game move by itself?

Sigton wrote:

deck26 wrote:

I'd use a grid system. You store the grid in a list and use it to store values representing what's in that position - eg a wall or another sprite or just space. Then when trying to move you can check the position in the grid to see if it would be a valid move.
I'm intrigued, could you please expand? Would you have an ID for each place on the grid and a item on the list equal to it's ID number and it would replace it's own item on the list with whatever was in the space it's currently in. Like a sort of path-finding system? Interesting…!

Sigton
Here's an intro I did a month or two ago. https://scratch.mit.edu/projects/47150778/

Last edited by deck26 (June 3, 2015 19:26:24)

SO_MUCH_NOPE
Scratcher
77 posts

How to make a enemy in a platformer game move by itself?

I am in a similar disposition.
but try something like this possibly?
when green flag clicked
forever

repeat (10)
change x by (10)

repeat (10)
change x by (-10)
end

end

end

it may not come out perfect I don't make scripts in discussion a lot but I'll bet you can get the main idea. Bear in mind this is merely a script for movement and not wall detection or player detection.
Failord
Scratcher
1000+ posts

How to make a enemy in a platformer game move by itself?

Give the enemy the same scripts you gave you player sprite, then change out the “key pressed” <booleans> for <(move) = [left]> etc. so that you can set variables to replace key presses. Have your AI enemy figure out where your character is and try to move towards them.

I used “keypress replacement” in Laser Battle 2.0's AI.

Hope this helps!
Luketheamazing
Scratcher
45 posts

How to make a enemy in a platformer game move by itself?

if your making a scrolling game your hopeless if its just a normal do this:

when green flag clicked
forever

if <(distance to [name v]) = 100 ]> then

point towards [name v]
move (10) steps
end

end
Daggerbite
Scratcher
7 posts

How to make a enemy in a platformer game move by itself?

hello ::events
Daggerbite
Scratcher
7 posts

How to make a enemy in a platformer game move by itself?

this is awesome ::sound
kieranblackley
Scratcher
500+ posts

How to make a enemy in a platformer game move by itself?

why not try this script out this might work i am not sure

when green flag clicked
forever

glide (pick random (1) to (3)) secs to x: (pick random (-240) to (240)) y: (0)
if <(distance to [name of what you would like it to get v]) < [120 ]> then
point towards [name of what you would like it to get v]
broadcast [whatever power it will do v]

end

end
The_scratcherTest
Scratcher
2 posts

How to make a enemy in a platformer game move by itself?

move (4) steps
GeometryPro1386
Scratcher
100+ posts

How to make a enemy in a platformer game move by itself?

ItchyCatIII wrote:

If you want it just horizontal, then
when green flag clicked
forever

set x or y to (pick random (-300) to (300))
end

The red block in that script is supposed to be dark blue.
You can turn it dark blue:
set x or y to (pick random (-300) to (300)) :: motion
[scratchblocks]
set x or y to (pick random (-300) to (300)) :: motion
[/scratchblocks]
just so you know.
GeometryPro1386
Scratcher
100+ posts

How to make a enemy in a platformer game move by itself?

GeometryPro1386 wrote:

ItchyCatIII wrote:

If you want it just horizontal, then
when green flag clicked
forever

set x or y to (pick random (-300) to (300))
end

The red block in that script is supposed to be dark blue.
You can turn it dark blue:
set x or y to (pick random (-300) to (300)) :: motion
[scratchblocks]
set x or y to (pick random (-300) to (300)) :: motion
[/scratchblocks]
just so you know.
(Not tryin' to necropost on this…)
Jareddddddd
Scratcher
1000+ posts

How to make a enemy in a platformer game move by itself?

hey guys this isnt the forum to test scratch forum codes. Theres is a easy solution. Just like in many popular platformer games, they have mobile support which make your player go to your mouse when it is down. You can reverse engineer this code to wire it to the enemy going to random positions, using the code that it uses. As for a scrolling platform, that is very hard. If you want to learn about just reply to this

something :: hat :: control
something :: hat :: control 
(banana @greenFlag) :: operators
banana @greenFlag :: operators
figure this one out :
[ v] [ v] [ v] :: hat :: motion
here is a good mobile engine that you can modify : https://scratch.mit.edu/projects/389115709/ (not by me)

Last edited by Jareddddddd (May 4, 2021 02:55:16)

chillazilla
Scratcher
20 posts

How to make a enemy in a platformer game move by itself?

Make a variable called movement. Have it pick random 1-8. Then have it go: if movement=1 set meovement to right. repeat with, left, jump, right jump, left jump and still. Then have a code if movement contains left move left etc etc. if contains jump change y by 10, then change y by -10.

Powered by DjangoBB