Cocos2d 基础知识 2

 单点触摸

//触摸开始

bool  Chapter05::onTouchBegan(Touch*touch,Event *unused_event)

//{

//    log("Touch Began\r\n");

//    Vec2 touchPoint =touch->getLocation();

//    log("x:%f,y%f",touchPoint.x,touchPoint.y);

//    return true;

//}

//触摸移动

// void  Chapter05::onTouchMoved(Touch*touch,Event *unused_event)

//{

//    auto drawNode=DrawNode::create();//创造 轨迹

//    this->addChild(drawNode,10);


//   Vec2 touchPoint =touch->getPreviousLocation();

//    log("Touch Moved\r\n");

//     log("x:%f,y%f",touchPoint.x,touchPoint.y);

//    if (sp->getBoundingBox().containsPoint(touch->getLocation()))//边界框  包含()

//    {

//        sp->setPosition(touch->getLocation());// 位置

        drawNode->drawLine(touch->getLocation(), touch->getPreviousLocation(), Color4F(1, 0, 0, 1));

//    }

//    drawNode->drawLine(touch->getLocation(), touch->getPreviousLocation(), Color4F(1, 0, 0, 1));

//

//}

触摸结束


// void  Chapter05::onTouchEnded(Touch*touch,Event *unused_event)

//{

//    Vec2 touchPoint =touch->getStartLocation();

//    log("Touch Ended\r\n");

//

//}

触摸取消:例 手机出现了来电,是我们游戏进入后台

// void  Chapter05::onTouchCancelled(Touch*touch,Event *unused_event)

//{

//    long("Touch Cancelled\r\n");

//}



//单点触摸


onEnter()

{

/*//    auto touchListener=EventListenerTouchOneByOne::create();//触摸监听

//    touchListener->onTouchBegan=CC_CALLBACK_2(Chapter05::onTouchBegan, this);

//    touchListener->onTouchMoved=CC_CALLBACK_2(Chapter05::onTouchMoved, this);

//    touchListener->onTouchEnded=CC_CALLBACK_2(Chapter05::onTouchEnded, this);

//    touchListener->onTouchCancelled=CC_CALLBACK_2(Chapter05::onTouchCancelled, this);

//    

//  Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority (touchListener,this);//注册该监听

//    drawNode->drawLine(touch->getLocation(), touch->getPreviousLocation(), Color4F(1, 0, 0, 1));*/

}

在哪里注册就在哪里销毁,销毁触摸接听

void Chapter05::onExit()

{

    Director::getInstance()->getEventDispatcher()->removeEventListenersForTarget(this);

    Layer::onExit();

}

或者种形式

void ChaCase1::onEnter()

{

    Layer::onEnter();

    auto  touchListener = EventListenerTouchOneByOne::create();

    touchListener->onTouchBegan = CC_CALLBACK_2(ChaCase1::onTouchBegan,this);

    touchListener->onTouchMoved = CC_CALLBACK_2(ChaCase1::onTouchMoved,this);

    touchListener->onTouchEnded = CC_CALLBACK_2(ChaCase1::onTouchEnded, this);

    touchListener->onTouchCancelled=[=](Touch* t,Event * e)

    {

        Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touchListener, this);

    };

    touchListener->setSwallowTouches(true);

    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touchListener,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值