Discuss Scratch

kfchikinfiter
Scratcher
100+ posts

how to recreate move block using code

I'm working on a project that's gonna need to use lists for the enemies, and instead of using move blocks, (as I'm not actually going to use clones but instead use stamping from 1 sprite) but I need to be able to change the x and y independently in a list depending on the direction the sprite is facing, i.e. if the sprite is at 38 degrees, and I want it to move 1 in the direction it's facing, it will accurately change the x and y to make that possible. I know it's a little confusing but yall should understand.
Vitou_
Scratcher
17 posts

how to recreate move block using code

this its hard, but, I think you can use a variable X and Y
kfchikinfiter
Scratcher
100+ posts

how to recreate move block using code

Vitou_ wrote:

this its hard, but, I think you can use a variable X and Y
I'm using a list with each enemy taking up 2 lines, with the first one being for the x and the second for the y which is what I want to change

Last edited by kfchikinfiter (Sept. 20, 2022 16:55:34)

awesome-llama
Scratcher
1000+ posts

how to recreate move block using code

You can use a little bit of trigonometry.

The movement you want to make can be drawn as a right-angled triangle like this:


Blue angle is direction (note that scratch has it go clockwise starting from the y axis), red arrow is the step you want to take, and the length of it is the length of the step.

The 2 black lines are the distances you need to travel along each of the axes to make the step.

They would therefore be calculated by:
x length = step length * sin(direction)
y length = step length * cos(direction)

Here is a version in block form:

move (5) steps

// is the same as:
change x by ((5) * ([sin v] of (direction)))
change y by ((5) * ([cos v] of (direction)))

I used move blocks just so it'll be easier to understand. In your case, you can just change the x and y position by the amounts calculated.
DeveloperTools
Scratcher
100+ posts

how to recreate move block using code

kfchikinfiter wrote:

Vitou_ wrote:

this its hard, but, I think you can use a variable X and Y
I'm using a list with each enemy taking up 2 lines, with the first one being for the x and the second for the y which is what I want to change

Heres the Move () Steps block:
replace item (? v) of [list v] with ((item (? v) of [list v] :: list) + (([sin v] of (direction)) * (Amt. Of steps))) // X position
replace item (? v) of [list v] with ((item (? v) of [list v] :: list) + (([cos v] of (direction)) * (Amt. Of steps))) // Y position
kfchikinfiter
Scratcher
100+ posts

how to recreate move block using code

DeveloperTools wrote:

kfchikinfiter wrote:

Vitou_ wrote:

this its hard, but, I think you can use a variable X and Y
I'm using a list with each enemy taking up 2 lines, with the first one being for the x and the second for the y which is what I want to change

Heres the Move () Steps block:
replace item (? v) of [list v] with ((item (? v) of [list v] :: list) + (([sin v] of (direction)) * (Amt. Of steps))) // X position
replace item (? v) of [list v] with ((item (? v) of [list v] :: list) + (([cos v] of (direction)) * (Amt. Of steps))) // Y position

thanks! although for some reason it's buggy and don't work correctly by having them go to that location, they all just go to the same place and it's the same place every single time
DeveloperTools
Scratcher
100+ posts

how to recreate move block using code

kfchikinfiter wrote:

DeveloperTools wrote:

kfchikinfiter wrote:

Vitou_ wrote:

this its hard, but, I think you can use a variable X and Y
I'm using a list with each enemy taking up 2 lines, with the first one being for the x and the second for the y which is what I want to change

Heres the Move () Steps block:
replace item (? v) of [list v] with ((item (? v) of [list v] :: list) + (([sin v] of (direction)) * (Amt. Of steps))) // X position
replace item (? v) of [list v] with ((item (? v) of [list v] :: list) + (([cos v] of (direction)) * (Amt. Of steps))) // Y position

thanks! although for some reason it's buggy and don't work correctly by having them go to that location, they all just go to the same place and it's the same place every single time
If you gave me the project link i could go and see why it does that

Last edited by DeveloperTools (Sept. 20, 2022 17:23:02)

kfchikinfiter
Scratcher
100+ posts

how to recreate move block using code

DeveloperTools wrote:

kfchikinfiter wrote:

Vitou_ wrote:

this its hard, but, I think you can use a variable X and Y
I'm using a list with each enemy taking up 2 lines, with the first one being for the x and the second for the y which is what I want to change

Heres the Move () Steps block:
replace item (? v) of [list v] with ((item (? v) of [list v] :: list) + (([sin v] of (direction)) * (Amt. Of steps))) // X position
replace item (? v) of [list v] with ((item (? v) of [list v] :: list) + (([cos v] of (direction)) * (Amt. Of steps))) // Y position
also it only happens once I put in the go to block, otherwise it shows it fine with the correct position
kfchikinfiter
Scratcher
100+ posts

how to recreate move block using code

DeveloperTools wrote:

kfchikinfiter wrote:

DeveloperTools wrote:

kfchikinfiter wrote:

Vitou_ wrote:

this its hard, but, I think you can use a variable X and Y
I'm using a list with each enemy taking up 2 lines, with the first one being for the x and the second for the y which is what I want to change

Heres the Move () Steps block:
replace item (? v) of [list v] with ((item (? v) of [list v] :: list) + (([sin v] of (direction)) * (Amt. Of steps))) // X position
replace item (? v) of [list v] with ((item (? v) of [list v] :: list) + (([cos v] of (direction)) * (Amt. Of steps))) // Y position

thanks! although for some reason it's buggy and don't work correctly by having them go to that location, they all just go to the same place and it's the same place every single time
If you gave me the project link i could go and see why it does that

I actually just found the bug lol, forgot I had some remnants of the old system in that was causing it to break lol
kfchikinfiter
Scratcher
100+ posts

how to recreate move block using code

DeveloperTools wrote:

kfchikinfiter wrote:

DeveloperTools wrote:

kfchikinfiter wrote:

Vitou_ wrote:

this its hard, but, I think you can use a variable X and Y
I'm using a list with each enemy taking up 2 lines, with the first one being for the x and the second for the y which is what I want to change

Heres the Move () Steps block:
replace item (? v) of [list v] with ((item (? v) of [list v] :: list) + (([sin v] of (direction)) * (Amt. Of steps))) // X position
replace item (? v) of [list v] with ((item (? v) of [list v] :: list) + (([cos v] of (direction)) * (Amt. Of steps))) // Y position

thanks! although for some reason it's buggy and don't work correctly by having them go to that location, they all just go to the same place and it's the same place every single time
If you gave me the project link i could go and see why it does that
alright, alright, I need some help, the enemies won't go towards the player even though I literally have them point to the player beforehand yet they don't even point in that direction after they are drawn, you'll see what I mean https://scratch.mit.edu/projects/733726128
RT_Borg
Scratcher
1000+ posts

how to recreate move block using code

Hi kfchikinfiter,

The enemies seem to move toward and follow the player. Am I missing something, or did you solve your problem? If you still need help with this, please describe what we should be looking for.

– RT_Borg
kfchikinfiter
Scratcher
100+ posts

how to recreate move block using code

RT_Borg wrote:

Hi kfchikinfiter,

The enemies seem to move toward and follow the player. Am I missing something, or did you solve your problem? If you still need help with this, please describe what we should be looking for.

– RT_Borg
I have gotten it fixed, thanks to @DeveloperTools thank you though
SquaredNES
Scratcher
1 post

how to recreate move block using code

This is useful for if you want sprites with physics to slide against each other, with modern scratch. In short, I love
define move(steps)
change x by ((steps) * ([sin] of (direction)))
change y by ((steps) * ([cos] of (direction)))
battle_rtx
Scratcher
2 posts

how to recreate move block using code

thank you guys so much i want to show u guys apreciation for helping everyone so thanks you guys have helped me with my game and i was struggling with having a custom game engine and custom x and y cordinates so this helped me bypass coding without move blocks and doing it in a particular order so thank you so much!!!

Last edited by battle_rtx (Nov. 9, 2023 15:54:39)

Powered by DjangoBB