一,定制开发简单快速入门
定制开发首先你的有其他开发语言的基础,如js java jQuery 等等
文章目录
二, Creator 发展历史
2008年2月,Python版Cocos2D诞生
2008年6月,Objective-C版Cocos2D for iphone诞生,将Cocos推上高峰
之后,定制开发出现了各种语言的cocos版本,如:
Cocos2d-x Cocos2d-js Cocos2d-android Cocos2d-net等等
定制开发在这些版本之中,定制开发最有意义的就是Cocos2d-x 定制开发可以轻易做出跨平台版本,之后Cocos2D-X 诞生出两个分支 一个给wp系统用的Cocos2D-xna 还有一个2D-HTML5
CocosCreator的诞生是为了将Cocos2D-x的纯代码编辑,分解成可视化,脚本化等特点,让更多新人轻松上手
提示:以下是本篇文章正文内容,下面案例可供参考
三,typescript基本语法
1,变量类型
数字类型声明
let tmp1: number = 21;
- 1
字符类型声明
let tmp2: string = "21";
- 1
boolean类型声明
let tmp3: boolean = true;
- 1
any类型声明
// any 属于任意类型 类似于varlet tmp4: any = "111";
- 1
- 2
数组类型声明
let a: number[] = [1,2,3,4,5]
- 1
联合类型声明
// 当前num变量既可以保存字符又可以保存数字let num: number | string = 0;
- 1
- 2
特殊字符的输出
2,
enum Color{ red, blue, green}// 取值/赋值let tmp:Color = Color.green
- 1
- 2
- 3
- 4
- 5
- 6
- 7
3,类型别名
type NewNumber = number;let num:NewNumber = 3;
- 1
- 2
4,函数定义
// 接收一个字符 和一个数字类型参数function func(char:string,num:number){}
- 1
- 2
- 3
- 4
5,对象创建以及使用
class Person{ name:string = "默认值"; age:number = 0; say(){ // 方法体 }}// 实例化对象let a = new Person();a.name = "唐僧";a.age = 20;a.say();
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
6,构造方法
class Person{ name:string = "默认值"; age:number = 0; constructor(name:string,age:number){ this.name = name; this.age = age; } say(){ // 方法体 }}// 实例化对象let a = new Person("唐僧",20);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
6,静态方法
class Person{ name:string = "默认值"; age:number = 0; constructor(name:string,age:number){ this.name = name; this.age = age; } static test(){ } say(){ // 方法体 }}// 调用静态方法Person.test();
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
7,抽象类的定义
abstract class Person{ name:string = ""; run(){ } abstract say();}class Student extends Person{ say(){ }}let a:Person = new Student();
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
8,接口的定义
class Person{ name:string;}interface I1{ a();}interface I2{ b();}class Test extends Person implements I1,I2{ // 重写 a方法 和 b方法 a(){ } b(){ }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
9,属性寄存器
class Person{ _hp:number = 100; get hp(){ return this._hp; } set hp(value){ this._hp = value }}// 注意的是,实例直接 点 hp即可let a = new Person();a.hp = 180;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
10,名称空间
防止撞类
namespace aa{ export class Person{ name:string }} namespace bb{ export class Person{ }}// 实例化let person = new aa.Person();let person2 = new bb.Person();
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
11,泛型
// function add(num: any): any{}//这句代码代表了传入任意类型,返回任意类型// T 则一样function add<T>(num:T):T{ if(typeof num == "number"){ num++; return num; } return num;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
12,元组数组字典
let a:number[] = [1,2,3];let b:Array<number> = new Array<number>();// 长度a.length// 追加a.push(4);// 前面追加a.unshift(0);// 删除最后元素a.pop();// 从第几位开始删除几个a.splice(0,1);//删除最前面的a.shift();// 合并a = a.concat(b);// 查找位置let index = a.indexOf(3);// 排序a.sort();// 反转a.reverse();// 字典 定义为key为string value为string的字典let dic:{[key:string]:string} = { "name":"唐僧", "name2":"孙悟空"}// 字典赋值dic["name3"] = "八戒";
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
13,回调
// 函数传参function func(value: Function){value();}function test(){console.log("test111"):}func(test)func(function(){ console.log("test22222")})func(()=>{ console.log("test3333")})
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
14,修饰符
public 公开的
protected 受保护的
private 私有的