cocos2d-x 单点触摸事件使用

前端之家收集整理的这篇文章主要介绍了cocos2d-x 单点触摸事件使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#ifndef__USE_SPRIT_SCENE_H__
#define__USE_SPRIT_SCENE_H__
#include"cocos2d.h"

typedefenum
{
	Box_a=102,Box_b,Box_c
}SpriteTags;



classUseSprit:publiccocos2d::Layer
{
public:
	staticcocos2d::Scene*createScene();
	//there'sno'id'incpp,sowerecommendreturningtheclassinstancepointer

	//Here'sadifference.Method'init'incocos2d-xreturnsbool,insteadofreturning'id'incocos2d-iphone
	virtualboolinit();

	//
	virtualvoidonEnter();

	//
	virtualvoidonExit();

	booltouchBegan(cocos2d::Touch*touch,cocos2d::Event*event);

	voidtouchMoved(cocos2d::Touch*touch,cocos2d::Event*event);

	voidtouchEnded(cocos2d::Touch*touch,cocos2d::Event*event);

	//implementthe"staticcreate()"methodmanually
	CREATE_FUNC(UseSprit);

};

#endif

说明:1.单点触摸事件分为三段:开始拖动(touchBegan)、拖移(touchMoved)、释放拖动(touchEnded);

2.它们拥有共同参数:cocos2d::Touch* touch,cocos2d::Event *event;

3.onEnter 初始化的时候调用,即 刚要显示当前场景的时候(场景进入的时候调用);

4.onExit 场景退出的时候调用

#include"UseSpritScene.h"

USING_NS_CC;

Scene*UseSprit::createScene()
{
	//'scene'isanautoreleaSEObject
	autoscene=Scene::create();

	//'layer'isanautoreleaSEObject
	autolayer=UseSprit::create();

	//addlayerasachildtoscene
	scene->addChild(layer);

	//returnthescene
	returnscene;
}

//on"init"youneedtoinitializeyourinstance
boolUseSprit::init()
{
	//////////////////////////////
	//1.superinitfirst
	if(!Layer::init())
	{
		returnfalse;
	}

	SizevisibleSize=Director::getInstance()->getVisibleSize();
	Vec2origin=Director::getInstance()->getVisibleOrigin();

	//
	autobg=Sprite::create("bg.png",Rect(0,visibleSize.width,visibleSize.height));
	Texture2D::TexParamstp={GL_LINEAR,GL_LINEAR,GL_REPEAT,GL_REPEAT};
	bg->getTexture()->setTexParameters(tp);
	bg->setPosition(origin+Vec2(visibleSize.width/2,visibleSize.height/2));
	addChild(bg,0);

	Sprite*BoxA=Sprite::create("ygq.png");
	BoxA->setPosition(origin+Vec2(visibleSize.width/2,visibleSize.height/2)+Vec2(-120,120));
	addChild(BoxA,10,Box_a);
	
	Sprite*BoxB=Sprite::create("yg.png");
	BoxB->setPosition(origin+Vec2(visibleSize.width/2,visibleSize.height/2));
	addChild(BoxB,20,Box_b);

	Sprite*BoxC=Sprite::create("ygq.png");
	BoxC->setPosition(origin+Vec2(visibleSize.width/2,visibleSize.height/2)+Vec2(120,160));
	addChild(BoxC,30,Box_c);


	returntrue;
}

voidUseSprit::onEnter()
{
	Layer::onEnter();
	log("UseSpritonEnter");

	autolistener=EventListenerTouchOneByOne::create();

	listener->setSwallowTouches(true);
	listener->onTouchBegan=CC_CALLBACK_2(UseSprit::touchBegan,this);
	listener->onTouchMoved=CC_CALLBACK_2(UseSprit::touchMoved,this);
	listener->onTouchEnded=CC_CALLBACK_2(UseSprit::touchEnded,this);

	EventDispatcher*eventDispatcher=Director::getInstance()->getEventDispatcher();

	eventDispatcher->addEventListenerWithSceneGraPHPriority(listener,getChildByTag(Box_a));
	eventDispatcher->addEventListenerWithSceneGraPHPriority(listener->clone(),getChildByTag(Box_b));
	eventDispatcher->addEventListenerWithSceneGraPHPriority(listener->clone(),getChildByTag(Box_c));

}


boolUseSprit::touchBegan(Touch*touch,Event*event)
{
	autotarget=static_cast<Sprite*>(event->getCurrentTarget());

	Vec2locationInNode=target->convertToNodeSpace(touch->getLocation());

	Sizes=target->getContentSize();

	Rectrect=Rect(0,s.width,s.height);

	if(rect.containsPoint(locationInNode))
	{



		target->runAction(ScaleBy::create(0.06f,1.06f));
		returntrue;


	}


	returnfalse;

}

voidUseSprit::touchMoved(Touch*touch,Event*event)
{
	autotarget=static_cast<Sprite*>(event->getCurrentTarget());
	target->setPosition(target->getPosition()+touch->getDelta());

}


voidUseSprit::touchEnded(Touch*touch,Event*event)
{
	autotarget=static_cast<Sprite*>(event->getCurrentTarget());


	Vec2locationInNode=target->convertToNodeSpace(touch->getLocation());

	Sizes=target->getContentSize();

	Rectrect=Rect(0,1.0f));


	}

}

voidUseSprit::onExit()
{
	Layer::onExit();
	Director::getInstance()->getEventDispatcher()->removeAllEventListeners();
}
原文链接:https://www.f2er.com/cocos2dx/342189.html

猜你在找的Cocos2d-x相关文章