1 月 2019

CocosCreator 2.0的cc.eventManager弃用换新API记录

Cocos Creator升级2.0以后发现原来写的一些代码开始报弃用警告了,今天有空学习下新API并更新下原来的代码,之前写的实现监听touch起动停事件的代码是这样写的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// use this for initialization
onLoad: function () {
    let self = this;
 
    cc.eventManager.addListener({
        event: cc.EventListener.TOUCH_ONE_BY_ONE,
 
        onTouchBegan: function (touch, event) {
            // x = touch.getLocation().x
            // y = touch.getLocation().y
 
            return true;
        },
        onTouchMoved: function (touch, event) {
            // x = touch.getLocation().x
            // y = touch.getLocation().y
 
            return true;
        },
        onTouchEnded: function (touch, event) {
            const x = touch.getLocation().x;
            const y = touch.getLocation().y;
            const cp = self.dragonPlayer.getPosition();
            NetworkManager.instance.getNetPhoton().sendMyPost(cp.x, cp.y, x, y);
            const dist = cc.v2(x, y).sub(cp).mag();
            self.dragonPlayer.stopAllActions();
            self.dragonPlayer.runAction(cc.moveTo(dist / 80, x, y));
            return true;
        }
    }, self.node);
},
Continue reading…