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

cocos3.0通过精灵控制精灵的触摸事件的实现

时间:2014-05-07 15:41:12      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:des   blog   class   code   tar   width   

auto listener = EventListenerTouchOneByOne::create();
	listener->setSwallowTouches(true);
	auto  sprite = this->getChildByTag(virTag)->getChildByTag(jumpTag);
	
	listener->onTouchBegan = CC_CALLBACK_2(MLayer::onTouchBegan, this);
	listener->onTouchMoved = CC_CALLBACK_2(MLayer::onTouchMoved, this);
	listener->onTouchEnded = CC_CALLBACK_2(MLayer::onTouchEnded, this);
	_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, sprite);  //该精灵享有触摸

	return  true;

}

bool  MLayer::onTouchBegan(Touch* touch, Event*  event)
{
	auto target = static_cast<Sprite*>(event->getCurrentTarget());//获取当前的触摸目标(那个跳跃精灵)
	Point locationInNode = target->convertToNodeSpace(touch->getLocation());//将本地坐标系转化为精灵坐标系(以精灵的左下角作为坐标原点)
	Size s= target->getContentSize();//获取精灵的文本尺寸大小
	Rect rect = Rect(0, 0, s.width, s.height);//获取精灵的矩形框(起始点为精灵的左下角)
	if (rect.containsPoint(locationInNode))//判断触摸点是否在精灵的矩形框上
	{
		auto sprite2 =this->getChildByTag(cmsTag)->getChildByTag(catTag);
		sprite2->setScale(0.2f);
	}
	
	return true;
}
void MLayer::onTouchMoved(Touch* touch, Event* event)
{
	auto target = static_cast<Sprite*>(event->getCurrentTarget());//获取当前的触摸目标
	Point locationInNode = target->convertToNodeSpace(touch->getLocation());//将本地坐标系转化为精灵坐标系(以精灵的左下角作为坐标原点)
	Size s = target->getContentSize();//获取精灵的文本尺寸大小
	Rect rect = Rect(0, 0, s.width, s.height);//获取精灵的矩形框(起始点为精灵的左下角)
	if (rect.containsPoint(locationInNode))//判断触摸点是否在精灵的矩形框上
	{
		target->setPosition(target->getPosition() + touch->getDelta());//将捕获的精灵坐标设在移动结束的地方
	}
}
void MLayer::onTouchEnded(Touch* touch ,Event* event)
{
	auto sprite2 = this->getChildByTag(cmsTag)->getChildByTag(catTag);
	sprite2->setScale(0.6f);
}

bubuko.com,布布扣

cocos3.0通过精灵控制精灵的触摸事件的实现,布布扣,bubuko.com

cocos3.0通过精灵控制精灵的触摸事件的实现

标签:des   blog   class   code   tar   width   

原文地址:http://blog.csdn.net/u010296979/article/details/25199345

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