mirror of
https://github.com/Leo501/CocosCreatorTutorial.git
synced 2026-05-09 07:07:31 +08:00
添加一个 android 返回键 demo
This commit is contained in:
52
AndroidBackEventDemo/assets/Script/HelloWorld.js
Normal file
52
AndroidBackEventDemo/assets/Script/HelloWorld.js
Normal file
@@ -0,0 +1,52 @@
|
||||
cc.Class({
|
||||
extends: cc.Component,
|
||||
|
||||
properties: {
|
||||
label: {
|
||||
default: null,
|
||||
type: cc.Label
|
||||
},
|
||||
text: 'Hello, World!'
|
||||
},
|
||||
|
||||
// use this for initialization
|
||||
onLoad: function () {
|
||||
this.label.string = this.text;
|
||||
this.registerEvent();
|
||||
},
|
||||
|
||||
registerEvent() {
|
||||
//android 返回键
|
||||
cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
|
||||
},
|
||||
|
||||
onKeyDown(event) {
|
||||
switch (event.keyCode) {
|
||||
case cc.KEY.back:
|
||||
if (this.isBackGame) {
|
||||
cc.director.end();
|
||||
return;
|
||||
}
|
||||
this.isBackGame = true;
|
||||
this.label.string = '再次按返回键\n将退出游戏';
|
||||
this.node.runAction(cc.sequence(cc.delayTime(3), cc.callFunc(() => {
|
||||
this.isBackGame = false;
|
||||
this.label.string = '';
|
||||
})));
|
||||
break;
|
||||
}
|
||||
},
|
||||
|
||||
offEvent() {
|
||||
cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
|
||||
},
|
||||
|
||||
// called every frame
|
||||
update: function (dt) {
|
||||
|
||||
},
|
||||
|
||||
onDestroy() {
|
||||
this.offEvent();
|
||||
}
|
||||
});
|
||||
9
AndroidBackEventDemo/assets/Script/HelloWorld.js.meta
Normal file
9
AndroidBackEventDemo/assets/Script/HelloWorld.js.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "1.0.5",
|
||||
"uuid": "280c3aec-6492-4a9d-9f51-a9b00b570b4a",
|
||||
"isPlugin": false,
|
||||
"loadPluginInWeb": true,
|
||||
"loadPluginInNative": true,
|
||||
"loadPluginInEditor": false,
|
||||
"subMetas": {}
|
||||
}
|
||||
Reference in New Issue
Block a user