Cocos Creator的Hello World

0. Documentation

Cocos official website documentation

1. Download

Visit the cocos official website to download and Cocos Dashboard
install. The option 安装 Visual Studio 2017can be unchecked
. Note: Cocos DashboardThe shortcut file name CocosDashboard.exeis not mistaken for the installation file.

2. Registration and login

Register/login cocos website

3. Cocos Dashboard setting language

Cocos Dashboard, click the gear icon in the upper right corner insert image description hereto open the setting interface
Language(Choose a display anguage.), and modify it to 简体中文
click the button in the lower right corner 返回to exit the setting interface.

4. Install the editor

Cocos Dashboard(Cocos Dashboard), version management software. Similar to nvm of node.js and conda of python,
it can manage Cocos Creatorgame projects, no matter what engine version the project is based on


Cocos Dashboard, click the gear icon in the upper right corner
to modify the editor directory at your own discretion, this setting will modify the storage location of the editor

Click on the left sidebar, 编辑器click 下载编辑器the button in the lower right corner
to select a version and click the download icon on the right insert image description here, this article chooses to download CocosCreator 3.7.2
the service agreement interface, click 同意the button
to wait for the download
system prompt Cocos Creator 3.7.2 下载成功 正在解压中
(you can open the task manager to check the CPU usage and disk usage if you are bored , it’s useless anyway)
the system prompts Cocos Creator 3.7.2 安装成功 欢迎使用Cocos Creator 3.7.2
that the editor is installed successfully

5. New project

Click on the left sidebar, 项目and click 新建the button in the lower right corner
. The editor version defaults to the newly installed 3.7.2
template selection Empty(2D)
. Enter the project name HiCocos(the project name cannot have special characters, spaces, and cannot end with .)
The position is default or modified by yourself. Click the button
in the lower right corner to wait for loading创建

6. Cocos Creator set language

Menu bar - File - Preferences, open Preferencesthe window
General category, change the Language to 中文
Close Preferencesthe window

7. Configure the code editor

Install the plug-in Prettier to format the ts script
Format shortcut key Shift+Alt+F, when formatting the ts script for the first time, configure the default formatter Select Prettier
the gear icon in the lower left corner of VSCode, set, search Prettier: Tab Width, you can set it to4


If VSCode is installed, Cocos Creator can find VSCode by default. Double-click the script file to open it automatically. Generally, you don’t need to set it (
although it still pretends not to find it, the prompt in the console is as follows (the prompt is still outdated): 为更好地打开脚本,请在 偏好设置 -> 外部程序 -> 默认脚本编辑器 里配置打开脚本的应用程序)


If you double-click the script and prompt that the script editor is not associated, follow the steps below to set the
VSCode download tutorial ,
install and run where.exe codeto get the path C:\Program Files\Microsoft VS Code\bin\code
, then find the VSCode installation folder, C:\Program Files\Microsoft VS Code\Code.exe
Cocos Creator, menu bar, file, preferences, program manager, default script editor , click the magnifying glass icon on the right insert image description here
to set Code.exeas

8. Expand the scene editor window

In general, the larger scene editor window is easier to view, and other windows can be dragged back at any time. Put the mouse
on the window border, and when the mouse pointer turns into a two-way arrow, drag the window to make the scene editor larger
Please add a picture description

9. How to rename a node

Hierarchy manager, select the node to be modified
Click F2, Enter, right click to select rename or modify the name in the first line of the property inspector

10. Add a player

Hierarchy manager in the upper left corner, right click Canvas, create, empty node, rename to玩家

11. Add a player child node sprite

Hierarchy manager in the upper left corner, right click 玩家, create, 2D object, Sprite (elf), rename it to 身体
select 身体, view the property inspector on the right, the penultimate item Sprite Frame, click the mouse icon next to it insert image description hereto modify the sprite frame
and select a button to use as the body, choosedefault_btn_normal

The hierarchy is as shown in the figure, if the hierarchy is wrong, you can refer to the gif to drag and drop
insert image description herePlease add a picture description

