One, typescript simple and quick start
首先你的有其他开发语言的基础,如js java jQuery 等等
Article directory
- One, typescript simple and quick start
- Second, the development history of Cocos Creator
- Third, the basic syntax of typescript
-
- 1. Variable type
- 2, enumeration type
- 3. Type aliases
- 4. Function definition
- 5. Object creation and use
- 6, the construction method
- 6, static method
- 7. Definition of abstract class
- 8, the definition of the interface
- 9, attribute register
- 10. Namespace
- 11. Generics
- 12, tuple array dictionary
- 13. Callback
- 14. Modifiers
Second, the development history of Cocos Creator
In February 2008, the Python version of Cocos2D was born
In June 2008, the Objective-C version of Cocos2D for iphone was born, pushing Cocos to its peak
After that, cocos versions in various languages appeared, such as:
Cocos2d-x Cocos2d-js Cocos2d-android Cocos2d-net etc.
Among these versions, the most meaningful is that Cocos2d-x can easily make a cross-platform version. After that, Cocos2D-X gave birth to two branches: Cocos2D-xna for wp system and 2D-HTML5
The birth of CocosCreator is to decompose the pure code editing of Cocos2D-x into features such as visualization and scripting, so that more newcomers can easily get started
提示:以下是本篇文章正文内容,下面案例可供参考
Third, the basic syntax of typescript
1. Variable type
Number type declaration
let tmp1: number = 21;
character type declaration
let tmp2: string = "21";
boolean type declaration
let tmp3: boolean = true;
any type declaration
// any 属于任意类型 类似于var
let tmp4: any = "111";
array type declaration
let a: number[] = [1,2,3,4,5]
union type declaration
// 当前num变量既可以保存字符又可以保存数字
let num: number | string = 0;
output of special characters
2, enumeration type
enum Color{
red,
blue,
green
}
// 取值/赋值
let tmp:Color = Color.green
3. Type aliases
type NewNumber = number;
let num:NewNumber = 3;
4. Function definition
// 接收一个字符 和一个数字类型参数
function func(char:string,num:number){
}
5. Object creation and use
class Person{
name:string = "默认值";
age:number = 0;
say(){
// 方法体
}
}
// 实例化对象
let a = new Person();
a.name = "唐僧";
a.age = 20;
a.say();
6, the construction method
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, static method
class Person{
name:string = "默认值";
age:number = 0;
constructor(name:string,age:number){
this.name = name;
this.age = age;
}
static test(){
}
say(){
// 方法体
}
}
// 调用静态方法
Person.test();
7. Definition of abstract class
abstract class Person{
name:string = "";
run(){
}
abstract say();
}
class Student extends Person{
say(){
}
}
let a:Person = new Student();
8, the definition of the interface
class Person{
name:string;
}
interface I1{
a();
}
interface I2{
b();
}
class Test extends Person implements I1,I2{
// 重写 a方法 和 b方法
a(){
}
b(){
}
}
9, attribute register
class Person{
_hp:number = 100;
get hp(){
return this._hp;
}
set hp(value){
this._hp = value
}
}
// 注意的是,实例直接 点 hp即可
let a = new Person();
a.hp = 180;
10. Namespace
anti-collision
namespace aa{
export class Person{
name:string
}
}
namespace bb{
export class Person{
}
}
// 实例化
let person = new aa.Person();
let person2 = new bb.Person();
11. Generics
// function add(num: any): any{}//这句代码代表了传入任意类型,返回任意类型
// T 则一样
function add<T>(num:T):T{
if(typeof num == "number"){
num++;
return num;
}
return num;
}
12, tuple array dictionary
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"] = "八戒";
13. Callback
// 函数传参
function func(value: Function){
value();
}
function test(){
console.log("test111"):
}
func(test)
func(function(){
console.log("test22222")
})
func(()=>{
console.log("test3333")
})
14. Modifiers
public public
protected protected
private private