Hello World project

This is the very first Cocos Creator project, and it aims to demonstrate the following:

  • Creating a project
  • Understanding the project directory
  • Creating an object
  • Modifying Camera properties
  • Creating, modifying, and binding scripts
  • Running projects

Creating a New Project

In the Dashboard panel, click the New button in the lower right corner and select Creator in the pop-up menu.

Hello world! - 图1

Select an empty template, set the project path, and click the Create button below.

new

The Editor interface

This is the default layout of the Editor interface:

engine

The layout is customizable, if you don’t find the default layout suitable.

Project Directory

Usually, the most commonly used directory is assets. There are others:

  • assets (resources directory)
  • build (build directory)
  • library (imported resources directory)
  • local (log file directory)
  • profiles (editor configuration)
  • temp (temporary file directory)
  • package.json (project configuration)

Creating a New Scene

In the bottom left Assets panel, click the right mouse button and select New -> Scene.

scene

Creating an Object

Upper left Hierarchy panel, click the right mouse button, select Create -> 3D object -> Cube cube. The created cube will appear in the scene editor.

cube

Modifying the Camera

Selecting the Camera object

In the Hierarchy panel, select Camera, and the scene editor will select it and display a Gizmo.

select

Modifying the Camera position

In the scene editor, drag the Gizmo so that the Camera can see the created cube.

move

Modifying the Camera background color

In the Inspector panel panel, click the Color property and select black as the background color.

property

Adding a script

Creating a new script

In the Assets panel, click the right mouse button, select New -> TypeScript.

script

Life cycle functions

Life cycle functions (called in the following order):

  • onLoad Called when the script is initialized
  • onEnable Called when the enabled property of the component changes from false to true
  • start Called when the component is first activated
  • update Update object call before rendering each frame
  • lateUpdate Called after the update of all components has been executed
  • onDisable Called when the enabled property of the component changes from true to false
  • onDestroy Called when the component or node is destroyed

Adding code

Add start() function to output Hello world:

  1. import { _decorator, Component, Node } from 'cc';
  2. const {ccclass, property} = _decorator;
  3. @ccclass('HelloWorld')
  4. export class HelloWorld extends Component {
  5. /* class member could be defined like this */
  6. // dummy ='';
  7. /* use `property` decorator if your want the member to be serializable */
  8. // @property
  9. // serializableDummy = 0;
  10. start () {
  11. // Your initialization goes here.
  12. console.info('Hello world');
  13. }
  14. // // update (deltaTime: number) {
  15. // // Your update function goes here.
  16. //}
  17. }

Bind scripts to objects

Select the created cube and click Add component -> Custom Script->HelloWorld

component

Running a project

In the Editor, from the menu bar click Project -> Run preview, or Click the Run button in the middle.

run

Debuging a project

In the Editor, from the menu bar click Developer -> Scene Debugging Tool

It is also may be necessary to Log information. The Console panel displays all log output.

console

Breakpoints can also be placed for stopping execution of the debugger to examine values. Select the Source option on the tab bar and press CTRL+P, Search for HelloWorld.ts, set a breakpoint in the start() function, and then run the preview to debug.

debug