12. Add script

Explorer in the lower left corner, right click assets, create, folder, name it 脚本
Right click 脚本, create, script (TypeScript), NewComponent, name itPlayerController

13. Player loading script

In the hierarchy manager in the upper left corner, select 玩家
the property inspector on the right, click 添加组件the button
to expand 自定义脚本, and click PlayerController
to complete the loading

14. Make a prefab

Explorer in the lower left corner, right click assets, create, folder, name it预制体

Hierarchy manager in the upper left corner, right click Canvas, create, 2D object, Sprite (elf), rename it to 浮岛
property inspector on the right side, Sprite Frame, set todefault_btn_disabled

Drag and drop the created one 浮岛into 预制体the folder
Delete the one created in the hierarchy manager浮岛

15. Save the scene

Explorer in the lower left corner, right click assets, create, folder, name it场景

Shortcut key Ctrl+S(or click the menu bar - File - Save Scene (CTRL+S)), the save scene window will pop up,
enter 场景the folder, scene.scenechange the file name 游戏.sceneto save

16. Edit script

Explorer in the lower left corner, assets, scripts, PlayerController, double-click the file to open the VSCode editor to edit the code

16.1 Change template

view default code.\HiCocos\assets\脚本\PlayerController.ts

import {
    
     _decorator, Component, Node } from 'cc';
const {
    
     ccclass, property } = _decorator;

@ccclass('PlayerController')
export class PlayerController extends Component {
    
    
    start() {
    
    

    }

    update(deltaTime: number) {
    
    
        
    }
}

Change it to the template for this tutorial

// import 导入位置1
import {
    
     _decorator, Component, Node } from "cc";
const {
    
     ccclass, property } = _decorator;

// export 常量位置1

@ccclass("PlayerController")
export class PlayerController extends Component {
    
    
    // Animation 动画属性1

    // private 私有属性1
    // private 私有属性2

    // 组件第一次激活时调用的方法
    start() {
    
    }

    // 激活后关闭鼠标监听的方法

    // 重置方法

    // 监听鼠标输入方法

    // 根据步数跳跃方法

    // 监听跳跃结束的方法

    // 根据每次的更新来计算角色最新的位置方法
    update(deltaTime: number) {
    
    }

    // 直到教程结束, 此处的"}"符号一直作为脚本最后一个字符
    // ↙
} 
// ↖
// 它的配对符号必须为"export class PlayerController extends Component {"中的"{"
// VSCode会把这对花括号对"{}"显示为相同的颜色表明它们的配对关系, 点击其中一个, 两个符号会同时显示一个小边框

16.2 Add PlayerController to all imports in this tutorial

Will

    // import 导入位置1

changed to

    // import 导入位置1
    import {
    
     Vec3, EventMouse, input, Input, AnimationComponent } from "cc";

16.3 Add method of monitoring mouse input

Will

    // private 私有属性1

changed to

    // private 私有属性1
    private _startJump: boolean = false;
    private _jumpStep: number = 0;
    private _curJumpTime: number = 0;
    private _jumpTime: number = 0.3;
    private _curJumpSpeed: number = 0;
    private _curPos: Vec3 = new Vec3();
    private _deltaPos: Vec3 = new Vec3(0, 0, 0);
    private _targetPos: Vec3 = new Vec3();

16.4 Calculating character movement

    // 根据步数跳跃方法

changed to

    // 根据步数跳跃方法
    jumpByStep(step: number) {
    
    
        if (this._startJump) {
    
    
            return;
        }

        // 动态计算_jumpTime

        this._startJump = true; // 标记开始跳跃
        this._jumpStep = step; // 跳跃的步数 1 或者 2
        this._curJumpTime = 0; // 重置开始跳跃的时间

        this._curJumpSpeed = this._jumpStep / this._jumpTime; // 根据时间计算出速度

        this.node.getPosition(this._curPos); // 获取角色当前的位置
        Vec3.add(
            this._targetPos,
            this._curPos,
            new Vec3(this._jumpStep, 0, 0)
        ); // 计算出目标位置

        // 播放动画

        // 增加步数
    }

