快速上手:制作第一个 2D 游戏 生成地图无效

那位大佬帮找找看是年龄出问题了 :joy:
版本:3.8.1
image
image
image
image

代码:
import {_decorator, CCInteger, Component, Node, Prefab, instantiate} from ‘cc’;
import {BLOCK_SIZE, PlayController} from “db://assets/scripts/PlayController”;
const {ccclass, property} = _decorator;

enum BlockType {
BT_NONE,
BT_STONE,
};

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

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

@property({type: CCInteger})
public roadLength: number = 50;

private _road: BlockType[] = [];

start() {
    this.generateRoad()
}

update(deltaTime: number) {

}

generateRoad() {
    console.log(this.node, 'node')

    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) {
            block.setPosition(j * BLOCK_SIZE, 0, 0);
            this.node.addChild(block);
        }
    }
}

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;
}

}

换了个版本跟着操作就行了

年龄到 35 就写不好代码了,,