x


Use yield WaitForSeconds to delay mouseover on a gameObject? (JS)

I have a main menu which has a 3 second transition in, which looks cool and all, but I ran into a problem - I can mouseover the objects during their transition in, which I don't want to be able to do. So I used a yield WaitForSeconds(3) before the function OnMouseOver, thinking that would solve it:

    iTween.MoveFrom(gameObject, {"y":5, "time":3, "EaseType":"EaseOutSine"});
iTween.RotateFrom(gameObject, {"y":-5, "time":5, "EaseType":"easeInOutQuad", "loopType":"pingPong"});
//wait for transition to finish to avoid glitches with mouseover
yield WaitForSeconds (3);
function OnMouseOver ()
{
    iTween.MoveTo(gameObject, {"z":-.25,"time":1});
    if(Input.GetMouseButtonDown(0))
    {
       Application.LoadLevel("Credits");
    }
}
function OnMouseExit ()
{
    iTween.MoveTo(gameObject, {"z":0, "time":1});
}

But it didn't appear to create any sort of delay, and the problem still existed. Is there another way to force this delay before a function without putting a delay within the function itself?

more ▼

asked Jul 10 '11 at 04:54 PM

playbass06 gravatar image

playbass06
41 6 7 11

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Forget the yield. Check how long the game has been running instead.

var delayTime : float = 3.0;
function Start()
{
    delayTime += Time.time;
}
function OnMouseOver()
{
    if( Time.time < delayTime )
        return; //exit the function if it's 3 seconds since this script started
}
more ▼

answered Jul 10 '11 at 05:05 PM

Joshua gravatar image

Joshua
6.4k 19 25 70

It also appears you yield outside of any function? That does.. nothing. The reason the iTeen functions work is because you're starting coroutines.

Jul 10 '11 at 05:06 PM Joshua

Thanks - that solved it. And yes, I have no idea what I'm doing sometimes. I kind of just assumed if one thing worked outside of a function, anything would.

Jul 10 '11 at 05:13 PM playbass06

We've all been there at some point. ;)

Functions are executed when they are called. The update function is called once every frame. The OnMouseOver function is called every frame when the mouse is over the collider. Variables declared outside of functions are global variables, and can then be used inside those functions. If you call a function outside of any function you're calling a coroutine at start. This is actually not good practice, it's better to put them in the Start function, which will then call them at start.

Jul 10 '11 at 05:27 PM Joshua
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3444
x477
x333
x170
x37

asked: Jul 10 '11 at 04:54 PM

Seen: 1054 times

Last Updated: Jul 10 '11 at 05:27 PM