16.5 Determine whether the mouse is left or right clicked

    // 监听鼠标输入方法

changed to

    // 监听鼠标输入方法
    onMouseUp(event: EventMouse) {
    
    
        if (event.getButton() === 0) {
    
    
            this.jumpByStep(1);
        } else if (event.getButton() === 2) {
    
    
            this.jumpByStep(2);
        }
    }

16.6 Using inputmonitor input

    // 组件第一次激活时调用的方法
    start() {
    
    }

changed to

    // 组件第一次激活时调用的方法
    start() {
    
    
        input.on(Input.EventType.MOUSE_UP, this.onMouseUp, this);
    }

16.7 Using inputmonitor input

    // 根据每次的更新来计算角色最新的位置方法
    update(deltaTime: number) {
    
    }

changed to

    // 根据每次的更新来计算角色最新的位置方法
    update(deltaTime: number) {
    
    
        if (this._startJump) {
    
    
            this._curJumpTime += deltaTime; // 累计总的跳跃时间
            if (this._curJumpTime > this._jumpTime) {
    
    
                // 当跳跃时间是否结束
                // end
                this.node.setPosition(this._targetPos); // 强制位置到终点
                this._startJump = false; // 清理跳跃标记
                // 调用监听跳跃结束的方法
                
            } else {
    
    
                // tween
                this.node.getPosition(this._curPos);
                this._deltaPos.x = this._curJumpSpeed * deltaTime; //每一帧根据速度和时间计算位移
                Vec3.add(this._curPos, this._curPos, this._deltaPos); // 应用这个位移
                this.node.setPosition(this._curPos); // 将位移设置给角色
            }
        }
    }

16.8 Try running, the movement is not obvious

Please add a picture description

16.9 Zooming in on Move Actions

Will

    // export 常量位置1

changed to

    // export 常量位置1
    export const BLOCK_SIZE = 40; // 添加一个放大比

again in // 根据步数跳跃方法jumpByStepthe middle

this._curJumpSpeed = this._jumpStep / this._jumpTime; // 根据时间计算出速度

changed to

this._curJumpSpeed = this._jumpStep * BLOCK_SIZE/ this._jumpTime; // 根据时间计算出速度并放大

again in // 根据步数跳跃方法jumpByStepthe middle

        Vec3.add(
            this._targetPos,
            this._curPos,
            new Vec3(this._jumpStep, 0, 0)
        ); // 计算出目标位置

changed to

        Vec3.add(
            this._targetPos,
            this._curPos,
            new Vec3(this._jumpStep * BLOCK_SIZE, 0, 0)
        ); // 计算出目标位置

17. Run

Go back to the Cocos Creator
head up position, click the triangle run buttoninsert image description here
Please add a picture description

18. Animation

In the lower left corner of the resource manager, right click , create, folder , named assetsas动画
动画
oneStep

Select it 身体, view the property inspector on the right, click to 添加组件
select Animation - Animation
will be oneStepdragged 身体to Clipsthe property, and Clipsit will 0change from1

Select 身体,
the shortcut key Ctrl+6(or 控制台switch to 动画编辑器)
animation editor prompt 进入动画编辑模式(Ctrl/Cmd+E)(拖拽动画剪辑资源到面板上)
Drag oneStepto 动画编辑器
( 动画编辑器the upper border where the mouse is placed, the mouse turns into ⬍ up and down arrows, drag and drop to enlarge) the attribute list in the lower left corner
insert image description here
of the animation editor
, click the + next to it Add the plus sign to add a property track, select position
and click New position
(the Y coordinate must be deleted from the default value and manually entered to record the properties of the frame)
set to 0, the property inspector modifies the body Position, the Y coordinate is 0
set to 10, the property inspector modifies the body Position , the Y coordinate is 80
set to 20, the attribute inspector modifies the body Position, and the Y coordinate is0

