[Solved ]Walk position and x,y position of character

Started by omegamit6zeichen, Tue 15/07/2014 11:31:59

Previous topic - Next topic

omegamit6zeichen

Hi, i wanted to write a function that lets the character walk to a specific point and after that pick up an item.

Code: ags

function game_start()
{
  currObj=null;
  currChar=null;
  currItem = null;
  visible=eVisible;
  MovesToItem=false;
}
void WalkToPickUpItem(Character *actChar, Object *obj, InventoryItem *invItem, String onPickup, VisibleAfter vis)
{
  currObj=obj;
  currChar=actChar;
  currItem=invItem;
  XInteractPos=obj.X-XMARGIN;
  YInteractPos=obj.Y-YMARGIN;
  sOnPickup=onPickup;
  visible=vis;
  actChar.Walk(XInteractPos, YInteractPos, eNoBlock, eWalkableAreas);
  MovesToItem=true;
}

function repeatedly_execute()
{
  if(MovesToItem)
  if(currObj != null && currChar != null && currItem != null)
  {
    if(currObj.Visible==true)
    {
      if(currChar.x == XInteractPos && currChar.y == YInteractPos)
      {
        currChar.AddInventory(currItem);
        if(visible != eVisible)
        currObj.Visible=false;
        
        currChar.Say(sOnPickup);
      }
    }
  }
}


the character walks to the specified position but then it does not pick up the item
i think the problem is here:
Code: ags

if(currChar.x == XInteractPos && currChar.y == YInteractPos)

My thoughts about it:
because the char.x, char.y position are the sprite position on the top left side
while the move x,y are the somwhere @ the feet of the character

but i dont know exactly how they are calculated
something like walkx=(char.x +char.width/2) walky=char.y+char.height ?

or is there an easier way to achieve this?

Khris

What you describe is most likely not why this doesn't work; given the code you use it is irrelevant where exactly the hotspots of characters and objects are; you're sending the character to walk to (XInteractPos, YInteractPos), then check whether the character has arrived there; this should work just fine (and I'm using the same method in my GotThere module).
Also, character sprites are drawn so their bottom center is at the character's coordinates, as in walk.x = char.x, walk.y = char.y

I'm not really sure what the problem is, but try this first:
Code: ags
function repeatedly_execute()
{
  if (MovesToItem && currChar.x == XInteractPos && currChar.y == YInteractPos)
  {
    currChar.AddInventory(currItem);
    currObj.Visible = visible;
    currChar.Say(sOnPickup);
    MovesToItem = false;
  }
}

omegamit6zeichen

ty for your fast answer,
i've solved the problem .. it was much easier ... the walkable area was a few pixel away from the position where i wanted to go ...

it will try to get an alogrithm which finds the best position around an item ...(because i dont want the char to stand on the item)


SMF spam blocked by CleanTalk