2019年3月3日

Creator-Typescript 入门案例

作者 admin

用typescript 做了一个点击效果 跟 拖拽效果 ,

废话不逼逼 请看灵魂作者的demo视频

直接上代码
const { ccclass, property } = cc._decorator;

@ccclass
export default class Main extends cc.Component {

//声明类型
@property(cc.Label)
label: cc.Label = null;

//声明组件
@property
text: string = ‘hello’;

//声明类型
@property(cc.Node)
cocos: cc.Node = null;

//声明类型
@property(cc.Node)
prefabsJs: cc.Node = null;

start() {
this.label.string = this.text;
this.loadStartGameBtn(this.touchText)
this.movePic()
}

//声明变量
private count: number = 0;
public textNode: cc.Node
//点击文本累加
private touchText(): void {
this.textNode = this.label.node;
this.label.node.on(cc.Node.EventType.TOUCH_START, (event: TouchEvent) => {

this.count++
this.prefabsJs.getComponent(‘PrefabsJs’).playStar(this.textNode)

console.log(event.type + ‘分割线’ + this.count, “分割” + this.textNode);

}, this);
}
//图片拖拽功能
private movePic(): void {
this.cocos.on(‘touchmove’, (ev) => {
// console.log(this.count);
var move = ev.touch.getDelta();
this.cocos.x += move.x;
this.cocos.y += move.y;
})
}
//游戏预加载页面
public loadStartGameBtn(func) {
const splash = window.document.getElementById(‘splash’);
const startBtn = window.document.getElementById(“start-btn”) //开始按钮
startBtn.style.display = ‘inline-block’;
startBtn.onclick = () => {
splash.style.display = ‘none’
if (func) {
func.bind(this)()
}
}
}
}