隨時(shí)間移動(dòng)游戲?qū)ο笪艺趶腟wift SpriteKit背景中學(xué)習(xí)UnityofSpriteKit,其中移動(dòng)一個(gè)精靈的x位置就像運(yùn)行一個(gè)動(dòng)作一樣直接,如下所示:let moveLeft = SKAction.moveToX(self.frame.width/5, duration: 1.0)let delayAction = SKAction.waitForDuration(1.0)let handSequence = SKAction.
sequence([delayAction, moveLeft])sprite.runAction(handSequence)我想知道一種等效或類似的方法,可以在特定的時(shí)間內(nèi)(例如,第二次)將sprite移動(dòng)到一個(gè)特定的位置,并且在UPDATE函數(shù)中不需要調(diào)用這個(gè)延遲。
3 回答

幕布斯7119047
TA貢獻(xiàn)1794條經(jīng)驗(yàn) 獲得超8個(gè)贊
WaitForSeconds()
Lerp
, Coroutine
Time.deltaTime
public GameObject objectectA;public GameObject objectectB;void Start(){ StartCoroutine(moveToX(objectectA.transform, objectectB.transform.position, 1.0f));}bool isMoving = false; IEnumerator moveToX(Transform fromPosition, Vector3 toPosition, float duration){ //Make sure there is only one instance of this function running if (isMoving) { yield break; ///exit if this is still running } isMoving = true; float counter = 0; //Get the current position of the object to be moved Vector3 startPos = fromPosition.position; while (counter < duration) { counter += Time.deltaTime; fromPosition.position = Vector3.Lerp(startPos, toPosition, counter / duration); yield return null; } isMoving = false;}

慕萊塢森
TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊
InvokeRepeating
float duration; //duration of movementfloat durationTime; //this will be the value used to check if Time. time passed the current duration setvoid Start(){ StartMovement();}void StartMovement(){ InvokeRepeating("MovementFunction", Time.deltaTime, Time.deltaTime); //Time.deltaTime is the time passed between two frames durationTime = Time.time + duration; //This is how long the invoke will repeat}void MovementFunction(){ if(durationTime > Time.time) { //Movement } else { CancelInvoke("MovementFunction"); //Stop the invoking of this function return; }}
- 3 回答
- 0 關(guān)注
- 441 瀏覽