Discuss Scratch
- Discussion Forums
 - » Help with Scripts
 - » moving an object with the mouse "click"
        
         
- pachelbel2
 - 
                            
						
						
                            Scratcher
                        
						
						 
8 posts
moving an object with the mouse "click"
So basically, I need an object to move (glide) only in the x position (left or right) using the click of the mouse, kinda like when we move the sprites in the project editor. Also, there is a limit to where the object can go (the ‘x’ position can't be less than 30 or more than 300). Does anyone know how I can possibly do something like this? My backup plan is to use the arrow buttons on the keyboard to make it work, but I don't even know how to do that either. Please help me.
                        
                        
                    - FindingPepper3
 - 
                            
						
						
                            Scratcher
                        
						
						 
32 posts
moving an object with the mouse "click"
From what I can understand, you are looking to do this (you'll need to create a “dragging” variable):
This should allow you to drag the object to the left and right, but not left or right of the coordinates you specified.
One note: The stage stretches from an x of -240 to an x of 240. So just fyi, 300 is far off the right edge of the screen.
                        
                        
                    when green flag clicked
forever
if <not <mouse down?>> then
set [dragging v] to [0]
end
if <<touching [mouse pointer v] ?> and <mouse down?>> then
set [dragging v] to [1]
end
if <(dragging) = [1]> then
set x to (mouse x)
end
if <(x position) < (30)> then
set x to (30)
end
if <(x position) > (300)> then
set x to (300)
end
end
This should allow you to drag the object to the left and right, but not left or right of the coordinates you specified.
One note: The stage stretches from an x of -240 to an x of 240. So just fyi, 300 is far off the right edge of the screen.
- pachelbel2
 - 
                            
						
						
                            Scratcher
                        
						
						 
8 posts
moving an object with the mouse "click"
From what I can understand, you are looking to do this (you'll need to create a “dragging” variable):when green flag clicked
forever
if <not <mouse down?>> then
set [dragging v] to [0]
end
if <<touching [mouse pointer v] ?> and <mouse down?>> then
set [dragging v] to [1]
end
if <(dragging) = [1]> then
set x to (mouse x)
end
if <(x position) < (30)> then
set x to (30)
end
if <(x position) > (300)> then
set x to (300)
end
end
This should allow you to drag the object to the left and right, but not left or right of the coordinates you specified.
One note: The stage stretches from an x of -240 to an x of 240. So just fyi, 300 is far off the right edge of the screen.
https://scratch.mit.edu/projects/882153089/
“So, here is the project I'm working on(The middle triangle is the one that's supposed to move. Ignore the button). What you said was more or less what I was thinking, but I don't know why the ‘mouse x’ and ‘go to mouse pointer’ don't work. The object goes only to the right for some reason and doesn't follow the mouse. I tried the ‘set x to mouse position’ solution, but it isn't limited either. (I'm leaving the part where the object's x position is limited to the end.)”
Last edited by pachelbel2 (Aug. 13, 2023 01:35:22)
- pachelbel2
 - 
                            
						
						
                            Scratcher
                        
						
						 
8 posts
moving an object with the mouse "click"
i meant to say that ‘set x to mouse position’ solution doesnt work eitherFrom what I can understand, you are looking to do this (you'll need to create a “dragging” variable):when green flag clicked
forever
if <not <mouse down?>> then
set [dragging v] to [0]
end
if <<touching [mouse pointer v] ?> and <mouse down?>> then
set [dragging v] to [1]
end
if <(dragging) = [1]> then
set x to (mouse x)
end
if <(x position) < (30)> then
set x to (30)
end
if <(x position) > (300)> then
set x to (300)
end
end
This should allow you to drag the object to the left and right, but not left or right of the coordinates you specified.
One note: The stage stretches from an x of -240 to an x of 240. So just fyi, 300 is far off the right edge of the screen.
https://scratch.mit.edu/projects/882153089/
“So, here is the project I'm working on(The middle triangle is the one that's supposed to move. Ignore the button). What you said was more or less what I was thinking, but I don't know why the ‘mouse x’ and ‘go to mouse pointer’ don't work. The object goes only to the right for some reason and doesn't follow the mouse. I tried the ‘set x to mouse position’ solution, but it isn't limited either. (I'm leaving the part where the object's x position is limited to the end.)”
- NeonG4
 - 
                            
						
						
                            Scratcher
                        
						
						 
1000+ posts
moving an object with the mouse "click"
Okay, you used the change block, not the set block. Additionally, you will want to position the triangle at the center of the canvas in the costume editor, and change the goto at the start to (0, 90)i meant to say that ‘set x to mouse position’ solution doesnt work eitherFrom what I can understand, you are looking to do this (you'll need to create a “dragging” variable):when green flag clicked
forever
if <not <mouse down?>> then
set [dragging v] to [0]
end
if <<touching [mouse pointer v] ?> and <mouse down?>> then
set [dragging v] to [1]
end
if <(dragging) = [1]> then
set x to (mouse x)
end
if <(x position) < (30)> then
set x to (30)
end
if <(x position) > (300)> then
set x to (300)
end
end
This should allow you to drag the object to the left and right, but not left or right of the coordinates you specified.
One note: The stage stretches from an x of -240 to an x of 240. So just fyi, 300 is far off the right edge of the screen.
https://scratch.mit.edu/projects/882153089/
“So, here is the project I'm working on(The middle triangle is the one that's supposed to move. Ignore the button). What you said was more or less what I was thinking, but I don't know why the ‘mouse x’ and ‘go to mouse pointer’ don't work. The object goes only to the right for some reason and doesn't follow the mouse. I tried the ‘set x to mouse position’ solution, but it isn't limited either. (I'm leaving the part where the object's x position is limited to the end.)”
- pachelbel2
 - 
                            
						
						
                            Scratcher
                        
						
						 
8 posts
moving an object with the mouse "click"
Okay, you used the change block, not the set block. Additionally, you will want to position the triangle at the center of the canvas in the costume editor, and change the goto at the start to (0, 90)i meant to say that ‘set x to mouse position’ solution doesnt work eitherFrom what I can understand, you are looking to do this (you'll need to create a “dragging” variable):when green flag clicked
forever
if <not <mouse down?>> then
set [dragging v] to [0]
end
if <<touching [mouse pointer v] ?> and <mouse down?>> then
set [dragging v] to [1]
end
if <(dragging) = [1]> then
set x to (mouse x)
end
if <(x position) < (30)> then
set x to (30)
end
if <(x position) > (300)> then
set x to (300)
end
end
This should allow you to drag the object to the left and right, but not left or right of the coordinates you specified.
One note: The stage stretches from an x of -240 to an x of 240. So just fyi, 300 is far off the right edge of the screen.
https://scratch.mit.edu/projects/882153089/
“So, here is the project I'm working on(The middle triangle is the one that's supposed to move. Ignore the button). What you said was more or less what I was thinking, but I don't know why the ‘mouse x’ and ‘go to mouse pointer’ don't work. The object goes only to the right for some reason and doesn't follow the mouse. I tried the ‘set x to mouse position’ solution, but it isn't limited either. (I'm leaving the part where the object's x position is limited to the end.)”
Im trying a bunch of things, i tried the set block it too but it didnt work either, the change block with the operation “length of mouse x” kinda works for some reason, but it only goes to the left XD
Last edited by pachelbel2 (Aug. 13, 2023 02:03:49)
- NeonG4
 - 
                            
						
						
                            Scratcher
                        
						
						 
1000+ posts
moving an object with the mouse "click"
snipBro, do these three things:
1) Adjust the costume so it is centered on the canvas
2) Change the change block to a set x position to mouse x
3) Adjust the starting value
All of these changes are in the Sprite4 sprite, and they will fix your problem if you do exactly them. If you only do one fix, then it won't work.
Please please change all of these at once.
- pachelbel2
 - 
                            
						
						
                            Scratcher
                        
						
						 
