0

I'm developing a simple game in Unity2D, in which several monsters eat things that are dragged to them. If the right object is dragged to the monster, the score goes up by one and the monster should make a happy face, otherwise, score goes down and makes a sad face. This is the code I'm using for that (minus the transitions to happy/sad):

 if (transform.name.Equals ("yellow")){

        if (collinfo.name.Equals ("plastic(Clone)")) {
            Debug.Log ("+1");
            audio.Play ();
            GameSetup.playerScore += 1;
            gs.GetComponent<GameSetup>().removeit(aux);             
        } 
        else {
            Debug.Log ("-1");
            audio.Play ();
            if (GameSetup.playerScore == 0)
            {}
            else
            {
            GameSetup.playerScore -= 1;
            }
            gs.GetComponent<GameSetup>().removeit(aux);
        }

The audio played is just a 'munching' sound.

I want the monster to change sprites to happyFace (via GameObject.GetComponent ().sprite = happyFace), wait one second and then switch back to it's normal sprite, but don't know how to implement that waiting period.

Any and all help would be much appreciated.

4

2 回答 2

1

This can be implemented several ways, however, I would use a method that returns an IEnumerator like so…</p>

This assumes you have a variable in your script that has a reference to the SpriteRenderer that is attached to the GameObject with this script.

SpriteRenderer sr = GetComponent <SpriteRenderer> ();

I also assume you have an original sprite and the possible sprites to change to as variables too.

public Sprite originalSprite;
public Sprite happyFaceSprite;
public Sprite sadFaceSprite;

public IEnumerator ChangeFace (Sprite changeToSprite)
{
    sr.sprite = changeToSprite;
    yield return new WaitForSeconds (1.0f);
    sr.sprite = originalFaceSprite;
}

You would then call this function with the applicable sprite as the variable.

if (happy)
    StartCoroutine (ChangeFace (happyFaceSprite);
else
    StartCoroutine (ChangeFace (sadFaceSprite);

Because the ChangeFace method returns an IEnumerator, we must call that function with the StartCoroutine function. The method will run until it reaches the yield return new WaitForSeconds (1.0f) function, then wait for 1.0f seconds and then resume where it stopped last time.

Understand?

Note I haven't tested this but I don't see why it wouldn't work.

于 2014-11-17T09:03:42.257 回答
-1

Put a floating point variable in your monster controller, call it happyTimer or something. It should start at zero.

Then in your Update function you should check happyTimer is above zero. If it is, then subtract Time.deltaTime and check again. If happyTimer is zero or below for the second check, then call your function that resets the sprite.

When you set the sprite to "happy face", also set happyTimer = 1. This will begin the countdown starting with the next Update call.

The relevant part of Update would look like this:

if(happyTimer > 0) {
    happyTimer -= Time.deltaTime;
    if(happyTimer <= 0) {
        resetSprite();
    }
}
于 2014-11-17T20:17:45.880 回答