本文介绍了如何在while循环中无限循环数? (Unity3D- unityscript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function OnMouseDown () {
    rotationNumber +=1;
}

function Update () {
    while (rotationNumber == 1) {
        gameObject.GetComponent(SpriteRenderer).sprite = leftArrow;
        return;
    }
    while (rotationNumber == 2) {
        gameObject.GetComponent(SpriteRenderer).sprite = upArrow;
        return;
    }
    while (rotationNumber == 3) {
        gameObject.GetComponent(SpriteRenderer).sprite = rightArrow;
        return;
    }
    while (rotationNumber == 4) {
        gameObject.GetComponent(SpriteRenderer).sprite = upArrow;
        rotationNumber = 1;
        return;
    }
}

我想循环播放,但是当我第四次单击它时,它直接进入第一张图像.我尝试让出WaitForSeconds,但是没有用.

I want to loop this but when I click it the fourth time it just goes straight to the first image. I tried yield WaitForSeconds but it didn't work.

推荐答案

问题是您在while循环中将rotationNumber设置为1.这样做:

The problem is that you are setting rotationNumber to 1 inside the while loop. Do it in this way:

function OnMouseDown () {
    rotationNumber += 1;
    if ( rotationNumber > 4 ) rotationNumber = 1;
}

function Update () {
    while (rotationNumber == 1) {
        gameObject.GetComponent(SpriteRenderer).sprite = leftArrow;
        return;
    }
    while (rotationNumber == 2) {
        gameObject.GetComponent(SpriteRenderer).sprite = upArrow;
        return;
    }
    while (rotationNumber == 3) {
        gameObject.GetComponent(SpriteRenderer).sprite = rightArrow;
        return;
    }
    while (rotationNumber == 4) {
        gameObject.GetComponent(SpriteRenderer).sprite = upArrow;
        return;
    }
}

这篇关于如何在while循环中无限循环数? (Unity3D- unityscript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 05:07