一,typescript简单快速入门
首先你的有其他开发语言的基础,如js java jQuery 等等
文章目录
- 一,typescript简单快速入门
- 二,Cocos Creator 发展历史
- 三,typescript基本语法
- 1,变量类型
- 2,枚举类型
- 3,类型别名
- 4,函数定义
- 5,对象创建以及使用
- 6,构造方法
- 6,静态方法
- 7,抽象类的定义
- 8,接口的定义
- 9,属性寄存器
- 10,名称空间
- 11,泛型
- 12,元组数组字典
- 13,回调
- 14,修饰符
二,Cocos 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,变量类型
数字类型声明
复制
字符类型声明
复制
boolean类型声明
| let tmp3: boolean = true; |
复制
any类型声明
复制
数组类型声明
| let a: number[] = [1,2,3,4,5] |
复制
联合类型声明
| |
| let num: number | string = 0; |
复制
特殊字符的输出

2,枚举类型
| enum Color{ |
| red, |
| blue, |
| green |
| } |
| |
| let tmp:Color = Color.green |
复制
3,类型别名
| type NewNumber = number; |
| let num:NewNumber = 3; |
复制
4,函数定义
| |
| function func(char:string,num:number){ |
| |
| } |
复制
5,对象创建以及使用
| class Person{ |
| name:string = "默认值"; |
| age:number = 0; |
| |
| say(){ |
| |
| } |
| } |
| |
| let a = new Person(); |
| a.name = "唐僧"; |
| a.age = 20; |
| a.say(); |
| |
复制
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); |
复制
6,静态方法
| class Person{ |
| name:string = "默认值"; |
| age:number = 0; |
| |
| constructor(name:string,age:number){ |
| this.name = name; |
| this.age = age; |
| } |
| |
| static test(){ |
| } |
| |
| say(){ |
| |
| } |
| } |
| |
| Person.test(); |
复制
7,抽象类的定义
| abstract class Person{ |
| name:string = ""; |
| run(){ |
| } |
| abstract say(); |
| } |
| class Student extends Person{ |
| say(){ |
| } |
| } |
| let a:Person = new Student(); |
复制
8,接口的定义
| class Person{ |
| name:string; |
| } |
| interface I1{ |
| a(); |
| } |
| interface I2{ |
| b(); |
| } |
| |
| class Test extends Person implements I1,I2{ |
| |
| a(){ |
| |
| } |
| |
| b(){ |
| |
| } |
| } |
| |
复制
9,属性寄存器
| class Person{ |
| _hp:number = 100; |
| |
| get hp(){ |
| return this._hp; |
| } |
| |
| set hp(value){ |
| this._hp = value |
| } |
| } |
| |
| let a = new Person(); |
| a.hp = 180; |
复制
10,名称空间
防止撞类
| namespace aa{ |
| export class Person{ |
| name:string |
| } |
| } |
| |
| namespace bb{ |
| export class Person{ |
| |
| } |
| } |
| |
| let person = new aa.Person(); |
| let person2 = new bb.Person(); |
| |
复制
11,泛型
| |
| |
| function add<T>(num:T):T{ |
| if(typeof num == "number"){ |
| num++; |
| return num; |
| } |
| return num; |
| } |
复制
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(); |
| |
| |
| |
| let dic:{[key:string]:string} = { |
| "name":"唐僧", |
| "name2":"孙悟空" |
| } |
| |
| dic["name3"] = "八戒"; |
复制
13,回调
| |
| function func(value: Function){ |
| value(); |
| } |
| function test(){ |
| console.log("test111"): |
| } |
| func(test) |
| func(function(){ |
| console.log("test22222") |
| }) |
| func(()=>{ |
| console.log("test3333") |
| }) |
复制
14,修饰符
public 公开的
protected 受保护的
private 私有的