首页 > 其他 > 详细

cocos2dx-3.2 笔记 - 点击事件

时间:2014-08-22 12:13:06      阅读:419      评论:0      收藏:0      [点我收藏+]

点击事件检测

直接上代码

auto dispatcher = Director::getInstance()->getEventDispatcher();
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = CC_CALLBACK_2(GameLayer::onTouchBegan, this);
listener->onTouchMoved = CC_CALLBACK_2(GameLayer::onTouchMoved, this);
listener->onTouchEnded = CC_CALLBACK_2(GameLayer::onTouchEnded, this);
dispatcher->addEventListenerWithSceneGraphPriority(listener, this);




bool GameLayer::onTouchBegan(Touch* touch, Event *event)
{
    return true;
}
void GameLayer::onTouchMoved(Touch* touch, Event *event)
{

}
void GameLayer::onTouchEnded(Touch*touch, Event *event)
{

}

 

检测点击了Sprite

 

bool GameLayer::CheckTouchAli(Touch *touch)
{
    //将点击点转换成自己坐标系中的坐标,即相对于aliSprite(一个Sprite)的原点
    Vec2 pt = aliSprite->convertTouchToNodeSpace(touch);
    int nw = aliSprite->GetRect().size.width;
    int nh = aliSprite->GetRect().size.height;
    Rect rc(0,0, nw, nh);
    return rc.containsPoint(pt);
}

Sprite跟随拖动

void GameLayer::SetAliPosToTouchPos(Touch* touch)
{
    Vec2 pt = aliSprite->convertTouchToNodeSpace(touch);
    int nw = aliSprite->GetRect().size.width;
    int nh = aliSprite->GetRect().size.height;

    Vec2 touchPos = Vec2(aliSprite->getPositionX()-nw*0.5+pt.x
        ,aliSprite->getPositionY()-nh*0.5+pt.y);

    aliSprite->setPosition(touchPos);

}

 

cocos2dx-3.2 笔记 - 点击事件,布布扣,bubuko.com

cocos2dx-3.2 笔记 - 点击事件

原文:http://www.cnblogs.com/kee-chen/p/3928837.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!