When the animation editor is in use, press Ctrl+S to save

Animation editor, click the triangle icon button to play, 玩家jump in the scene editor window

In the same way, create another one twoStep
that will twoStepbe dragged 身体onto Clipsthe property, and change Clipsfrom Edit to Frame Set , Y Coordinate to Frame Set to , Y Coordinate to Frame Set to , Y Coordinate to12
twoStep
00
2080
400

When the animation editor is in use, press Ctrl+S to save and
Ctrl+Q to exit the animation editor mode

19. Edit script (play animation code)

Explorer in the lower left corner, assets, scripts, PlayerController, double-click the file to open the VSCode editor to edit the code
will

// Animation 动画属性1

changed to

// Animation 动画属性1
@property(AnimationComponent)
BodyAnim: AnimationComponent = null;

After the creation is completed , save it, go back to the layer manager in the upper left corner of BodyAnim:Animation = null;the Cocos Creator settings , select it and drag it to the upper Ctrl+S of the property inspector on the right玩家Body Anim
玩家
身体Body Anim

Back to VSCode
will jumpByStep in// 播放动画

        // 播放动画

changed to

        // 播放动画
        if (this.BodyAnim) {
    
    
            if (step === 1) {
    
    
                this.BodyAnim.play("oneStep");
            } else if (step === 2) {
    
    
                this.BodyAnim.play("twoStep");
            }
        }

Go back to Cocos Creator to run the game, left click for a small jump, and right click for a big jump (but the fall back is very weird, because _jumpTime has not been modified yet)

Modify _jumpTime
to jumpByStep in// 动态计算_jumpTime

        // 动态计算_jumpTime

changed to

        // 动态计算_jumpTime
        if (step === 1) {
    
    
            const oneStep = "oneStep";
            const state = this.BodyAnim.getState(oneStep);
            this._jumpTime = state.duration;
        } else if (step === 2) {
    
    
            const twoStep = "twoStep";
            const state = this.BodyAnim.getState(twoStep);
            this._jumpTime = state.duration;
        }

Run the game again, left click for a small jump, right click for a big jump, and return to normal

20. Game manager


Right click on the resource manager in the lower left corner 脚本, create, script (TypeScript), NewComponent, name itGameManager

Hierarchy manager in the upper left corner, right click Canvas, create, empty node, rename to 游戏管理层级
selected 游戏管理层级, click on property inspector 添加组件
to expand 自定义脚本, click GameManager, loading is complete

double click GameManagerto edit it

20.1 Change template

view default code.\HiCocos\assets\脚本\GameManager.ts

import {
    
     _decorator, Component, Node } from 'cc';
const {
    
     ccclass, property } = _decorator;

@ccclass('GameManager')
export class GameManager extends Component {
    
    
    start() {
    
    

    }

    update(deltaTime: number) {
    
    
        
    }
}

change it to template

// import 导入位置1
// import 导入位置2
import {
    
     _decorator, Component, Node } from "cc";
const {
    
     ccclass, property } = _decorator;

// enum 枚举1

// enum 枚举2

@ccclass("GameManager")
export class GameManager extends Component {
    
    
    // 预制体

    // 道路长度

    // 地图数据

    // 属性2

    // 组件第一次激活时调用的方法
    start() {
    
    }

    // 初始化的方法

    // 控制游戏状态的读取器

    // 生成地图的方法

    // 根据所需的数量和前1个位置来生成方块

    // 根据 BlockType 生成方块

    // 响应 开始游戏 按钮按下的事件

    // 判断跳坑失败或游戏完成的方法

    // 更新计步器的显示并判断"跳坑失败或游戏完成"

    // 没有用到update,注释掉
    // update(deltaTime: number) {}

