Using animation as a timer

Started by Furwerkstudio, Sat 04/01/2020 19:01:59

Previous topic - Next topic

Furwerkstudio

Hey, I was trying to figure out what the code would look like to use an object that is animated as a "timer". That when it reaches a certain frame it triggers a situation, like say when the animation is done it triggers goes to another room unless a different object is clicked.

I did some mock coding, and wonding if this would work or is there any better way to code this.
Code: ags

ObjectX.Animate(1, 1, eOnce, eNoblockk, eForward);

if (OtherObject.AnyClick) {
       player.gotoroom(x,y);
      }
else {
     game.GetViewFrame(1, 80);
     player.gotoroom(y,x);


Crimson Wizard

#1
Generally, you wait in a loop and check for Object.Loop, Object.Frame, and perhaps Object.Animating.

How to do this depends on whether you allow some other actions while the object is animated. From your description it seems to be the second situation. But I will post example solutions for both, just in case.

Variant 1. Animate and blocking wait:
Code: ags

ObjectX.Animate(1, 1, eOnce, eNoBlock);
while (ObjectX.Animating && ObjectX.Frame < 80) {
     Wait(1); // to keep game redrawn
}
player.ChangeRoom(...);


Variant 2. Animate and non-blocking wait.

In this case you need to start animation and let it go, but keep checking for an object in room's rep-exec function.

Code: ags

function Room_RepExec() { // assuming you connected this function to corresponding room's event
    if (ObjectX.Animating && ObjectX.Frame == 80) {
         // object was animating and reached certain frame
         player.ChangeRoom(...);
    }
}


But since you also allow interruption, then you need to handle other object clicking somehow, for instance make a function for "Any click on object" event, and somehow cancel the timer there (stop or reset animation, or set some variable to tell that it should be ignored).

SMF spam blocked by CleanTalk