本教程适用于2.4.0版本的状态机 (3.X版本用到promise,前端可能会用兼容问题)
在官方下载https://github.com/jakesgordon/javascript-state-machine
或者在我网盘也可以链接:http://pan.baidu.com/s/1nvDSYYt 密码:hovp
先将state-machine.js或者state-machine-min.js放入工程中
然后新建脚本,代码如下:
var StateMachine = require('state-machine');
/*
fsm.current :返回当前状态。
* fsm.is(s) :返回一个布尔值,表示状态s是否为当前状态。
* fsm.can(e) :返回一个布尔值,表示事件e是否能在当前状态触发。
* fsm.cannot(e) :返回一个布尔值,表示事件e是否不能在当前状态触发。
callbacks:
* onbeforeevent :任一事件发生之前触发。例:onbeforeplay
* onleavestate :离开任一状态时触发。例:onleavegame
* onenterstate :进入任一状态时触发。例:onentergame
* onafterevent :任一事件结束后触发。例:onafterplay
*/
cc.Class({
extends: cc.Component,
properties: {
label: cc.Label,
},
// use this for initialization
onLoad: function () {
var sel