    // 直到教程结束, 此处的"}"符号一直作为脚本最后一个字符
    // ↙
} 
// ↖
// 它的配对符号必须为"export class PlayerController extends Component {"中的"{"
// VSCode会把这对花括号对"{}"显示为相同的颜色表明它们的配对关系, 点击其中一个, 两个符号会同时显示一个小边框

20.2 Add GameManager to all imports in this tutorial

Will

// import 导入位置1
// import 导入位置2

changed to

// import 导入位置1
import {
    
     CCInteger, instantiate, Label, Prefab, Vec3 } from "cc";
// import 导入位置2
import {
    
     BLOCK_SIZE, PlayerController } from "./PlayerController";

20.3 Adding a boxPrefab

Will

    // 预制体

changed to

    // 预制体
    @property({
    
     type: Prefab })
    public boxPrefab: Prefab | null = null;

Go back to Cocos Creator
and select 游戏管理层级
it , 浮岛drag the attribute inspector to Box PrefabCtrl
+S to save

20.4 Add enumeration BlockType

Will

// enum 枚举1

changed to

// enum 枚举1
enum BlockType {
    
    
    BT_NONE,
    BT_STONE,
}

20.5 spawnBlockByType

    // 根据 BlockType 生成方块

changed to

    // 根据 BlockType 生成方块
    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;
    }

20.6 spawnBlockByCount

    // 根据所需的数量和前1个位置来生成方块

changed to

    // 根据所需的数量和前1个位置来生成方块
    spawnBlockByCount(lastPos: number, count: number) {
    
    
        let block: Node | null = this.spawnBlockByType(BlockType.BT_STONE);
        if (block) {
    
    
            this.node.addChild(block);
            block?.setScale(count, 1, 1);
            block?.setPosition(
                (lastPos - (count - 1) * 0.5) * BLOCK_SIZE,
                -(BLOCK_SIZE + 1.5),
                0
            );
        }
    }

20.7 roadLength 和 _road

Will

    // 道路长度

    // 地图数据

changed to

    // 道路长度
    public roadLength: number = 50;
    // 地图数据
    private _road: BlockType[] = [];

20.8 Generating a map

    // 生成地图的方法

changed to

    // 生成地图的方法
    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));
            }
        }

        let linkedBlocks = 0;
        for (let j = 0; j < this._road.length; j++) {
    
    
            if (this._road[j]) {
    
    
                ++linkedBlocks;
            }
            if (this._road[j] == 0) {
    
    
                if (linkedBlocks > 0) {
    
    
                    this.spawnBlockByCount(j - 1, linkedBlocks);
                    linkedBlocks = 0;
                }
            }
            if (this._road.length == j + 1) {
    
    
                if (linkedBlocks > 0) {
    
    
                    this.spawnBlockByCount(j, linkedBlocks);
                    linkedBlocks = 0;
                }
            }
        }
    }

20.9 Call generateRoad in start to create a map

    // 组件第一次激活时调用的方法
    start() {
    
    }

changed to

    // 组件第一次激活时调用的方法
    start() {
    
    
        this.generateRoad();
    }

20.10 run

Run the game, the floating island is generated
insert image description here

21. The camera follows the player

Layer manager Camera, Canvasdrag and drop from layer to 玩家layer
Run the game, the camera follows the player

22. Plan the display content of the camera in advance to avoid overlapping display of the same object in subsequent dual-camera cameras

  1. In the hierarchy manager, select Canvas
    the property inspector and Layerchange it to DEFAULTa prompt as shown in the figure.
    insert image description here
    Click 连同修改子节点
    Ctrl+S to save

  2. Explorer, prefab, double-click 浮岛
    the attribute inspector, it will Layerbe changed to DEFAULT
    Ctrl+S to save, Ctrl+Q to exit the edit prefab mode

  3. Hierarchy Manager, Canvas - Player - Camera, check Camera
    the Attributes Inspector, uncheck it Visibility, UI_2Dkeep it VisibilitycheckedDEFAULT

23. Game menu

Hierarchy manager, right-click the top level 游戏
to create, UI component, Canvas (canvas)
and name itUICanvas

