快速上手:制作第一个2D游戏生成地图卡住了

跟着教程来,通过GameManager生产随机方块地图这步卡住了,

求助各位大神

import { _decorator, CCInteger, Component, instantiate, Node, Prefab } from ‘cc’;
import { BLOCK_SIZE, PlayerController } from ‘./PlayerController’;
const { ccclass, property } = _decorator;

enum BlockType {
BT_NONE,
BT_STONE,
};

@ccclass(‘NewComponent’)
export class NewComponent extends Component {

@property({ type: Prefab })
public boxPrefab: Prefab | null = null;

@property({ type: CCInteger })
public roadLength: number = 50;
private _road: BlockType[] = [];

init() {
    this.generateRoad();
}

generateRoad() {

    this.node.removeAllChildren();

    this._road = [];
    // startPos
    this._road.push(BlockType.BT_STONE);

    for (let i = 1; i < this.roadLength; i++) {
        if (this._road[i - 1] === BlockType.BT_NONE) {
            this._road.push(BlockType.BT_STONE);
        } else {
            this._road.push(Math.floor(Math.random() * 2));
        }
    }

    for (let j = 0; j < this._road.length; j++) {
        let block: Node | null = this.spawnBlockByType(this._road[j]);
        if (block) {
            this.node.addChild(block);
            block.setPosition(j * BLOCK_SIZE, 0, 0);
        }
    }
}

spawnBlockByType(type: BlockType) {
    if (!this.boxPrefab) {
        return null;
    }

    let block: Node |null = null;
    switch (type) {
        case BlockType.BT_STONE:
            block = instantiate(this.boxPrefab);
            break;
    }

    return block;
}

start() {
    this.generateRoad()
}

update(deltaTime: number) {
    
}

}

没有看出啥问题啊

你这个初始化的时候 roadLength 等于 1 ,循环都进不去。

在上面有添加 public roadLength: number = 50; 欸

图片
图片
文件名与内容中的类名不一致

需要一个标签 在编辑模式运行