Make Object Animation Follow Main Character [SOLVED]

Started by Dervish, Tue 09/10/2007 07:36:24

Previous topic - Next topic

Dervish

OK I know this topic will probably be locked before I get a decent answer but here goes anyway.

I have an object (might have to make it a character)
The object is a dog and i want the dog to follow (with his head not the whole body) where the main character walks and snap at him.  So that if the character is on the right side of the dog he snaps and barks to his right and so on.
I have a right, right3/4, left, left 3/4 and center animation. 
How would i go about implementing it into the game with as much ease as possible?

Khris

Use the player's x coordinate.
Place all five barking animations in the first five loops of a view (loops 0-4).
Lock the object's view in before fadein.

Then put something like this in the room's repeatedly_execute:

Code: ags
int ol=-1;  // old loop

#sectionstart...          // created by AGS!
function roomx_x() {   // created by AGS!
  // ...                            created by AGS!
  
  float a=IntToFloat(player.x-160);  // replace 160 with the center x-coord of the dog's neck
  a=(a/50.0)+2.0;
  int l=FloatToInt(a, eRoundNearest);
  if (l<0) l=0;
  if (l>4) l=4;

  if (l != ol) {
    ol=l;
    oDoghead.Animate(l, delay, eRepeat, eNoBlock);
  }
}
#sectionend ... // created by AGS!


The anim will switch every 50 pixels. Adjust the 50 to your liking.

monkey0506

#2
Well probably the easiest way to set this up would be to set up a view with the right, 3/4 right, left, 3/4 left, and center animations in different loops. Set the object's view to that view and then you can use oDog.SetView to set the loop. Maybe something like this:

Code: ags
// room rep_ex
if ((player.x >= (oDog.X - 10)) && (player.x <= (oDog.X + 10))) oDog.SetView(oDog.View, VDOGCENTER); // player is within 10 pixels of dog (x-axis only)
else if ((player.x < oDog.X) && (player.x >= (oDog.X - 20))) oDog.SetView(oDog.View, VDOGMIDLEFT); // player is within 20 pixels to the left of dog (x-axis only)
else if ((player.x > oDog.X) && (player.x <= (oDog.X + 20))) oDog.SetView(oDog.View, VDOGMIDRIGHT); // player is within 20 pixels to the right of dog (x-axis only)
else if (player.x < oDog.X) oDog.SetView(oDog.View, VDOGLEFT); // player is to left of dog (x-axis only)
else if (player.x > oDog.X) oDog.SetView(oDog.View, VDOGRIGHT); // player is to right of dog (x-axis only)


The code is untested so in my sleep deprived state I may have mucked something up, but I think something like that should work. As denoted by my comments, I've only taken the x-axis into account though.

Nevermind...I took an insanely long time to write this post because I was chatting up the ladies on MSN and Khris's code would probably yield better results.

Dervish

Thank you Kris and Monkey... I ended up using yours Khris... One more question though... It now will turn as the player goes by but the animation of the snap and bark do not play out it only shows the first frame any suggestions

Khris

I'm not sure where the problem is, try the following:
In before fadein, after setting the view, put
Code: ags
  oDoghead.Animate(0, delay, eRepeat, eNoBlock);

Then comment out the Animate line after ol=l;.
Run the game.

The dog won't turn his head any longer, but if the animation plays ok, put
Code: ags
    oDog.Loop=l;

instead of the commented out Animate line.

If this works, it's even better than the original code because the animation will continue while the head turns.

Dervish

#5
Khris I tried to send you a message.  not sure if it worked or not.  The animation does work but the code gave me an error like     'Object::Loop' is read only is there anyway around this?


Well I figured it out... instead of using "L" to set the animation I used OL and that seemed to work perfect.  I also made OL a global Variable.

But thank you for all yall's help

Khris

Yeah, sorry, .Loop is read only for objects (confused it with characters).

And I believe I know what the error was.

The whole point of including the "// created by AGS!" lines was to make sure you'd place "int ol=-1;" outside the rep_ex function. ol doesn't have to be global, but it can't be local to the function either, otherwise it's initialized to -1 every time.

Putting it in the room script but outside the function makes it local to the room; that's sufficient in this case.

The whole old loop business is only there to make sure the .Animate command is only called when necessary; otherwise it's called 40 times a second, i.e. the Animation is run from frame 0 40 times a second, i.e. only the first frame will ever show.

It doesn't matter whether you use ol or l in the command, they're the same by that time anyway. But I assume you changed it to ol and made ol global (which fixed the problem) before testing it again.

monkey0506

It's possible to set an Object's loop using the Object.SetView function:

Code: ags
oDog.SetView(oDog.View, l); // set oDog.Loop to loop 'L' of oDog's current view

SMF spam blocked by CleanTalk