Right click UICanvas, create, empty node, name it开始菜单

Right click UICanvas, create, 2D object, Label (text), change the name to 步数
Positionproperty -440and 280
clear Stringproperty


Right-click , create, 2D object, Sprite ( 开始菜单elf), name it as 背景
Content Sizeattribute 400and change 250
Sprite Frameattribute from attribute to attributedefault_spritedefault_panel
TypeSIMPLESLICED

Right-click , create, UI component , 开始菜单Button (button), name it Change the property to and Expand , change the property to开始菜单按钮
开始菜单按钮Position0-80
开始菜单按钮LabelString开始游戏


Right click 开始菜单, create, 2D object, Label (text), name it as 开始菜单标题
Positionproperty change 0and 80
Colorproperty change #000000
Stringproperty change property change 跳跳乐 2D
Font Sizeproperty change40

Right click 开始菜单, create, 2D object, Label (text), name it as 开始菜单提示
Positionproperty change 0and 40
Colorproperty change #000000
Stringproperty change操作:左键小跳,右键大跳

24. Modify the visible layer of the Camera of UICanvas

Hierarchy Manager, under UICancas, select Camera
Property Inspector, uncheck it Visibility, DEFAULTkeep Visibilityit checkedUI_2D

The default layer of UICanvas is UI_2D, no need to modify

25. Game state

25.1 Go back to modify PlayerController

25.1.1 Modify start PlayerController

    // 组件第一次激活时调用的方法
    start() {
    
    
        input.on(Input.EventType.MOUSE_UP, this.onMouseUp, this);
    }

changed to

    // 组件第一次激活时调用的方法
    start() {
    
    
        // input.on(Input.EventType.MOUSE_UP, this.onMouseUp, this);
    }

25.1.2 Modify setInputActive PlayerController

    // 激活后关闭鼠标监听的方法
    // 激活后关闭鼠标监听的方法
    setInputActive(active: boolean) {
    
    
        if (active) {
    
    
            input.on(Input.EventType.MOUSE_UP, this.onMouseUp, this);
        } else {
    
    
            input.off(Input.EventType.MOUSE_UP, this.onMouseUp, this);
        }
    }

25.1.3 Add reset PlayerController

    // 重置方法

changed to

    // 重置方法
    reset() {
    
    }

25.2 Add enumeration GameManager

Will

// enum 枚举2

changed to

// enum 枚举2
enum GameState {
    
    
    GS_INIT,
    GS_PLAYING,
    GS_END,
}

25.3 Add property GameManager

    // 属性2

changed to

    // 属性2
    @property({
    
     type: Node })
    public startMenu: Node | null = null; // 开始的 UI
    @property({
    
     type: PlayerController })
    public playerCtrl: PlayerController | null = null; // 角色控制器
    @property({
    
     type: Label })
    public stepsLabel: Label | null = null; // 计步器

25.4 Add init GameManager

    // 初始化的方法

changed to

    // 初始化的方法
    init() {
    
    
        if (this.startMenu) {
    
    
            this.startMenu.active = true;
        }

        this.generateRoad();

        if (this.playerCtrl) {
    
    
            this.playerCtrl.setInputActive(false);
            this.playerCtrl.node.setPosition(Vec3.ZERO);
            this.playerCtrl.reset();
        }
    }

25.5 Add curState GameManager

    // 控制游戏状态的读取器

changed to

    // 控制游戏状态的读取器
    set curState(value: GameState) {
    
    
        switch (value) {
    
    
            case GameState.GS_INIT:
                this.init();
                break;
            case GameState.GS_PLAYING:
                if (this.startMenu) {
    
    
                    this.startMenu.active = false;
                }

                if (this.stepsLabel) {
    
    
                    this.stepsLabel.string = "0"; // 将步数重置为0
                }

                setTimeout(() => {
    
    
                    //直接设置active会直接开始监听鼠标事件,做了一下延迟处理
                    if (this.playerCtrl) {
    
    
                        this.playerCtrl.setInputActive(true);
                    }
                }, 0.1);
                break;
            case GameState.GS_END:
                break;
        }
    }

