码迷,mamicode.com
首页 > 其他好文 > 详细

cocos2dx-3.2 笔记 - 点击事件

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

标签:des   style   blog   color   os   io   strong   div   

点击事件检测

直接上代码

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 笔记 - 点击事件

标签:des   style   blog   color   os   io   strong   div   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!