8 posts
moving an object with the mouse "click"
snipBro, do these three things:
1) Adjust the costume so it is centered on the canvas
2) Change the change block to a set x position to mouse x
3) Adjust the starting value
All of these changes are in the Sprite4 sprite, and they will fix your problem if you do exactly them. If you only do one fix, then it won't work.
Please please change all of these at once.
dont realy know how to do ‘1’ and what you mean in ‘3’, im realy dont know how to program XD
whats is the canvas?
- NeonG4
 - 
                            
						
						
                            Scratcher
                        
						
						 
1000+ posts
moving an object with the mouse "click"
1) move the entire costume to the centersnipBro, do these three things:
1) Adjust the costume so it is centered on the canvas
2) Change the change block to a set x position to mouse x
3) Adjust the starting value
All of these changes are in the Sprite4 sprite, and they will fix your problem if you do exactly them. If you only do one fix, then it won't work.
Please please change all of these at once.
dont realy know how to do ‘1’ and what you mean in ‘3’, im realy dont know how to program XD
whats is the canvas?
3) adjust the starting goto (x, y) value to (0, 90)
- pachelbel2
 - 
                            
						
						
                            Scratcher
                        
						
						 
8 posts
moving an object with the mouse "click"
1) move the entire costume to the centersnipBro, do these three things:
1) Adjust the costume so it is centered on the canvas
2) Change the change block to a set x position to mouse x
3) Adjust the starting value
All of these changes are in the Sprite4 sprite, and they will fix your problem if you do exactly them. If you only do one fix, then it won't work.
Please please change all of these at once.
dont realy know how to do ‘1’ and what you mean in ‘3’, im realy dont know how to program XD
whats is the canvas?
3) adjust the starting goto (x, y) value to (0, 90)
I really appreciate your help, and I totally understand if you give up on me, but I'm really not very familiar with Scratch or programming. If you mean that the whole ‘monocordio’ should be in the center, I really need it at the top (there will be more stuff later), and I don't really know what you mean with the starting ‘go to’. If you'd be kind enough to show me the code, I'd appreciate it very much, but it's okay if you don't really have the time to do so
- NeonG4
 - 
                            
						
						
                            Scratcher
                        
						
						 
