Point and Click games--moving actor to Mouse X and Mouse Y

rofurumao

  • Posts: 2
Hi everyone :) I want to make a simple behavior for a point and click adventure game (think Monkey Island). Very basic question though--how do I move an actor to where I clicked the mouse? I know I have to get the MouseX and Y but I don't know how.

EDIT :

Wait--I got it. Now I need to find out how to MOVE the actor instead of setting its position. Using push didn't seem to work.

Abigayl

  • Posts: 807
If you follow the system I show in Tutorial 4 (http://www.kongregate.com/games/Abigayl/stencyl-tutorial-4-mouse-based-shooting) for doing mouse-based shooting, you can send the actor in the direction of the mouse (with some minor modifications to the code). If you want the actor to STOP at the location the mouse was, store the location of the mouse in the actor's coding and set it to stop once it reaches the mouse's old location (might have to use a small region around the mouse-click's area since the actor might not actually hit the exact point).

If you want to, you can also modify the coding in Tutorial 4 to be the "slide" tween, but again add a check at the end (this time a "do after") to make sure the ending is the correct position.
If you need help, please check my tutorials either here or Kongregate (http://www.kongregate.com/accounts/Abigayl) or come over to my stream: http://www.livestream.com/poweredbyflowers

rofurumao

  • Posts: 2
If you follow the system I show in Tutorial 4 (http://www.kongregate.com/games/Abigayl/stencyl-tutorial-4-mouse-based-shooting) for doing mouse-based shooting, you can send the actor in the direction of the mouse (with some minor modifications to the code). If you want the actor to STOP at the location the mouse was, store the location of the mouse in the actor's coding and set it to stop once it reaches the mouse's old location (might have to use a small region around the mouse-click's area since the actor might not actually hit the exact point).

If you want to, you can also modify the coding in Tutorial 4 to be the "slide" tween, but again add a check at the end (this time a "do after") to make sure the ending is the correct position.

Thanks for the help!

Yes, I followed that part of your tutorial before and helped during my initial experiments--the problem was me modifying it.  For some reason the character moved , but not towards the Mouse location.

Code: [Select]
always
if mouse is down
push self gradually towards x : x of mouse press, y : y of mouse press at 30 force

The 30 force is a placeholder. It keeps moving towards the lower right corner of the screen, not the mouse position. It worked if I just set its position though.

EDIT: Nevermind, it worked! My mistake is using "BY" instead of "TO".

« Last Edit: March 27, 2012, 08:57:03 am by rofurumao »

Abigayl

  • Posts: 807
Glad you got it to work :)

Please make sure to edit your first post to add [SOLVED] to the title.
If you need help, please check my tutorials either here or Kongregate (http://www.kongregate.com/accounts/Abigayl) or come over to my stream: http://www.livestream.com/poweredbyflowers

stoneyftw

  • Posts: 25
Thanks for this.  I'm using it for my game!

I knew to use SOHCAHTOA, but it was nice to have it all laid out already for me.

pixelhunter

  • Posts: 11
Sorry... I'm way late to the discussion! But I wondered if you might share the successful solution that you found for this? I'm trying to implement point-click-move mechanic as well, and I am stuck on the same problem that you mentioned - my character moves upon clicking, but not towards where I clicked - he moves towards the bottom right of the screen. How did you fix that?

I am having the same problem with the actor moving to the bottom right of the screen. if anyone can explain why and what is needed to fix it that would great.

right now, my code block is as follows.

Code: [Select]
Always
if mouse is down
push self gently towards x: x of mouse press y: y of mouse press at 100 force

no matter where I click the ball moves to the bottom right of the game screen.
any help would be great.

Thank you.

gamegirlxl

  • Posts: 713
(For the actor you want to move: )

Code: [Select]
When mouse is released
         set x of self to x of mouse
         set y of self to y of mouse

That should work, unless you want to GLIDE to that location...