欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Cocos 2d事件模块

程序员文章站 2024-03-16 21:15:52
...

事件Event

cc.EventManager(事件管理器)
管理用户注册的监听器,根据触发的时间类型非法给响应的事件监听器
cc.EventListener(事件监听器)
用户事件处理逻辑
cc.Event(事件对象)
事件类型

鼠标事件Mouse

鼠标事件-event参数

event.getLocation();
event.getCurrentTarget();获取事件对象
event.getUserData();获取事件对象
event.getButton();判定左右键
event.getDelta();onMouseMove有效

监听函数中有一个event函数,可以传递操作参数

鼠标事件的定义

if('mouse' in cc.sys.capabilities) {
    var listenerSushi = cc.EventListener.create({
        event: cc.EventListener.MOUSE,
        onMouseDown: function (event) {},
        onMouseMove: function (event) {},
        onMouseUp: function (event) {}
    });
    cc.eventManager.addListener(listenerSushi, spriteSushi);
}

对象选中

onMouseDown: function (event) {
    var pos = event.getLocation();
    var target = event.getCurrentTarget();
    if (cc.rectContainsPoint(target.getBoundingBox(), pos)) {
        console.log("金币点击");
    }
}

示例代码

var MouseScene3 = cc.Scene.extend({
    sprite:null,
    ctor: function () {
        this._super();
        var size = cc.director.getWinSize();
        this.sprite = new cc.Sprite("res/coin/coin.png");
        this.sprite.x = size.width / 2;
        this.sprite.y = size.height / 2;
        this.addChild(this.sprite);

        var listener = cc.EventListener.create({
            event: cc.EventListener.MOUSE,
            onMouseDown: function (event) {
                var pos = event.getLocation();
                var target = event.getCurrentTarget();
                if (cc.rectContainsPoint(target.getBoundingBox(), pos)) {
                    console.log("金币点击");
                }
            },
            onMouseMove: function (event) {
            },
            onMouseUp: function (event) { }
        });
        cc.eventManager.addListener(listener, this.sprite);
        

        var spriteSushi = new cc.Sprite("res/sushi_1n.png");
        spriteSushi.x = size.width / 2-200;
        spriteSushi.y = size.height / 2;
        this.addChild(spriteSushi);
        if('mouse' in cc.sys.capabilities) {
            var listenerSushi = cc.EventListener.create({
                event: cc.EventListener.MOUSE,
                onMouseDown: function (event) {
                    var pos = event.getLocation();
                    var target = event.getCurrentTarget();
                    if (cc.rectContainsPoint(target.getBoundingBox(), pos)) {
                        console.log("棍子点击");
                    }
                },
                onMouseMove: function (event) {
                },
                onMouseUp: function (event) { }
            });
            cc.eventManager.addListener(listenerSushi, spriteSushi);
        }
    }
})

键盘事件keyboard

单点触摸-事件的定义

if ('keyboard' in cc.sys. capabilities) 
{

    var listener = cc.EventListener.create({

        event: cc.EventListener.KEYBOARD,
        onKeyPressed: function (keycode, event) { },
        onKeyReleased: function (keycode, event) { }

    });
    cc.eventManager.addListener(listener, this);
}

监听函数中有一个event、touch参数,可以传递操作参数

ADSW实现移动

var KeyboardScene = cc.Scene.extend({
    ctor: function () {
        this._super();
        var size = cc.director.getWinSize();
        var sprite = new cc.Sprite("res/coin/coin.png");
        sprite.x = size.width / 2;
        sprite.y = size.height / 2;
        this.addChild(sprite);

        var listener = cc.EventListener.create({
            event: cc.EventListener.KEYBOARD,
            onKeyPressed: function (keycode, event) {
                console.log(keycode);
                var target = event.getCurrentTarget();
                switch (keycode) {
                    case 65:
                        target.x -= 5;
                        console.log(target.x);
                        break;
                    case 68:
                        target.x += 5;
                        break;
                    case 87:
                        target.y += 5;
                        break;
                    case 83:
                        target.y -= 5;
                        break;
                }
            },
            onKeyReleased: function (keycode, event) { }
        });
        cc.eventManager.addListener(listener, sprite);
    }
})

Touch事件

左右键点击屏幕都可**

var TouchScene = cc.Scene.extend({
    ctor: function () {
        this._super();
        var size = cc.director.getWinSize();
        var sprite = new cc.Sprite("res/coin/coin.png");
        sprite.x = size.width / 2;
        sprite.y = size.height / 2;
        this.addChild(sprite);

        var listener = cc.EventListener.create({
            event: cc.EventListener.TOUCH_ONE_BY_ONE,
            onTouchBegan: function (touch, event) {
                var pos = touch.getLocation();
                var target = event.getCurrentTarget();

                if (cc.rectContainsPoint(target.getBoundingBox(), pos)) {
                    console.log("金币点击");
                    target.removeFromParent();
                }
            },
            onTouchMoved: function (touch, event) { },
            onTouchEnded: function (touch, event) { }
        });
        cc.eventManager.addListener(listener, sprite);
    }
})

自定义事件custom

自定义实现分数的增加:鼠标事件传递数据到自定义事件

var CustomScene = cc.Scene.extend({
    label: null,
    sprite: null,
    score:0,
    ctor: function () {
        this._super();
        this.addCoin();
        this.addLabel();
    },
    addCoin: function () {
        this.sprite = new cc.Sprite("res/coin/coin.png");
        var size = cc.director.getWinSize();
        this.sprite.x = size.width / 2;
        this.sprite.y = size.height / 2;
        this.addChild(this.sprite);
        var listener = cc.EventListener.create({

            event: cc.EventListener.MOUSE,
            onMouseDown: function (event) {

                var pos = event.getLocation();
                var target = event.getCurrentTarget();
                if (cc.rectContainsPoint(target.getBoundingBox(), pos)) {
                    //console.log("金币点击");
                    var event = new cc.EventCustom("GETSCORE");
                    var data = { score: 100 };
                    event.setUserData(data);
                    cc.eventManager.dispatchEvent(event);
                }
            }
        });
        cc.eventManager.addListener(listener, this.sprite);
    },
    addLabel: function () {
        this.label = new cc.LabelTTF("分数", "arial", 24);
        var size = cc.director.getWinSize();
        this.label.x = size.width / 2;
        this.label.y = size.height - 100;
        this.addChild(this.label);

        var listenerLabel = cc.EventListener.create({

            event: cc.EventListener.CUSTOM,
            eventName: "GETSCORE",
            callback: function (event) {
                //console.log("自定义事件被**");
                var data = event.getUserData()
                var target = event.getCurrentTarget();
                target.score += data.score;
                //target.setString(target.score);
                target.label.setString(target.score);
            }
        });
        cc.eventManager.addListener(listenerLabel, this);
    },
})
相关标签: cocos 2D