Discuss Scratch

  • Discussion Forums
  • » Help with Scripts
  • » how do you make a sprite if specific key pressed (ex: space button) will go to the mouse pointer but stop before it reaches the mouse pointer (add speed) [RSS Feed]
Pabello_Aquilla
New Scratcher
4 posts

how do you make a sprite if specific key pressed (ex: space button) will go to the mouse pointer but stop before it reaches the mouse pointer (add speed)

Hi, i'm a new scratcher and i want to make my own game, problem is i cant find any solution)

what i want when a specified key is pressed, the sprite will go towards the mouse pointer (with speed that is changable) and before it reaches the mouse pointer, it will stop (i need a code that i can customize it where it will stop), thats all thx

(btw no fake codes)
RokCoder
Scratcher
1000+ posts

how do you make a sprite if specific key pressed (ex: space button) will go to the mouse pointer but stop before it reaches the mouse pointer (add speed)

I gave you the solution to this yesterday in your previous forum post

set [speed v] to (5) // Set this to the speed at which you want the sprite to move towards the mouse pointer
set [nearest v] to (50) // Set this to the distance from the mouse pointer at which you want the sprite to stop movement
set [activate v] to (0) // Sprite is not activates to move towards mouse pointer when we first start
forever // This represents your main game-loop
if <key [space v] pressed?> then // Activate the sprite movement when space is pressed
set [activate v] to (1)
end
if <(activate) = (1)> then
point towards [mouse-pointer v]
if <(distance to [mouse-pointer v]) > (nearest)> then
move (speed) steps
else
set [activate v] to (0) // Deactivate the movement (which allows for the player to reactivate by pressing space again)
end
end

If you want something more specific, share your project with what you've already implemented. You can then state what areas need improvement or which areas aren't working as you intend them to.
TheCoder12365
Scratcher
100+ posts

how do you make a sprite if specific key pressed (ex: space button) will go to the mouse pointer but stop before it reaches the mouse pointer (add speed)

RokCoder wrote:

I gave you the solution to this yesterday in your previous forum post

set [speed v] to (5) // Set this to the speed at which you want the sprite to move towards the mouse pointer
set [nearest v] to (50) // Set this to the distance from the mouse pointer at which you want the sprite to stop movement
set [activate v] to (0) // Sprite is not activates to move towards mouse pointer when we first start
forever // This represents your main game-loop
if <key [space v] pressed?> then // Activate the sprite movement when space is pressed
set [activate v] to (1)
end
if <(activate) = (1)> then
point towards [mouse-pointer v]
if <(distance to [mouse-pointer v]) > (nearest)> then
move (speed) steps
else
set [activate v] to (0) // Deactivate the movement (which allows for the player to reactivate by pressing space again)
end
end

If you want something more specific, share your project with what you've already implemented. You can then state what areas need improvement or which areas aren't working as you intend them to.

This all is correct, but just thought i'd say you should add a “wait until not space key pressed” to the end of the active script. That way it doesn't make the script work in strange ways.
RokCoder
Scratcher
1000+ posts

how do you make a sprite if specific key pressed (ex: space button) will go to the mouse pointer but stop before it reaches the mouse pointer (add speed)

TheCoder12365 wrote:

This all is correct, but just thought i'd say you should add a “wait until not space key pressed” to the end of the active script. That way it doesn't make the script work in strange ways.
You wouldn't want a wait in the main game loop. I think this'll be fine as it won't check again until the object is in range. If there are more problems, I'm sure we'll hear about them

Last edited by RokCoder (Feb. 27, 2025 14:48:57)

Pabello_Aquilla
New Scratcher
4 posts

how do you make a sprite if specific key pressed (ex: space button) will go to the mouse pointer but stop before it reaches the mouse pointer (add speed)

RokCoder wrote:

TheCoder12365 wrote:

This all is correct, but just thought i'd say you should add a “wait until not space key pressed” to the end of the active script. That way it doesn't make the script work in strange ways.
You wouldn't want a wait in the main game loop. I think this'll be fine as it won't check again until the object is in range. If there are more problems, I'm sure we'll hear about them

i tried it it doesnt work for me, im going to share it because i dont know what to do, (soccer ball sprite)

RokCoder-TestAccount
Scratcher
30 posts

how do you make a sprite if specific key pressed (ex: space button) will go to the mouse pointer but stop before it reaches the mouse pointer (add speed)

The only shared project I see is Pabello_PongGame, so I'll show you how to modify the Soccer Ball sprite for that project.

You have your current code for moving the ball -

when flag clicked
point in direction (45)
forever
move (15) steps
if on edge, bounce

Modify that so that it looks like this -

when flag clicked
point in direction (45)
set [_speed v] to (20)
set [_minDistance v] to (50)
forever
if <key [space v] pressed?> then // If you're pressing space, move towards mouse pointer
set [_dir v] to (direction) // Store the current direction the sprite is facing
repeat until <(distance to [mouse-pointer v]) < (_minDistance)> // Loop until the sprite is close enough to the mouse pointer
point towards [mouse-pointer v] // Turn to face the mouse pointer
move (_speed) steps // Move towards the mouse pointer
end
point in direction (_dir) // Face the original direction
else
move (15) steps // If you're not pressing space, move as normal
if on edge, bounce

Last edited by RokCoder-TestAccount (Feb. 28, 2025 08:16:21)

  • Discussion Forums
  • » Help with Scripts
  • » how do you make a sprite if specific key pressed (ex: space button) will go to the mouse pointer but stop before it reaches the mouse pointer (add speed) [RSS Feed]

Powered by DjangoBB