25.6 Modify the properties of the game management level

Go back to the Cocos Creator editor and drag the
modified 游戏管理层级properties to Ctrl+S to save
开始菜单Start Menu
玩家Player Ctrl
步数Steps Label

26. Binding button events

    // 响应 开始游戏 按钮按下的事件

changed to

    // 响应 开始游戏 按钮按下的事件
    onStartButtonClicked() {
    
    
        this.curState = GameState.GS_PLAYING;
    }

keep

Go back to the Cocos Creator editor
and modify 开始菜单按钮the property
Click Events(under the cc.Button category), 0set it to 1
Drag 游戏管理层级and drop it to item [0], select GameManagerandonStartButtonClicked

Run the game and run normally.
It is found that the player does not respond when stepping on the pit. Add the game logic of stepping on the pit and fail

27. Modify PlayerController.ts after listening to the end of the jump

1. Add attributes

    // private 私有属性2

changed to

    // private 私有属性2
    private _curMoveIndex: number = 0;



2. Modify reset

    // 重置方法
    reset() {
    
    }

changed to

    // 重置方法
    reset() {
    
    
        this._curMoveIndex = 0;
    }



3. jumpByStepAdding// 增加步数

        // 增加步数

changed to

        // 增加步数
        this._curMoveIndex += step;



4. Listen to the end of the jump

    // 监听跳跃结束的方法

changed to

    // 监听跳跃结束的方法
    onOnceJumpEnd() {
    
    
        this.node.emit('JumpEnd', this._curMoveIndex);
    }



5. In the update call to monitor the end of the jump

                // 调用监听跳跃结束的方法

changed to

                // 调用监听跳跃结束的方法
                this.onOnceJumpEnd();

28. Modify GameManager.ts after monitoring the end of the jump

1. Add an empty onPlayerJumpEnd

    // 更新计步器的显示并判断"跳坑失败或游戏完成"

changed to

    // 更新计步器的显示并判断"跳坑失败或游戏完成"
    onPlayerJumpEnd(moveIndex: number) {
    
    }

2.start

    // 组件第一次激活时调用的方法
    start() {
    
    
        this.generateRoad();
    }

changed to

    // 组件第一次激活时调用的方法
    start() {
    
    
        this.curState = GameState.GS_INIT;
        this.playerCtrl?.node.on("JumpEnd", this.onPlayerJumpEnd, this);
    }

3.checkResult

    // 判断跳坑失败或游戏完成的方法

changed to

    // 判断跳坑失败或游戏完成的方法
    checkResult(moveIndex: number) {
    
    
        if (moveIndex < this.roadLength) {
    
    
            if (this._road[moveIndex] == BlockType.BT_NONE) {
    
    
                //跳到了空方块上
                this.curState = GameState.GS_INIT;
            }
        } else {
    
    
            // 跳过了最大长度
            this.curState = GameState.GS_INIT;
        }
    }

4. Populate onPlayerJumpEnd

    // 更新计步器的显示并判断"跳坑失败或游戏完成"
    onPlayerJumpEnd(moveIndex: number) {
    
    }

changed to

    // 更新计步器的显示并判断"跳坑失败或游戏完成"
    onPlayerJumpEnd(moveIndex: number) {
    
    
        if (this.stepsLabel) {
    
    
            this.stepsLabel.string =
                "" +
                (moveIndex >= this.roadLength ? this.roadLength : moveIndex);
        }
        this.checkResult(moveIndex);
    }

29. Basic finished product

The official tutorial ends here
1. Clear the level
Please add a picture description
2. Fail directly
Please add a picture description

other

Compared with Cocos Creator2, Cocos Creator3 lacks the control library, which can be created by right-clicking in the hierarchy window



Please add a picture description

Guess you like

Origin blog.csdn.net/qq_39124701/article/details/129936289