Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » How to make an enemy in a top-down-scroller?
- C-143
-
Scratcher
72 posts
How to make an enemy in a top-down-scroller?
Hello, I'm working on a game: https://scratch.mit.edu/projects/258712368/ and I want to know how to add separate entities to the game. Entities that can move on their own, whilst also being moved by the player moving. For example, if the player were to get to close to said enemy, or was to attack them, they would chase the player. Is this possible? Please help.
- awesome-llama
-
Scratcher
1000+ posts
How to make an enemy in a top-down-scroller?
You need to store the enemy's position in a variable or list. Then, every game tick, the position is set, offset to match the correct position in the screen.
Edit: I noticed you don't store the player's position properly. You need an x and y variable, for the player's location on the grid. This means the grid sprites can move and be offset on to this variable the same as the enemy.
If you want to make the enemy move (most likely), the enemy's position will have to be continuously changed to match expected movement of them.
Edit: I noticed you don't store the player's position properly. You need an x and y variable, for the player's location on the grid. This means the grid sprites can move and be offset on to this variable the same as the enemy.
If you want to make the enemy move (most likely), the enemy's position will have to be continuously changed to match expected movement of them.
Last edited by awesome-llama (Nov. 1, 2018 22:36:44)
- akinyemi08
-
Scratcher
100+ posts
How to make an enemy in a top-down-scroller?
Maybe…
Then this:
if <([abs v] of ((playerX) - (enemyX))) < (chosen distance)> then
if <(playerX) > (enemyX)> then
change [enemyX v] by (chosen speed)
else
if <(playerX) < (enemyX)> then
change [enemyX v] by ((0) - (chosen speed))
end
end
end
Then this:
go to x: ((enemyX) - (playerX)) y: ((enemyY) - (playerY))
Last edited by akinyemi08 (Nov. 1, 2018 22:44:36)
- C-143
-
Scratcher
72 posts
How to make an enemy in a top-down-scroller?
You need to store the enemy's position in a variable or list. Then, every game tick, the position is set, offset to match the correct position in the screen.
Edit: I noticed you don't store the player's position properly. You need an x and y variable, for the player's location on the grid. This means the grid sprites can move and be offset on to this variable the same as the enemy.
If you want to make the enemy move (most likely), the enemy's position will have to be continuously changed to match expected movement of them.
Hoo boy… I should probably mention i'm quite the noobie at making video games on scratch, especially complex scroller-type games like this. I'm not like griffpatch or anything. Is there any way you can simplify this for me?
- C-143
-
Scratcher
72 posts
How to make an enemy in a top-down-scroller?
Maybe…if <([abs v] of ((playerX) - (enemyX))) < (chosen distance)> then
if <(playerX) > (enemyX)> then
change [enemyX v] by (chosen speed)
else
if <(playerX) < (enemyX)> then
change [enemyX v] by ((0) - (chosen speed))
end
end
end
Then this:go to x: ((enemyX) - (playerX)) y: ((enemyY) - (playerY))
This is probably a noob thing of me to question, but what exactly is ABS?
- akinyemi08
-
Scratcher
100+ posts
How to make an enemy in a top-down-scroller?
ABS means absolute, which means positive. If the number is less than 0, it will convert it to positive.Maybe…if <([abs v] of ((playerX) - (enemyX))) < (chosen distance)> then
if <(playerX) > (enemyX)> then
change [enemyX v] by (chosen speed)
else
if <(playerX) < (enemyX)> then
change [enemyX v] by ((0) - (chosen speed))
end
end
end
Then this:go to x: ((enemyX) - (playerX)) y: ((enemyY) - (playerY))
This is probably a noob thing of me to question, but what exactly is ABS?
For example:
ABS of 4: 4
ABS of -6: 6
Last edited by akinyemi08 (Nov. 2, 2018 01:11:35)
- C-143
-
Scratcher
72 posts
How to make an enemy in a top-down-scroller?
Intresting! thanks!ABS means absolute, which means positive. If the number is less than 0, it will convert it to positive.Maybe…if <([abs v] of ((playerX) - (enemyX))) < (chosen distance)> then
if <(playerX) > (enemyX)> then
change [enemyX v] by (chosen speed)
else
if <(playerX) < (enemyX)> then
change [enemyX v] by ((0) - (chosen speed))
end
end
end
Then this:go to x: ((enemyX) - (playerX)) y: ((enemyY) - (playerY))
This is probably a noob thing of me to question, but what exactly is ABS?
For example:
ABS of 4: 4
ABS of -6: 6
- awesome-llama
-
Scratcher
1000+ posts
How to make an enemy in a top-down-scroller?
Just a note: I cannot fully understand the code you have written, so you may already have such a system implemented, just a bit different. Also the project is now unshared anyway.
Well, the usual way is to use two variables, one for X and one for the Y position. This stores the player's position relative to the grid ground.
Now, to use the variable, all of the player's movement controls are fed into these variables. It may be as simple as changing the variable by 4 or something. The movement of the background is controlled by a script that moves it to the right x and y coordinates (as stored in the X and Y variables). Remember: The X and Y coordinates may be reversed, so you may have to do some multiplying by -1 to invert the value fed into the set position block.
Now you should have a functioning movement for the background. Adding other props can be done this way.
Edit: I think it would be easier if you first try this, rather than going all the way to implement a proper AI enemy that uses this system, all at once.
Hello, I had a quick question to ask of you. In my forum post about adding enemies to my top down scroller, you mentioned that I did not have a script keeping track of the players position on the grid. How would one do this? Can you please help?(Written in a comment on my profile)
Well, the usual way is to use two variables, one for X and one for the Y position. This stores the player's position relative to the grid ground.
Now, to use the variable, all of the player's movement controls are fed into these variables. It may be as simple as changing the variable by 4 or something. The movement of the background is controlled by a script that moves it to the right x and y coordinates (as stored in the X and Y variables). Remember: The X and Y coordinates may be reversed, so you may have to do some multiplying by -1 to invert the value fed into the set position block.
Now you should have a functioning movement for the background. Adding other props can be done this way.
Edit: I think it would be easier if you first try this, rather than going all the way to implement a proper AI enemy that uses this system, all at once.
Last edited by awesome-llama (Nov. 2, 2018 02:40:33)
- Discussion Forums
- » Help with Scripts
-
» How to make an enemy in a top-down-scroller?