Making object to animate while mouse cursor is on it.

Started by Thiscold, Sun 17/01/2010 22:29:37

Previous topic - Next topic

Thiscold

I've been trying to make a game title screen. In the title screen i want one of the objects (the one that says new game)
to animate once players mouse cursor is on it and stop when player removes cursor from the object.
I also wanted it to be possible for the player to interact with the object by left clicking it but in my understanding
eBlock prevents actions during its execute?

My question is this; why my animation wont commence if I DONT have that eblock on ( instead using eNoBlock).
So if I change that script to eNoBlock, animation wont start.

function room_Load()
{
   onewmotley.SetView(7, 0, 0);
}

function room_RepExec()
 {
    if (Object.GetAtScreenXY(mouse.x,mouse.y) == onewmotley){
    onewmotley.Animate(0, 5, eOnce, eBlock);}
    else {  if (onewmotley.Animating) {
    onewmotley.StopAnimating();}}
      
}


Ryan Timothy B

Code: ags

function room_RepExec()
 {
    if (Object.GetAtScreenXY(mouse.x,mouse.y)==onewmotley && onewmotley.Animating==false) {
      onewmotley.Animate(0, 5, eRepeat);
    }
    else if (Object.GetAtScreenXY(mouse.x,mouse.y)!=onewmotley && onewmotley.Animating==true) onewmotley.StopAnimating();
}


It's been awhile since I've animated any objects (I'm usually working with drawing functions :P), but this should work.

monkey0506

#2
I imagine the reason it's not working with eNoBlock is because you're constantly telling it to start animating from the beginning of the loop. Try doing something like this:

Code: ags
function room_RepExec()
{ // I fixed the alignment of your braces (and your indentation) to be consistent
  if (Object.GetAtScreenXY(mouse.x,mouse.y) == onewmotley)
  {
    if (!onewmotley.Animating)
    { // if the object is NOT already animating
      onewmotley.Animate(0, 5, eOnce, eNoBlock);
    } // otherwise it's already animating and we don't need to call it again
  }
  else
  {
    if (onewmotley.Animating)
    {
      onewmotley.StopAnimating();
    }
  }
}


Edit: Ryan Timothy beat me to the post, but he's pretty much right. A couple of points about the code you're using though Ryan:

Saying "if (condition)...else if (not condition)" is redundant and requires evaluating the condition twice. This should be avoided where possible.

Also using Object.GetAtScreenXY as part of a condition is valid, but if you're checking it more than once it's faster to do:

Code: ags
Object *oat = Object.GetAtScreenXY(mouse.x, mouse.y);
if (oat == onewmotley) {
}
else if (oat == ootherobject) {
}


Seeing as then it doesn't have to keep comparing the co-ordinates against the screen each time and only has to do it once.

But you did correctly identify the problem. :D Just wanted to point out a few places where your code could be optimized.

Thiscold

Alright this works great, thanks to both of you for clearifying this one up :). I will name my children after you two.

monkey0506

Hah. "Monkey Oh-five-oh-six Thiscold you get in here right now! What have I told you about..." ;D

Glad to hear we got it sorted out for you.

And get used to thinking like this...program logic can seem daunting at first but once you get the hang of it... ::)

MrCheminee

I had the same problem and solved it by playing the animation under the Afterfadein function and making it visible under the Repexec function...

SMF spam blocked by CleanTalk