cocos2d-x 3.x 触摸事件

HelloWorldScene.h

   bool touchBegan(cocos2d::Touch*touch, cocos2d::Event*event);//触摸开始,返回bool类型
    void touchMoved(cocos2d::Touch*touch, cocos2d::Event*event);
    void touchEnded(cocos2d::Touch*touch, cocos2d::Event*event);
    void touchCancelled(cocos2d::Touch*touch, cocos2d::Event*event);

HelloWorldScene.cpp

   EventListenerTouchOneByOne*listener=EventListenerTouchOneByOne::create();
    listener->onTouchBegan=CC_CALLBACK_2(HelloWorld::touchBegan,this);//触摸开始,必须添加,不使用也要添加,否则运行不通过
    listener->onTouchMoved=CC_CALLBACK_2(HelloWorld::touchMoved,this);//触摸移动
    listener->onTouchEnded=CC_CALLBACK_2(HelloWorld::touchEnded,this);//触摸中断,一般是系统层级的消息,如来电话,触摸事件就会被打断 
    listener->onTouchCancelled=CC_CALLBACK_2(HelloWorld::touchCancelled,this);
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener,this);




bool HelloWorld::touchBegan(Touch*touch, Event*event){
    Vec2 pos=touch->getLocation();
    log("touchBegan x:%f,y:%f",pos.x,pos.y);
    return true;//返回false时,触摸移动、触摸结束不会触发
}

void HelloWorld::touchMoved(Touch*touch, Event*event){
    Vec2 pos=touch->getLocation();
    log("touchMoved x:%f,y:%f",pos.x,pos.y);
}

void HelloWorld::touchEnded(Touch*touch, Event*event){
    Vec2 pos=touch->getLocation();
    log("touchEnded x:%f,y:%f",pos.x,pos.y);
}

void HelloWorld::touchCancelled(Touch*touch, Event*event){
    Vec2 pos=touch->getLocation();
    log("touchCancelled x:%f,y:%f",pos.x,pos.y);
}

 

转载于:https://www.cnblogs.com/kingBook/p/5569607.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值