cocos2d-x 单点触摸事件使用

原创
2015/07/17 19:57
阅读数 79
  • 场景类头文件:

#ifndef __USE_SPRIT_SCENE_H__
#define __USE_SPRIT_SCENE_H__
#include "cocos2d.h"

typedef enum
{
	box_a = 102,
	box_b,
	box_c
} SpriteTags;



class UseSprit :public cocos2d::Layer
{
public:
	static cocos2d::Scene* createScene();
	// there's no 'id' in cpp, so we recommend returning the class instance pointer

	// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
	virtual bool init();

	//
	virtual void onEnter();

	//
	virtual void onExit();

	bool touchBegan(cocos2d::Touch* touch, cocos2d::Event *event);

	void touchMoved(cocos2d::Touch* touch, cocos2d::Event *event);

	void touchEnded(cocos2d::Touch* touch, cocos2d::Event *event);

	// implement the "static create()" method manually
	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' is an autorelease object
	auto scene = Scene::create();

	// 'layer' is an autorelease object
	auto layer = UseSprit::create();

	// add layer as a child to scene
	scene->addChild(layer);

	// return the scene
	return scene;
}

// on "init" you need to initialize your instance
bool UseSprit::init()
{
	//////////////////////////////
	// 1. super init first
	if (!Layer::init())
	{
		return false;
	}

	Size visibleSize = Director::getInstance()->getVisibleSize();
	Vec2 origin = Director::getInstance()->getVisibleOrigin();

	//
	auto bg = Sprite::create("bg.png", Rect(0, 0, visibleSize.width, visibleSize.height));
	Texture2D::TexParams tp = { 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);


	return true;
}

void UseSprit::onEnter()
{
	Layer::onEnter();
	log("UseSprit onEnter");

	auto listener = 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));

}


bool UseSprit::touchBegan(Touch *touch,Event *event)
{
	auto target = static_cast<Sprite*>(event->getCurrentTarget());

	Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation());

	Size s = target->getContentSize();

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

	if (rect.containsPoint(locationInNode))
	{



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


	}


	return false;

}

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

}


void UseSprit::touchEnded(Touch *touch, Event *event)
{
	auto target = static_cast<Sprite*>(event->getCurrentTarget());


	Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation());

	Size s = target->getContentSize();

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

	if (rect.containsPoint(locationInNode))
	{



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


	}

}

void UseSprit::onExit()
{
	Layer::onExit();
	Director::getInstance()->getEventDispatcher()->removeAllEventListeners();
}



























展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部