Move player to mouse coordinates

Started by Glenjamin, Tue 30/01/2018 16:08:01

Previous topic - Next topic

Glenjamin

Hello everyone,

I wanted to program a unique kind of movement for a part in my game where the player is on a raft.

My plan was to make a region that when clicked on, sets the player to walk to the mouse location, but with some distance added to simulate momentum.

Also it would be blocked so the player could only move to one location, then wait, and then move to another location to simulate the raft only moving when they paddle with an oar

Code: ags
function hswimtest_AnyClick()
{
//if the mouse is in front of the player
 if (mouse.x > player.x){
    player.Move(mouse.x+5, mouse.y, eNoBlock, eAnywhere);

//if the mouse is behind the player    
   }else  if (mouse.x < player.x){
     player.Move(mouse.x-5, mouse.y, eBlock, eAnywhere);

   }  
}


The issue is the mouse coordinates seem to change to the far left of the screen, and the character moves there instead of where the mouse had clicked.

As far as I can tell the in-engine walk mechanics aren't interfering with this. Thoughts?

selmiak

is this a scrolling room? Then you have to add the viewport coordinates to the mouse coordinates.

Glenjamin

Quoteis this a scrolling room? Then you have to add the viewport coordinates to the mouse coordinates.

This IS a scrolling room!

How would I go about adding the viewport coordinates to the mouse coordinates?


dayowlron

Code: ags

function hswimtest_AnyClick()
{
//if the mouse is in front of the player
 if (mouse.x > player.x){
    player.Move(GetViewPortX() + mouse.x+5, GetViewPortY() + mouse.y, eNoBlock, eAnywhere);
 
//if the mouse is behind the player    
   }else  if (mouse.x < player.x){
     player.Move(GetViewPortX() + mouse.x-5, GetViewPortY() + mouse.y, eBlock, eAnywhere);
 
   }  
}
Pro is the opposite of Con                       Kids of today are so much different
This fact can clearly be seen,                  Don't you know?
If progress means to move forward         Just ask them where they are from
Then what does congress mean?             And they tell you where you can go.  --Nipsey Russell

Crimson Wizard

#5
dayowlron, you forgot to subtract Viewport coordinate from player.x:
Code: ags

if (mouse.x > player.x - GetViewportX())


IMO its better to translate everything into room coordinates first. This way the further calculations will be much simplier.

Code: ags

int room_click_x = mouse.x + GetViewportX();
int room_click_y = mouse.y + GetViewportY();
if (room_click_x  > player.x){
    player.Move(room_click_x + 5, room_click_y, eNoBlock, eAnywhere);

and so on...

EDIT: my apologies, did wrong conversion :) fixed now.

dayowlron

Pro is the opposite of Con                       Kids of today are so much different
This fact can clearly be seen,                  Don't you know?
If progress means to move forward         Just ask them where they are from
Then what does congress mean?             And they tell you where you can go.  --Nipsey Russell

SMF spam blocked by CleanTalk