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

我理解的 cocos2d_x3.4 的单点触摸机制,第1张

概述因本人能力有限,请网友指针。不胜感激。自己简单修改的喵星战争的触摸机制源码 /* GameObjHero Class Definitions Created by taotao man on 2015-5-18 */ #ifndef GameObjHero_h #define GameObjHero_h #include "cocos2d.h" using namespace cocos2d; /

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

/*
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) { //释放子d 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); } }

总结

以上是内存溢出为你收集整理的我理解的 cocos2d_x3.4 的单点触摸机制全部内容,希望文章能够帮你解决我理解的 cocos2d_x3.4 的单点触摸机制所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1059653.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存