微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我理解的 cocos2d_x3.4 的单点触摸机制

因本人能力有限,请网友指针。不胜感激。自己简单修改的喵星战争的触摸机制源码

/*
GameObjHero Class DeFinitions
Created by taotao man on 2015-5-18
*/


#ifndef GameObjHero_h
#define GameObjHero_h


#include "cocos2d.h"
using namespace cocos2d;
// USING_NS_CC;

class GameObjHero : public CCNode//,public EventListenerTouchOneByOne //CCTargetedTouchDelegate用于处理单点触摸。
{
public:
CCSprite *lefthand;
CCSprite *righthand;
CCPoint offset;
bool iscontrol;
GameObjHero(void);
virtual ~GameObjHero(void);

virtual void onEnter();
virtual void onExit();
void releasebullet(float dt);

CCRect rect();
bool containsTouchLocation(CCTouch* touch); //注册触摸
//virtual bool ccTouchBegan(CCTouch* touch,CCEvent* event);
bool onTouchBegan(Touch *touch,Event *event);
//virtual void ccTouchMoved(CCTouch* touch,CCEvent* event);
void onTouchMoved(Touch *touch,Event *event);
//virtual void ccTouchEnded(CCTouch* touch,CCEvent* event);
void onTouchEnded(Touch *touch,Event *event);

virtual void touchDelegateRetain();
virtual void touchDelegateRelease();

};
#endif


/* GameObjHero Class Implementation Created by taotao man on 2015-5-20 */ #include "GameObjHero.h" #include "GameScene.h" GameObjHero::GameObjHero(void) { } GameObjHero::~GameObjHero(void) { } CCRect GameObjHero::rect() { CCSize s = CCSizeMake(85,90); return CCRectMake(-s.width / 2,-s.height / 2,s.width,s.height); } void GameObjHero::touchDelegateRetain() { this->retain(); } void GameObjHero::touchDelegateRelease() { this->release(); } void GameObjHero::onEnter() { CCNode::onEnter(); this->setContentSize(CCSizeMake(85,90)); CCDirector *pDirector = CCDirector::sharedDirector(); // pDirector->getTouchdispatcher()->addTargetedDelegate(this,true); // 事件触摸机制 //创建一个触摸监听器,这里使用单点触摸事件 auto TouchListener = EventListenerTouchOneByOne::create(); //设置吞噬为true,不让触摸往下传递 TouchListener->setSwallowtouches(true);//不向下传递触摸 // sjt TouchListener->onTouchBegan = CC_CALLBACK_2(GameObjHero::onTouchBegan,this); TouchListener->onTouchMoved = CC_CALLBACK_2(GameObjHero::onTouchMoved,this); TouchListener->onTouchEnded = CC_CALLBACK_2(GameObjHero::onTouchEnded,this); _eventdispatcher->addEventListenerWithSceneGraPHPriority(TouchListener,this);//将TouchListener和this绑定,放入事件委托中 CCSprite *mainsprite = CCSprite::create("catBody1.png"); //主体动画 CCAnimation *animation = CCAnimation::create(); animation->addSpriteFrameWithFileName("catBody1.png"); animation->addSpriteFrameWithFileName("catBody2-4.png"); animation->addSpriteFrameWithFileName("catBody3.png"); animation->addSpriteFrameWithFileName("catBody2-4.png"); animation->setDelayPerUnit(0.1f);//// 设置两帧之间的时间间隔 mainsprite->runAction(CCRepeatForever::create(CCAnimate::create(animation))); addChild(mainsprite); //尾巴 CCSprite *tail = CCSprite::create("catTail.png"); tail->setAnchorPoint(ccp(0.5,1)); tail->setPosition(ccp(-5,-29)); tail->setScale(0.5); tail->setRotation(20);// 角度 正数为顺时针旋转,负数为逆时针旋转。 // 执行需要一定的时间(或者说一个过程) tail->runAction(CCRepeatForever::create((CCActionInterval*)CCSequence::create( CCRotateBy::create(0.5,-40),CCRotateBy::create(0.5,40),NULL))); addChild(tail); //手 lefthand = CCSprite::create("catHand1.png"); lefthand->setAnchorPoint(ccp(1,0.5)); lefthand->setPosition(ccp(-18,-20)); addChild(lefthand); righthand = CCSprite::create("catHand2.png"); righthand->setPosition(ccp(18,-20)); righthand->setAnchorPoint(ccp(0,0.5)); addChild(righthand); iscontrol = false; offset = ccp(0,0); schedule(schedule_selector(GameObjHero::releasebullet),1.0f); // 每隔1秒就执行GameObjHero类的方法:releasebullet() } void GameObjHero::releasebullet(float dt) { //释放子弹 if (iscontrol) { CCPoint pos = this->getPosition(); GameMain *p = (GameMain*)this->getParent(); p->releaseheroBullet(pos.x,pos.y + 30);// sjt } } void GameObjHero::onExit() { CCDirector *pDirector = CCDirector::sharedDirector(); //关闭触屏监听 //pDirector->getTouchdispatcher()->removeDelegate(this); _eventdispatcher->removeAllEventListeners(); CCNode::onExit(); // Sprite::onExit(); } bool GameObjHero::containsTouchLocation(CCTouch* touch)//注册触摸 { // return CCRect::CCRectContainsPoint(rect(),convertTouchToNodeSpaceAR(touch)); // 将世界坐标中触摸点转换为模型坐标。AR表示相对于锚点。 return rect().containsPoint(convertTouchToNodeSpaceAR(touch)); return true; } bool GameObjHero::onTouchBegan(Touch *touch,Event *event) { if (((GameMain*)this->getParent())->isover) return false; if (!containsTouchLocation(touch)) return false; else { //获得触摸偏移位置 iscontrol = true; // CCPoint touchPoint = touch->locationInView(); CCPoint touchPoint = touch->getLocationInView();// 从触摸点获取到在屏幕坐标系中的坐标 touchPoint = CCDirector::sharedDirector()->convertToGL(touchPoint); offset.x = touchPoint.x - this->getPosition().x; offset.y = touchPoint.y - this->getPosition().y; // this->getPositon().x 获得CCNode的认坐标 } return true; } void GameObjHero::onTouchMoved(Touch *touch,Event *event) { if (iscontrol) { CCPoint touchPoint = touch->getLocationInView(); // 获取点在视图中的坐标(左上角为原点) touchPoint = CCDirector::sharedDirector()->convertToGL(touchPoint); // 把点的坐标转换成OpenGL坐标(左下角为原点) //设置左右手上下 float x = touchPoint.x - offset.x; float y = touchPoint.y - offset.y; if (x < this->getPosition().x) { lefthand->setFlipY(true);//CCNode 执行 setFlipY 纵向翻转节点(相当于CCFlipY) righthand->setFlipY(false); } else { lefthand->setFlipY(false); righthand->setFlipY(true); } this->setPosition(x,y); } } void GameObjHero::onTouchEnded(Touch *touch,Event *event) { if (iscontrol) { iscontrol = false; lefthand->setFlipY(false); righthand->setFlipY(false); } }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