Game Maker's Garage Forum

Game Creation => SilverCreator => Topic started by: Chamez on May 24, 2010, 07:38:48 AM

Title: Move Sprite to Mouse Click?
Post by: Chamez on May 24, 2010, 07:38:48 AM
How do you move a sprite to mouse click, and make it smooth instead of just appearing to the mouse?
Title: Re: Move Sprite to Mouse Click?
Post by: Gan on May 24, 2010, 07:58:44 AM
I haven't used Sc in quite a while, so I'll give some psuedo-code:
Code: [Select]
If spritex(1) < mousex() Then
spritex(1) = spritex(1) + 1
End If
If spritex(1) > mousex() Then
spritex(1) = spritex(1) - 1
End If
If spritey(1) < mousey() Then
spritey(1) = spritey(1) + 1
End If
If spritey(1) > mousey() Then
spritey(1) = spritey(1) - 1
End If
This makes it so that the sprite moves one pixel closer to where the mouse clicked every tick.


-Gan
Title: Re: Move Sprite to Mouse Click?
Post by: Chamez on May 25, 2010, 07:40:43 AM
Didn't work.
Title: Re: Move Sprite to Mouse Click?
Post by: Teruri on May 25, 2010, 10:11:49 AM
This should do the trick;

Code: [Select]
ON TIMER 1
   IF spritex(1) < mousex Then
      LET spritex(1) = spritex(1) + 1
   END IF
   IF spritex(1) > mousex Then
      LET spritex(1) = spritex(1) - 1
   END IF
   IF spritey(1) < mousey Then
      LET spritey(1) = spritey(1) + 1
   END IF
   IF spritey(1) > mousey Then
      LET spritey(1) = spritey(1) - 1
   END IF
END TIMER

I used this in the Open Card event to test it and it worked. Note that it's a slightly modified version of Gandolf's script.
Title: Re: Move Sprite to Mouse Click?
Post by: Mystor on May 25, 2010, 10:24:33 AM
Quote
Didn't work.
Yea, Gandolf's code was pseudo-code which doesn't use the right syntax but instead gives you an idea of what the right syntax should look like.
Title: Re: Move Sprite to Mouse Click?
Post by: WarHampster on May 25, 2010, 12:03:09 PM
You don't learn anything by copy and pasting ;)