Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: m0rph84 on Fri 24/02/2017 03:09:11

Title: Tween module issue and newbie doubt
Post by: m0rph84 on Fri 24/02/2017 03:09:11

I can't figure out how to accomplish the below explained animation using Tween module:

I have an object and I want it to move from position x,y to position x1,y1.
At this point I want to flip the image linked to the object and move back it to position x,y, flip again and keep repeating
it until the room is switched.

What I've done so far is in function room_AfterFadeIn():


oSharkfin.TweenPosition(10.0,470, 347, eDirectionLeft, eOnce); //move
oSharkfin.TweenImage(otweenobj, 1.5, 82); //flip
//oSharkfin.TweenPosition(10.0,840, 347, eDirectionLeft, eOnce); //move back


The problem is that I have no clue on how to wait for the first TweenPosition to reach coordinate(470,347)
before execute oSharkfin.TweenImage(otweenobj, 1.5, 82)

I tried something like:


if(oSharkfin.X == 470 && oSharkfin.Y == 347)
{
     oSharkfin.TweenImage(otweenobj, 1.5, 82); //flip
}


But seem not to work. There is a simpler way I'm not aware of? (I'm sure there is)

And another question:

How the function room_RepExec() works?
I tried before using Tween module to move the object inside this function with object.Move() function
and it worked but not in the way I expected. The object moved only when I performed an action with the main
character like talk or interact with an hotspot or object...

Thanks in advance.




Title: Re: Tween module issue and newbie doubt
Post by: Khris on Fri 24/02/2017 11:57:18
room_RepExec() is called 40 times per second (each game frame), so you need to carefully manage what you put in there.

The general idea for doing what you want to do is

a) start movement non-blocking, say in room_Load
b) in RepExec, check for some condition that will only be true *once*, and if the condition is reached, run some command that will immediately change the game state, so the event doesn't fire again

A common mistake is to put commands for animation or movement directly inside RepExec; this will not work in most cases because that commands is run, again, 40 times per second.

You should also avoid blocking commands in RepExec, because while allowed, they will cause the game to become unresponsive.

Try this:
function room_RepExec() {

  if (oSharkfin.X == 840) {
    oSharkfin.Graphic = 82;
    oSharkfin.TweenPosition(10.0, 470, 347, eDirectionLeft, eOnce);
  }
  else if (oSharkfin.X == 470) {
    oSharkfin.Graphic = 81;
    oSharkfin.TweenPosition(10.0, 870, 347, eDirectionRight, eOnce);
  }

}
Title: Re: Tween module issue and newbie doubt
Post by: m0rph84 on Fri 24/02/2017 16:21:55
Thank you for your explanation.

I tried the code you propose, but is not working, the sharkfin arrive to x1,y1, flip side and
after a little while I got an error from Tween module saying:

"Cannot create a new tween because Tween module is currently playing 64 tweens which is the maxmimu"

I think this is due to the number of time RepExec() is called.
I really can't figure out how to implement control over Tween moving function for object I want move around
for all the time the player is in the current room...

Maybe I need to play with setTimer() and IsTimerExpired() ?
Title: Re: Tween module issue and newbie doubt
Post by: m0rph84 on Fri 24/02/2017 17:07:07
I tried this:

Code (ags) Select


function room_AfterFadeIn()
{
    oSharkfin.TweenPosition(10.0,470, 347, eDirectionLeft, eOnce);
}

function room_RepExec()
{
  if (oSharkfin.X == 830) {
    oSharkfin.StopTweenPosition();
    oSharkfin.Graphic = 6;
    oSharkfin.TweenPosition(10.0, 470, 347, eDirectionRight, eOnce);
  }
  else if (oSharkfin.X == 470) {
    oSharkfin.StopTweenPosition();
    oSharkfin.Graphic = 82;
    oSharkfin.TweenPosition(10.0, 870, 347, eDirectionLeft, eOnce);
  }

}


It works but I still have the same problem, the oSharkfin.TweenPosition() inside the 2 if are triggered(move) only if
I interact with the PC on a object or if he do another action like talk...
Title: Re: Tween module issue and newbie doubt
Post by: Slasher on Fri 24/02/2017 17:19:02
Version 1.22 Tween.

You can raise the maximum of Tweens.

Tween Header

Code (ags) Select
#define MAX_TWEENS 10 // raise the number to say 2000

Example that works
Code (ags) Select

function room_Load()
{
oA1.TweenPosition(2.5, 570, 140, eEaseOutTween, eNoBlockTween);
}


function room_RepExec()
{
if (oA1.X == 570) {
oA1.TweenPosition(2.5, 70, 140, eEaseOutTween, eNoBlockTween);
}
else if (oA1.X == 70) {
oA1.TweenPosition(2.5, 570, 140, eEaseOutTween, eNoBlockTween);
}
}



hope this helps
Title: Re: Tween module issue and newbie doubt
Post by: m0rph84 on Fri 24/02/2017 18:56:49
Thanks, now I solved the tween number error.
I have the Tween module 2.1.0

