Topic:   Move Sprite to Mouse Click?   (Read 19206 times)


0 Members and 1 Guest are viewing this topic.

Chamez


  • GMG Newbie

  • *


  • Posts: 31

  • The Town Noob
Move Sprite to Mouse Click?
« 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?
I am the noob... Fear my stupid questions.

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: Move Sprite to Mouse Click?
« Reply #1 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

Chamez


  • GMG Newbie

  • *


  • Posts: 31

  • The Town Noob
Re: Move Sprite to Mouse Click?
« Reply #2 on: May 25, 2010, 07:40:43 AM »
Didn't work.
I am the noob... Fear my stupid questions.

Teruri


  • GMG-er

  • **


  • Posts: 167

  • This personal text couldn\'t be worse
Re: Move Sprite to Mouse Click?
« Reply #3 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.
Max 500; characters remaining: 466

Mystor


  • GMG-er

  • **


  • Posts: 696

  • I am the myst that consumes us all.
Re: Move Sprite to Mouse Click?
« Reply #4 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.
"I'll lie to him."
"And if that doesn't work?"
"Then I'll tell the truth. We're allowed to do that, in emergencies. We can't plan for everything, you know."
   -Colonel Graff, Ender&

WarHampster


  • GMG Extraordinaire

  • ***


  • Posts: 1501

  • The People's Moderator
    • Arcade of the Absurd
Re: Move Sprite to Mouse Click?
« Reply #5 on: May 25, 2010, 12:03:09 PM »
You don't learn anything by copy and pasting ;)