Hi there,
Been playing with animation. I want to have a background character:
1. choose a random number
2. choose animation based on number
3. play the animation, NO blocking; so it plays in the background
4. while it's animating, don't allow the routine to choose another animation.
Code for this is:
function room_RepExec()
{
if (cPig.Animating==false) {
PigRandAnim=Random(2);
if (PigRandAnim==1) { //idle
cPig.LockView(53);
cPig.Animate(1, 1, eOnce, eNoBlock);
cPig.UnlockView();
Display("finished normal view");
} else if (PigRandAnim==2) { //drinking
cPig.LockView(5);
cPig.Animate(1, 1, eOnce, eNoBlock);
cPig.UnlockView();
Display("finished drinking view");
}
}
}
But this doesn't work - I just get constant reminders that "finished <something> view", the animation doesn't actually occur.
If I do eBlocking, everything works fine; but I can't do anything since this isn't happening in the background, and stops everything else.
IdleView and NormalView are set. I've tried with MovementLinkedToAnimation as true and false, no dice.
I feel like this has something to do with NormalView showing only frame 0 if not moving (character never moves); but I can't get my head around it. Maybe you can help?
(drinking==normal)
What happens if you take the cPig.UnlockView(); line from both the if statements and move it to the beginning of each if statement instead of the end? (Just speculation)
You are using eNoBlock command. This means that after command is run it does not wait until animation finished, and continue executing next commands. This is why it locks view and unlocks view almost immediately.
Usual solution is to remember that animation was started, and then keep checking Animating property to know when its finished, and then unlock the view.
You are already checking for Animating, so there is just one small change that must be done:
EDIT: basically, almost what Morgan LeFlay said :).
function room_RepExec()
{
if (cPig.Animating==false) {
// cPig.UnlockView(); // maybe not even needed
PigRandAnim=Random(2);
if (PigRandAnim==1) { //idle
cPig.LockView(53);
cPig.Animate(1, 1, eOnce, eNoBlock);
} else if (PigRandAnim==2) { //drinking
cPig.LockView(5);
cPig.Animate(1, 1, eOnce, eNoBlock);
}
}
}
In fact, I am not even sure if UnlockView ought to be called in such case, since Pig keeps animating forever anyway. Maybe you can just throw this command out?
Depends on whether AGS requires you to unlock the view before locking to another.