I'm almost there but something is still wrong....I tried what you suggest:

Code (ags) Select

function room_Load()
{
    ..
    ....
    oSharkfin.TweenPosition(10.0,470, 347, eEaseLinearTween,  eNoBlock);
}


and

Code (ags) Select

function room_RepExec()
{
  //right to left
  if (oSharkfin.X == 840) {
    oSharkfin.Graphic = 6;
    oSharkfin.TweenPosition(10.0, 470, 347, eEaseLinearTween, eNoBlock);
  }
  //left to right
  else if (oSharkfin.X == 470) {
    oSharkfin.Graphic = 82;
    oSharkfin.TweenPosition(10.0, 870, 347, eEaseLinearTween, eNoBlock);
  }
}


The problem is in the else if;
The code execute for the right to left case but not vice versa .
The strange thing is that it execute oSharkfin.Graphic = 82 and flip the image but then until I interact with something
the oSharkfin.TweenPosition function isn't processed.

I really have no clue...

Title: Re: Tween module issue and newbie doubt
Post by: Slasher on Fri 24/02/2017 19:15:40
Code (ags) Select
function room_RepExec()
{
  //right to left
  if (oSharkfin.X == 840) {         // states 840
    oSharkfin.Graphic = 6;
    oSharkfin.TweenPosition(10.0, 470, 347, eEaseLinearTween, eNoBlock);
  }
  //left to right
  else if (oSharkfin.X == 470) {
    oSharkfin.Graphic = 82;
    oSharkfin.TweenPosition(10.0, 870, 347, eEaseLinearTween, eNoBlock); // goes to 870 but won't go back because it supposed to be 840..
  }
}


Check you X positions on code.
Title: Re: Tween module issue and newbie doubt
Post by: m0rph84 on Fri 24/02/2017 20:10:30
I make it works but I have no idea why is working...make no sense to me :grin:
This below is the working version:

Code (ags) Select

function room_Load()
{
  ...
  ......
  oSharkfin.TweenPosition(10.0,470, 347, eEaseLinearTween,  eNoBlock); //Right(X=870) to left(X=470) first time.
}

function room_RepExec()
{
   if (oSharkfin.X == 869)
  {
    oSharkfin.Graphic = 6;
    oSharkfin.TweenPosition(10.0, 470, 347, eEaseLinearTween, eNoBlock); //Right(X=870) to left(X=470)
  }
  else if (oSharkfin.X == 471)
  {
    //oSharkfin.StopTweenPosition();
    oSharkfin.Graphic = 82;
    oSharkfin.TweenPosition(10.0, 870, 347, eEaseLinearTween, eNoBlock); //Left(X=470) to Right(X=870)
  }
}


For some obscure reason(to me) if I set 869 (instead of 870) and 471 instead of (470)
all work. If I set the exact value of X in one or both places it stops works right away(after the first move right to left) or
after a couple of loop(depending which X I modified).

I don't know what to say...???
Title: Re: Tween module issue and newbie doubt
Post by: m0rph84 on Fri 24/02/2017 20:38:02

And I just realize something more.
I have uncommented a portion of code in room_afterFadeIn()(that I commented to test sharkfin movement more rapidly) that contains
some wait() and now again the shark fin stop once reached right-left the first time....
Title: Re: Tween module issue and newbie doubt
Post by: m0rph84 on Fri 24/02/2017 23:53:31
I make other tests.
It seems that every action that could trigger a wait() or a blocking action
slightly before sharkfin reaches one of the 2 edges (before the flip), makes sharkfin object
to stop moving forever even if the function is inside room_RepExec()

I also put a check to see if sharkfin obj reaches is final destination after a wait and it seem so:

Code (ags) Select

function room_AfterFadeIn()
{
  oSharkfin.TweenPosition(10.0,470, 347, eEaseLinearTween,  eNoBlock);//sharkfin start moving to the left
  cRicca.Walk(356, 584, eBlock); //My character walks in
  Wait(300); //I put a wait that is still active when sharfin reaches X=470
  Display("X = %d",oSharkfin.X);
}


and it display 470, so it should trigger the if statement in the RepExec() function, but this doesn't happen.

Code (ags) Select

function room_RepExec()
{
   if (oSharkfin.X == 870)
  {
    oSharkfin.Graphic = 6;
    oSharkfin.TweenPosition(10.0, 470, 347, eEaseLinearTween, eNoBlock); //Right(X=870) to left(X=470)
  }
  else if (oSharkfin.X == 470)
  {
    //oSharkfin.StopTweenPosition();
    oSharkfin.Graphic = 82;
    oSharkfin.TweenPosition(10.0, 870, 347, eEaseLinearTween, eNoBlock); //Left(X=470) to Right(X=870)
  }
}

Title: Re: Tween module issue and newbie doubt
Post by: Cassiebsg on Fri 24/02/2017 23:55:44
Here, read CW's reply: http://www.adventuregamestudio.co.uk/forums/index.php?topic=54494.0