第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使用 do while 循環(huán)凍結(jié)統(tǒng)一

使用 do while 循環(huán)凍結(jié)統(tǒng)一

C#
慕運(yùn)維8079593 2021-07-13 17:09:44
所以我想讓我的木板從A點(diǎn)移動(dòng)到B點(diǎn),然后在它到達(dá) B 點(diǎn)時(shí)停止。我實(shí)現(xiàn)了do while loop,for-loop但不幸的是,每次我點(diǎn)擊播放場(chǎng)景按鈕時(shí),Unity 都會(huì)凍結(jié),知道為什么會(huì)發(fā)生這種情況嗎?public class movingplank : MonoBehaviour {    public Rigidbody2D Rigidbody2d;            float x;       Vector2 ve = new Vector2();    // Use this for initialization    void Start ()     {        Rigidbody2d = GetComponent<Rigidbody2D>();    }    // Update is called once per frame    void Update ()     {          do         {   ve = Rigidbody2d.transform.position;             x = ve.x;      // storing x component of my plank into float x            Rigidbody2d.velocity = new Vector2(1f, 0f);        } while (x <-4);   // move plank till it reaches point B    } }
查看完整描述

1 回答

?
倚天杖

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊

你的 do while 循環(huán)Rigidbody2d.velocity = new Vector2(1f, 0f);每次都會(huì)執(zhí)行。該循環(huán)中沒有任何變化。如果你這樣做:


while (x < y)

{

    a = 5;

    x++;

}

這樣做沒有任何意義。只是a = 5會(huì)產(chǎn)生相同的效果,只是少了很多不必要的循環(huán)。


最重要的是,您根本沒有改變 的價(jià)值x。這就是導(dǎo)致問題的原因。你基本上在做


while (x < y)

    a = 5;

如果在開始x時(shí)小于y,x將始終小于y,因此它將永遠(yuǎn)執(zhí)行while循環(huán)體,因此 Unity 卡在該Update方法中。


這與每幀調(diào)用一次的事實(shí)無關(guān)Update。這只是一個(gè)簡單的無限循環(huán),由使用不變的條件引起。即使程序在不同的函數(shù)中,這也會(huì)阻塞程序。


您可以改為執(zhí)行以下操作:


// Using a property will always return the targets X value when called without having to 

// set it to a variable

private float X 

    // Called when getting value of X

    get { return Rigidbody2d.transform.position.X; } }  


    // Called when setting the value of X

    set { transform.position = new Vector2(value, transform.position.y); }  

}

private bool isMoving = false;


private void Update () 

{  


    if (X < -4 && !isMoving)

    { 

        Rigidbody2d.velocity = new Vector2(1f, 0f); // Move plank till it reaches point B

        isMoving = true;

    }

    else if (isMoving) 

    { 

        Rigidbody2d.velocity = new Vector(0f, 0f);  // You probably want to reset the 

                                                    // velocity back to 0

        isMoving = false;

    }                                               


查看完整回答
反對(duì) 回復(fù) 2021-07-18
  • 1 回答
  • 0 關(guān)注
  • 185 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)