我将如何在javascript中编写下面的生成代码? [关闭]

问题描述 投票:-4回答:1

下面的C#代码以不同的概率生成游戏对象,但我用JavaScript编写了大部分游戏逻辑。下面的代码在Javascript中是什么?帮助将不胜感激。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnerScript05 : MonoBehaviour {
public Spawn04[] spheres;

void Start () {
}

void Update () {
    if (Input.GetKeyDown (KeyCode.Space)) SpawnObjects ();
}

void SpawnObjects() {
    int i = Random.Range (0, 100);
    for (int j = 0; j < spheres.Length; j++) {
        if (i >= spheres [j].minProbabilityRange && i <= spheres [j].maxProbabilityRange) {
            Instantiate (spheres [j].spawnObject, transform.position, transform.rotation);
            break;
        }
    }
}
}

[System.Serializable]
public class Spawn04 {

public GameObject spawnObject;
public int minProbabilityRange = 0;
public int maxProbabilityRange = 0;
}
c# unity3d unityscript spawn spawning
1个回答
0
投票

应该是这样的:

import UnityEngine;
import System.Collections;
import System.Collections.Generic;

class SpawnerScript05 extends MonoBehaviour {

    var spheres : Spawn04[];

    function Start() {
    }

    function Update() {
        if (Input.GetKeyDown(KeyCode.Space)) SpawnObjects();
    }

    void SpawnObjects() {
        var i: int;
        i = Random.Range(0, 100);
        for (var j = 0; j < spheres.Length; j++) {
            if (i >= spheres[j].minProbabilityRange && i <= spheres[j].maxProbabilityRange) {
                Instantiate(spheres[j].spawnObject, transform.position, transform.rotation);
                break;
            }
        }
    }

    class Spawn04 {
        var spawnObject : GameObject;
        var minProbabilityRange : int = 0;
        var maxProbabilityRange : int = 0;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.