1000+ posts
moving an object with the mouse "click"
1) move the entire costume to the centersnipBro, do these three things:
1) Adjust the costume so it is centered on the canvas
2) Change the change block to a set x position to mouse x
3) Adjust the starting value
All of these changes are in the Sprite4 sprite, and they will fix your problem if you do exactly them. If you only do one fix, then it won't work.
Please please change all of these at once.
dont realy know how to do ‘1’ and what you mean in ‘3’, im realy dont know how to program XD
whats is the canvas?
3) adjust the starting goto (x, y) value to (0, 90)
I really appreciate your help, and I totally understand if you give up on me, but I'm really not very familiar with Scratch or programming. If you mean that the whole ‘monocordio’ should be in the center, I really need it at the top (there will be more stuff later), and I don't really know what you mean with the starting ‘go to’. If you'd be kind enough to show me the code, I'd appreciate it very much, but it's okay if you don't really have the time to do so
go to x: (0) y: (90) // <== this blockIn sprite4, change the block inputs to this.
Also in Sprite4, where the if block is empty, put this:
set x to (mouse x)so it looks like this:
when green flag clicked
forever
if <not <mouse down?>> then
set [drag v] to [0]
end
if <<touching [mouse pointer v] ?> and <mouse down?>> then
set [drag v] to [1]
if <(drag) = [1]> then
set x to (mouse x)
end
end
end
Additionally, for costume1, zoom out, press ctrl + a, and drag the triangle to the center of the editor.
- pachelbel2
 - 
                            
						
						
                            Scratcher
                        
						
						 
8 posts
moving an object with the mouse "click"
1) move the entire costume to the centersnipBro, do these three things:
1) Adjust the costume so it is centered on the canvas
2) Change the change block to a set x position to mouse x
3) Adjust the starting value
All of these changes are in the Sprite4 sprite, and they will fix your problem if you do exactly them. If you only do one fix, then it won't work.
Please please change all of these at once.
dont realy know how to do ‘1’ and what you mean in ‘3’, im realy dont know how to program XD
whats is the canvas?
3) adjust the starting goto (x, y) value to (0, 90)
I really appreciate your help, and I totally understand if you give up on me, but I'm really not very familiar with Scratch or programming. If you mean that the whole ‘monocordio’ should be in the center, I really need it at the top (there will be more stuff later), and I don't really know what you mean with the starting ‘go to’. If you'd be kind enough to show me the code, I'd appreciate it very much, but it's okay if you don't really have the time to do sogo to x: (0) y: (90) // <== this blockIn sprite4, change the block inputs to this.
Also in Sprite4, where the if block is empty, put this:set x to (mouse x)so it looks like this:when green flag clicked
forever
if <not <mouse down?>> then
set [drag v] to [0]
end
if <<touching [mouse pointer v] ?> and <mouse down?>> then
set [drag v] to [1]
if <(drag) = [1]> then
set x to (mouse x)
end
end
end
Additionally, for costume1, zoom out, press ctrl + a, and drag the triangle to the center of the editor.
Hey, thank you, it worked! Now the only problem I have is with the small triangle hitbox. Do you know how I can make it bigger? I tried drawing a bigger invisible box in the sprite editor; it works if it has color, but when it's invisible, it doesn't work as a hitbox at all.
- pachelbel2
 - 
                            
						
						
                            Scratcher
                        
						
						 
8 posts
moving an object with the mouse "click"
1) move the entire costume to the centersnipBro, do these three things:
1) Adjust the costume so it is centered on the canvas
2) Change the change block to a set x position to mouse x
3) Adjust the starting value
All of these changes are in the Sprite4 sprite, and they will fix your problem if you do exactly them. If you only do one fix, then it won't work.
Please please change all of these at once.
dont realy know how to do ‘1’ and what you mean in ‘3’, im realy dont know how to program XD
whats is the canvas?
3) adjust the starting goto (x, y) value to (0, 90)
I really appreciate your help, and I totally understand if you give up on me, but I'm really not very familiar with Scratch or programming. If you mean that the whole ‘monocordio’ should be in the center, I really need it at the top (there will be more stuff later), and I don't really know what you mean with the starting ‘go to’. If you'd be kind enough to show me the code, I'd appreciate it very much, but it's okay if you don't really have the time to do sogo to x: (0) y: (90) // <== this blockIn sprite4, change the block inputs to this.
Also in Sprite4, where the if block is empty, put this:set x to (mouse x)so it looks like this:when green flag clicked
forever
if <not <mouse down?>> then
set [drag v] to [0]
end
if <<touching [mouse pointer v] ?> and <mouse down?>> then
set [drag v] to [1]
if <(drag) = [1]> then
set x to (mouse x)
end
end
end
Additionally, for costume1, zoom out, press ctrl + a, and drag the triangle to the center of the editor.
Hey, thank you, it worked! Now the only problem I have is with the small triangle hitbox. Do you know how I can make it bigger? I tried drawing a bigger invisible box in the sprite editor; it works if it has color, but when it's invisible, it doesn't work as a hitbox at all.
Already solved, thanks for your help!
 - Discussion Forums
 - » Help with Scripts
 - 
            » moving an object with the mouse "click" 
         
            

