1、node和u3d中的gameobject相似。
2、事件(鼠标事件和触屏事件的定义方式和原理还有些模糊。需要进一步学习)
2.1 点击事件可以通过回调方式挂载
// LIFE-CYCLE CALLBACKS:
btnClick(event, customEventData){
//console.log("event=",event.type," data=",customEventData);
};
onLoad () {
let self = this;
2.2 也可单独写脚本挂载node上const {ccclass, property} = cc._decorator;
import Global = require("./GlobalEvents");
@ccclass
export default class NewClass extends cc.Component {
@property(cc.Button)
btnplay: cc.Button = null;
@property(cc.Button)
btnsound: cc.Button = null;
@property(cc.Button)
btnmusic: cc.Button = null;
@property(cc.Button)
btntotal: cc.Button = null;
@property(cc.SpriteFrame)
soundenbPic: cc.SpriteFrame = null;
@property(cc.SpriteFrame)
sounddisPic: cc.SpriteFrame = null;
@property(cc.SpriteFrame)
musicenbPic: cc.SpriteFrame = null;
@property(cc.SpriteFrame)
musicedisPic: cc.SpriteFrame = null;
// LIFE-CYCLE CALLBACKS:
onLoad () {
var self = this;
//加载按钮动画事件
self.btnsound.node.on(cc.Node.EventType.TOUCH_START, function (event) {
//console.log(' TOUCH_START');
});
self.btnsound.node.on(cc.Node.EventType.TOUCH_MOVE, function (event) {
//console.log(' TOUCH_MOVE');
self.btnsound.getComponent(cc.Animation).play();
});
self.btnsound.node.on(cc.Node.EventType.TOUCH_END, function (event) {
//console.log(' TOUCH_END');
self.btnsound.getComponent(cc.Animation).stop();
//换图片
if(Global.isSound==1){
//Global.isSound = 0;
self.btnsound.getComponent(cc.Sprite).spriteFrame = self.sounddisPic;
}
else{
//Global.isSound = 1;
self.btnsound.getComponent(cc.Sprite).spriteFrame = self.soundenbPic;
}
});
self.btnsound.node.on(cc.Node.EventType.TOUCH_CANCEL, function (event) {
//console.log(' TOUCH_CANCEL');
self.btnsound.getComponent(cc.Animation).stop();
});
self.btnsound.node.on(cc.Node.EventType.MOUSE_ENTER, function (event) {
//console.log(' MOUSE_ENTER');
self.btnsound.getComponent(cc.Animation).play();
});
self.btnsound.node.on(cc.Node.EventType.MOUSE_LEAVE, function (event) {
//console.log(' MOUSE_LEAVE');
self.btnsound.getComponent(cc.Animation).stop();
});