Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: ncw14 on Sat 09/02/2008 06:12:59

Title: Player Coordinates = Mouse Coordinates
Post by: ncw14 on Sat 09/02/2008 06:12:59
Im trying to make the player walk to where the mouse is at all time however in reapeatedly execute when i do ANY walk command the charecter seems to be trying to walk but cant but here is my code in repeatedly execute

player.WalkStraight(mouse.x,  mouse.y);

how could i get this to work


ps. if that wont work

how would i code that if mouse animating using view 3, that it animates left right up and down other than just the down loop?
Title: Re: Player Coordinates = Mouse Coordinates
Post by: Radiant on Sat 09/02/2008 09:05:29
Try this:


function repeatedly_execute () {
  if (character[EGO].x < mouse.x) character[EGO].x ++;
  if (character[EGO].x > mouse.x) character[EGO].x --
  if (character[EGO].y < mouse.y) character[EGO].y ++;
  if (character[EGO].y > mouse.y) character[EGO].y --;
}

Title: Re: Player Coordinates = Mouse Coordinates
Post by: ncw14 on Sat 09/02/2008 19:30:52
the character moves but facing right no matter in what direction, moves very slow and doesnt animate so that doesnt work
Title: Re: Player Coordinates = Mouse Coordinates
Post by: Shane 'ProgZmax' Stevens on Sat 09/02/2008 20:27:44
Try substituting the .x adjustments with a non-block cEgo.Walk to the mouse current position:

if(player.x != mouse.x || player.y != mouse.y)
{
if(player.Moving==0)
player.Walk(mouse.x, mouse.y, eNoBlock);

}



One thing to be aware of is that since it allows him to move only when he has stopped first you may need a check to see if the mouse has moved and then stop the player so he can move toward the new mouse position.  The player.Moving check is necessary so he can animate properly.
Title: Re: Player Coordinates = Mouse Coordinates
Post by: ncw14 on Sat 09/02/2008 20:33:44
This isnt working how i want it to, but is there something (without character) i could do say

if mouse moving left change image and animate the loop

kinda hard for me to word it

Kinda like in twilight princess for wii the mouse is the fairy navi but it animates like a character would moving up down left and right
Title: Re: Player Coordinates = Mouse Coordinates
Post by: Baron on Sun 10/02/2008 03:56:04
To get your player to face the right direction is pretty easy: the trick would be to get him moving fluidly.
The walk cycle is probably choppy because you are calling the walk command again and again every single game cycle (40 times a second), which prevents the whole loop from running.  The best way to solve this is to not have your player "walk" at all.  Since you are updating his x/y coordinates manually, just set the character idle (in his walk view) if his position is not equal to the mouse position. Something like this:

--TOP OF MAIN SCRIPT--
int xdiff;
int ydiff;
int playmove; //0 if stopped, 1 if moving  (could be boolean....)


-- REPEATEDLY EXECUTE --
if (player.x == mouse.x && player.y == mouse.y) {
       player.SetIdleView (-1, 0);   // change to standing view
       playmove =0; }
}
if (player.x  != mouse.x || player.y != mouse.y){
      if (playmove ==0) {   //this stops the idle view from being constantly restarted
            player.SetIdleView (____, 0);
            playmove =1;
      }
  xdiff = player.x -mouse.x;
  if (xdiff < 0) xdiff *-1; //creating absolute distances (I don't see a math function in the manual to do it).
  ydiff = player.y - mouse.y;
  if (ydiff < 0) ydiff * -1;
  if (xdiff > ydiff){   //the side to side distance from mouse to player is greater than up down distance
      player.FaceLocation (mouse.x, player.y);
   }
   else { player.FaceLocation (player.x, mouse.y);  }

   //and finally you'd put the movement code, much as Radiant has it above, only in the new syntax

      if (player.x < mouse.x) player.x ++;
       if (player> mouse.x) player.x --;
       if (player.y < mouse.y) player.y ++;
       if (player.y > mouse.y) player.y --;
}


Of course I haven't tried any of this, but hopefully it will give you a good start.

Baron
Title: Re: Player Coordinates = Mouse Coordinates
Post by: ncw14 on Sun 10/02/2008 05:39:42
this isnt working, parse error with xdiff the first time it comes up
Title: Re: Player Coordinates = Mouse Coordinates
Post by: Ubel on Sun 10/02/2008 09:58:50
Here's my rather more simple version:


int pos_x, pos_y;

function repeatedly_execute() {

if (pos_x != mouse.x && pos_y != mouse.y) {
pos_x = mouse.x;
pos_y = mouse.y;
player.Walk(pos_x, pos_y, eNoBlock, eWalkableAreas);
}

}


I tested it and it worked fine for me. Works best if you turn anti-glide mode off and change the character's Turn Before Moving value to False.
Title: Re: Player Coordinates = Mouse Coordinates
Post by: Baron on Sun 10/02/2008 22:25:07
Ah, you might have to put brackets around those (-1) to multiply them.  You did declare the variables, right?  If you are using repeatedly execute in a room then you can declare them at the top of the room script.

But try Pablo's script, since he's got it to work and I just made mine up.  The simpler approach is appealing also.

Baron