Learning Cocos2d-x for WP8(9)——Sprite到哪,我做主

时间:2022-08-28 19:23:48

原文:Learning Cocos2d-x for WP8(9)——Sprite到哪,我做主

工程文件TouchesTest.h和TouchesTest.cpp

相关素材文件

Learning Cocos2d-x for WP8(9)——Sprite到哪,我做主Learning Cocos2d-x for WP8(9)——Sprite到哪,我做主

事件驱动同样适用于cocos2d-x引擎,cocos2d-x的触屏事件可分为单点和多点触屏。

一般用到情况有:

Layer统一接受触屏消息,然后由程序根据需要分发给不同位置的sprite;

自定义可接收触屏事件的sprite。

Layer层实现触屏事件

1.开启触屏事件

在Layer层初始化的时候设置

setIsTouchEnabled( true );

2.重写(覆盖)父类CCLayer的方法

以下为CCLayer类的CCCtouch相关虚方法

    // default implements are used to call script callback if exist
virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent); // default implements are used to call script callback if exist
virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent);
virtual void ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent);
virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent);
virtual void ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent);

Begin:触屏事件开始

Ended:触屏事件结束

Moved:触屏拖动

根据具体情况,改写自己需要的触屏事件方法。

选择其中的ccTouchesEnded方法,目的实现Sprite射击开火。

 void TouchesLayer::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)
{
CCSetIterator it;
CCTouch* touch;
for( it = pTouches->begin(); it != pTouches->end(); it++)
{
touch = (CCTouch*)(*it);
if(!touch)
break;
CCPoint location = touch->locationInView();
location = CCDirector::sharedDirector()->convertToGL(location); //SGQSpriteBatchRepeatAnimation(role2,"Role2Run%d.png",10,0.2f,1);
//CCMoveTo* role2Move=CCMoveTo::actionWithDuration(2,location);
//role2->runAction(role2Move); CCSprite* Role2FireArrow=CCSprite::spriteWithSpriteFrameName("FireArrow0.png");
Role2FireArrow->setPosition(role2->getPosition());
Role2FireArrowBatch= CCSpriteBatchNode::batchNodeWithFile("Sprite/Role2/Role2FireArrow.png");//与CCSpriteFrameCache同一纹理
Role2FireArrowBatch->addChild(Role2FireArrow);
this->addChild(Role2FireArrowBatch,); Role2Fire(Role2FireArrow,location);
}
}

其中包含方法Role2Fire,实现动画效果

Role2Fire
 void TouchesLayer::Role2Fire(CCSprite* pSprite,CCPoint touchLocation)
{
//创建逐帧数组
CCMutableArray<CCSpriteFrame*>* animFrames1=new CCMutableArray<CCSpriteFrame*>();
char str1[]={};
for(int i=;i<;i++)
{
sprintf(str1,"FireArrow%d.png",i);
CCSpriteFrame* pFrame=cache->spriteFrameByName( str1 );
animFrames1->addObject(pFrame);
}
CCMutableArray<CCSpriteFrame*>* animFrames2=new CCMutableArray<CCSpriteFrame*>();
for(int i=;i<;i++)
{
sprintf(str1,"FireArrowExplode%d.png",i);
CCSpriteFrame* pFrame=cache->spriteFrameByName( str1 );
animFrames2->addObject(pFrame);
}
CCAnimation* animation1=CCAnimation::animationWithFrames(animFrames1,0.25f);
CCAnimation* animation2=CCAnimation::animationWithFrames(animFrames2,0.1f);
CCMoveTo* Role2FireArrowMoveTo=CCMoveTo::actionWithDuration(,touchLocation);
CCCallFuncN* role3FunN=CCCallFuncN::actionWithTarget(this,callfuncN_selector(TouchesLayer::removeSprite)); //float offY=touchLocation.y-this->getPosition().y;
//float offX=touchLocation.x-this->getPosition().x;
//float angleRadians=atan2f(offY,offX);
//float angleDegrees=CC_RADIANS_TO_DEGREES(angleRadians);
//float cocosAngle=-1*angleDegrees;
//pSprite->setRotation(cocosAngle); pSprite->runAction(CCAnimate::actionWithAnimation(animation1,false));
pSprite->runAction(CCSequence::actions(Role2FireArrowMoveTo,CCAnimate::actionWithAnimation(animation2,false),role3FunN,NULL));
animFrames1->release();
}

注意

坐标系转换,设备中是以左上角为坐标系原点,cococs2d-x以左下角为坐标系原点,所以,在获取坐标点后,需要转换坐标系。

        CCPoint location = touch->locationInView();
location = CCDirector::sharedDirector()->convertToGL(location);

运行后,触摸屏幕后,Sprite会向着该触点发射开火。

Learning Cocos2d-x for WP8(9)——Sprite到哪,我做主Learning Cocos2d-x for WP8(9)——Sprite到哪,我做主

Sprite自定义触屏事件

同样,在CCLayer上实现Touch的效果,使用Sprite自定义触屏事件也可。

1. 创建一个类继承CCSprite和Touch相关接口

要使sprite实现自定义touch必须继承相关的touch接口。

Learning Cocos2d-x for WP8(9)——Sprite到哪,我做主

CCTouchDelegate下包含CCTargetedTouchDelegate和CCStandardTouchDelegate委托

2.CCTouch方法中处理事件函数

 void TouchesSprite::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)
{
CCSetIterator it;
CCTouch* touch;
for( it = pTouches->begin(); it != pTouches->end(); it++)
{
touch = (CCTouch*)(*it);
if(!touch)
break;
CCPoint location = touch->locationInView();
location = CCDirector::sharedDirector()->convertToGL(location); TouchesRole2Running(this);
CCMoveTo* moveTo=CCMoveTo::actionWithDuration(,location);
runAction(moveTo);
}
}

其中,TouchesRole2Running实现动画,触摸屏幕后,Sprite会跑动到触点位置。

 void TouchesSprite::TouchesRole2Running(CCSprite* pSprite)
{
//创建逐帧数组
CCMutableArray<CCSpriteFrame*>* animFrames1=new CCMutableArray<CCSpriteFrame*>();
char str1[]={};
for(int i=;i<;i++)
{
sprintf(str1,"Role2Run%d.png",i);
CCSpriteFrame* pFrame=TouchesRole2Cache->spriteFrameByName( str1 );
animFrames1->addObject(pFrame);
}
CCAnimation* animation1=CCAnimation::animationWithFrames(animFrames1,0.2f);
pSprite->runAction(CCAnimate::actionWithAnimation(animation1,false));
animFrames1->release();
}

运行后,触摸屏幕后,Sprite会跑动到触点位置。

Learning Cocos2d-x for WP8(9)——Sprite到哪,我做主Learning Cocos2d-x for WP8(9)——Sprite到哪,我做主

Lyaer层和Sprite层实现触摸屏事件,各有各的优点,都可实现触摸效果。

完整代码

TouchesTest.h
 #ifndef _TOUCHES_TEST_
#define _TOUCHES_TEST_ #include "cocos2d.h" using namespace cocos2d; //-------------------------------
//
//TouchesScene
//
//-------------------------------
class TouchesScene:public CCScene
{
public:
TouchesScene();
~TouchesScene(); virtual void onEnter();
}; //----------------------------
//
//TouchesSprite
//
//----------------------------
class TouchesSprite:public CCSprite,public CCStandardTouchDelegate
{
public:
TouchesSprite();
~TouchesSprite(); static TouchesSprite* SGQSpriteWithSpriteFrameName(const char* spriteFrameName); public:
virtual void onEnter();
//virtual void ccTouchEnded(CCTouch* touch, CCEvent* event);//ccTouchEnded无效,触摸无反应
virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent); CCSpriteFrameCache* TouchesRole2Cache;
void TouchesRole2Running(CCSprite* pSprite);
}; //------------------------------
//
//TouchesLayer
//
//------------------------------
class TouchesLayer:public CCLayer
{
public:
TouchesLayer();
~TouchesLayer(); //ccTouchEnded出现BUG,触摸无反应
//virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent); virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent); void Role2Standing(CCSprite* pSprite);
void Role2Fire(CCSprite* pSprite,CCPoint touchLocation);
void TouchesRole2Standing(CCSprite* pSprite); void removeSprite(CCNode* pSender);
private:
CCSprite* role2;
TouchesSprite* touchesRole2;
CCSpriteFrameCache* cache;
CCSpriteBatchNode* Role2FireArrowBatch;
}; #endif
TouchesTest.cpp
 #include "pch.h"
#include "Classes\TouchesTest.h" //------------------------------------
//
//TouchesLayer
//
//------------------------------------
TouchesLayer::TouchesLayer()
{
setIsTouchEnabled( true );//开启触屏事件
CCSize s=CCDirector::sharedDirector()->getWinSize();
//Layer层Touches
cache=CCSpriteFrameCache::sharedSpriteFrameCache();
cache->addSpriteFramesWithFile("Sprite/Role2/Role2.plist","Sprite/Role2/Role2.png");
cache->addSpriteFramesWithFile("Sprite/Role2/Role2FireArrow.plist","Sprite/Role2/Role2FireArrow.png"); role2=CCSprite::spriteWithSpriteFrameName("Role2Stand0.png");
role2->setPosition(CCPointMake(s.width/,s.height/)); CCSpriteBatchNode* spritebatch1 = CCSpriteBatchNode::batchNodeWithFile("Sprite/Role2/Role2.png");//与CCSpriteFrameCache同一纹理
spritebatch1->addChild(role2);
this->addChild(spritebatch1,); Role2Standing(role2); //Sprite层Touches
touchesRole2=TouchesSprite::SGQSpriteWithSpriteFrameName("Role2Stand0.png");
touchesRole2->setPosition(CCPointMake(,s.height-));
CCSpriteBatchNode* spritebatch2 = CCSpriteBatchNode::batchNodeWithFile("Sprite/Role2/Role2.png");//与CCSpriteFrameCache同一纹理
spritebatch2->addChild(touchesRole2);
this->addChild(spritebatch2,); TouchesRole2Standing(touchesRole2);
} TouchesLayer::~TouchesLayer()
{} void TouchesLayer::Role2Standing(CCSprite* pSprite)
{
//创建逐帧数组
CCMutableArray<CCSpriteFrame*>* animFrames1=new CCMutableArray<CCSpriteFrame*>();
char str1[]={};
for(int i=;i<;i++)
{
sprintf(str1,"Role2Stand%d.png",i);
CCSpriteFrame* pFrame=cache->spriteFrameByName( str1 );
animFrames1->addObject(pFrame);
}
CCAnimation* animation1=CCAnimation::animationWithFrames(animFrames1,0.2f);
pSprite->runAction(CCRepeatForever::actionWithAction(CCAnimate::actionWithAnimation(animation1,false)));
animFrames1->release();
} void TouchesLayer::TouchesRole2Standing(CCSprite* pSprite)
{
//创建逐帧数组
CCMutableArray<CCSpriteFrame*>* animFrames1=new CCMutableArray<CCSpriteFrame*>();
char str1[]={};
for(int i=;i<;i++)
{
sprintf(str1,"Role2Stand%d.png",i);
CCSpriteFrame* pFrame=cache->spriteFrameByName( str1 );
animFrames1->addObject(pFrame);
}
CCAnimation* animation1=CCAnimation::animationWithFrames(animFrames1,0.2f);
pSprite->runAction(CCRepeatForever::actionWithAction(CCAnimate::actionWithAnimation(animation1,false)));
animFrames1->release();
} /*
ccTouchEnded出现BUG,触摸无反应
*/
//void TouchesLayer::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
//{
// CCPoint location=pTouch->locationInView();
// location=CCDirector::sharedDirector()->convertToGL(location);
//
// CCMoveTo* moveTo=CCMoveTo::actionWithDuration(3,location);
// role->runAction(moveTo);
//} void TouchesLayer::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)
{
CCSetIterator it;
CCTouch* touch;
for( it = pTouches->begin(); it != pTouches->end(); it++)
{
touch = (CCTouch*)(*it);
if(!touch)
break;
CCPoint location = touch->locationInView();
location = CCDirector::sharedDirector()->convertToGL(location); //SGQSpriteBatchRepeatAnimation(role2,"Role2Run%d.png",10,0.2f,1);
//CCMoveTo* role2Move=CCMoveTo::actionWithDuration(2,location);
//role2->runAction(role2Move); CCSprite* Role2FireArrow=CCSprite::spriteWithSpriteFrameName("FireArrow0.png");
Role2FireArrow->setPosition(role2->getPosition());
Role2FireArrowBatch= CCSpriteBatchNode::batchNodeWithFile("Sprite/Role2/Role2FireArrow.png");//与CCSpriteFrameCache同一纹理
Role2FireArrowBatch->addChild(Role2FireArrow);
this->addChild(Role2FireArrowBatch,); Role2Fire(Role2FireArrow,location);
}
} void TouchesLayer::Role2Fire(CCSprite* pSprite,CCPoint touchLocation)
{
//创建逐帧数组
CCMutableArray<CCSpriteFrame*>* animFrames1=new CCMutableArray<CCSpriteFrame*>();
char str1[]={};
for(int i=;i<;i++)
{
sprintf(str1,"FireArrow%d.png",i);
CCSpriteFrame* pFrame=cache->spriteFrameByName( str1 );
animFrames1->addObject(pFrame);
}
CCMutableArray<CCSpriteFrame*>* animFrames2=new CCMutableArray<CCSpriteFrame*>();
for(int i=;i<;i++)
{
sprintf(str1,"FireArrowExplode%d.png",i);
CCSpriteFrame* pFrame=cache->spriteFrameByName( str1 );
animFrames2->addObject(pFrame);
}
CCAnimation* animation1=CCAnimation::animationWithFrames(animFrames1,0.25f);
CCAnimation* animation2=CCAnimation::animationWithFrames(animFrames2,0.1f);
CCMoveTo* Role2FireArrowMoveTo=CCMoveTo::actionWithDuration(,touchLocation);
CCCallFuncN* role3FunN=CCCallFuncN::actionWithTarget(this,callfuncN_selector(TouchesLayer::removeSprite)); //float offY=touchLocation.y-this->getPosition().y;
//float offX=touchLocation.x-this->getPosition().x;
//float angleRadians=atan2f(offY,offX);
//float angleDegrees=CC_RADIANS_TO_DEGREES(angleRadians);
//float cocosAngle=-1*angleDegrees;
//pSprite->setRotation(cocosAngle); pSprite->runAction(CCAnimate::actionWithAnimation(animation1,false));
pSprite->runAction(CCSequence::actions(Role2FireArrowMoveTo,CCAnimate::actionWithAnimation(animation2,false),role3FunN,NULL));
animFrames1->release();
} void TouchesLayer::removeSprite(CCNode* pSender)
{
this->removeChild(Role2FireArrowBatch,true);
} //------------------------------------
//
//TouchesSprite
//
//------------------------------------ TouchesSprite::TouchesSprite()
{
TouchesRole2Cache=CCSpriteFrameCache::sharedSpriteFrameCache();
TouchesRole2Cache->addSpriteFramesWithFile("Sprite/Role2/Role2.plist","Sprite/Role2/Role2.png");
} TouchesSprite::~TouchesSprite()
{} TouchesSprite* TouchesSprite::SGQSpriteWithSpriteFrameName(const char* spriteFrameName)
{
TouchesSprite* pSprite=new TouchesSprite();
pSprite->initWithSpriteFrameName(spriteFrameName);
return pSprite;
} void TouchesSprite::onEnter()
{
//CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0, true);//ccTouchEnded无效
CCTouchDispatcher::sharedDispatcher()->addStandardDelegate(this,);
CCSprite::onEnter();
} //ccTouchEnded无效
//void TouchesSprite::ccTouchEnded(CCTouch* touch, CCEvent* event)
//{
// CCPoint location=touch->locationInView();
// location=CCDirector::sharedDirector()->convertToGL(location);
//
// CCMoveTo* moveTo=CCMoveTo::actionWithDuration(3,location);
// runAction(moveTo);
//} void TouchesSprite::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)
{
CCSetIterator it;
CCTouch* touch;
for( it = pTouches->begin(); it != pTouches->end(); it++)
{
touch = (CCTouch*)(*it);
if(!touch)
break;
CCPoint location = touch->locationInView();
location = CCDirector::sharedDirector()->convertToGL(location); TouchesRole2Running(this);
CCMoveTo* moveTo=CCMoveTo::actionWithDuration(,location);
runAction(moveTo);
}
} void TouchesSprite::TouchesRole2Running(CCSprite* pSprite)
{
//创建逐帧数组
CCMutableArray<CCSpriteFrame*>* animFrames1=new CCMutableArray<CCSpriteFrame*>();
char str1[]={};
for(int i=;i<;i++)
{
sprintf(str1,"Role2Run%d.png",i);
CCSpriteFrame* pFrame=TouchesRole2Cache->spriteFrameByName( str1 );
animFrames1->addObject(pFrame);
}
CCAnimation* animation1=CCAnimation::animationWithFrames(animFrames1,0.2f);
pSprite->runAction(CCAnimate::actionWithAnimation(animation1,false));
animFrames1->release();
} //------------------------------------
//
//TouchesScene
//
//------------------------------------
TouchesScene::TouchesScene()
{} TouchesScene::~TouchesScene()
{} void TouchesScene::onEnter()
{
CCScene::onEnter();
CCLayer* touchesLayer=new TouchesLayer();
addChild(touchesLayer);
touchesLayer->release();
}

著作权声明:本文由http://www.cnblogs.com/suguoqiang 原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢!

Learning Cocos2d-x for WP8(9)——Sprite到哪,我做主的更多相关文章

  1. Cocos2d 3&period;0继承自Sprite的类在addChild后出现故障

    当继承自Sprite的类被addChild到其它的Node里后出现例如以下图问题,说明没有调用父类Sprite::init()的方法.由于父类Sprite里的_textureAtlas须要初始化为nu ...

  2. Cocos2d-iphone 为sprite添加双击的事件响应

    这篇文章介绍两种方式处理cocos2d中的双击事件响应. 在iOS中使用UITapGestureRecognizer ,很容易就可以添加双击事件处理,但是在cocos2d中无法直接向sprite添加U ...

  3. Cocos2d-x游戏移植到WP8之路 -- c&plus;&plus;和c&num;交互

    Cocos2d-x是眼下最流行的手机游戏引擎之中的一个,开源.轻量.多平台等的诸多特性使得它被非常多国内外手游开发人员所喜爱. 利用Cocos2d-x来开发Windows Phone 8的游戏相同也是 ...

  4. (转) &lbrack;it-ebooks&rsqb;电子书列表

    [it-ebooks]电子书列表   [2014]: Learning Objective-C by Developing iPhone Games || Leverage Xcode and Obj ...

  5. 25 个增强iOS应用程序性能的提示和技巧 应用程序性能的提示和技巧

    初级 在开发过程中,下面这些初级技巧需要时刻注意: 1.使用ARC进行内存管理2.在适当的情况下使用reuseIdentifier3.尽可能将View设置为不透明(Opaque)4.避免臃肿的XIBs ...

  6. 增强iOS应用程序性能的提示和技巧&lpar;25个&rpar;

    转自 http://www.cocoachina.com/newbie/basic/2013/0522/6259.html 在开发iOS应用程序时,让程序具有良好的性能是非常关键的.这也是用户所期望的 ...

  7. 非常优秀的iphone学习文章总结!

    This site contains a ton of fun tutorials – so many that they were becoming hard to find! So I put t ...

  8. iPhone Tutorials

    http://www.raywenderlich.com/tutorials This site contains a ton of fun written tutorials – so many t ...

  9. 25个增强iOS应用程序性能的提示和技巧&lpar;中级篇&rpar;&lpar;2&rpar;

    25个增强iOS应用程序性能的提示和技巧(中级篇)(2) 2013-04-16 14:42 破船之家 beyondvincent 字号:T | T 本文收集了25个关于可以提升程序性能的提示和技巧,分 ...

  10. 25个增强iOS应用程序性能的提示和技巧--中级篇

    25个增强iOS应用程序性能的提示和技巧--中级篇 标签: ios性能优化内存管理 2013-12-13 10:55 738人阅读 评论(0) 收藏 举报  分类: IPhone开发高级系列(34)  ...

随机推荐

  1. 比较字符串&comma;equals防空指针问题

    1,比较两个字符串内容的话,用a.equals(b)比较,其中a,b是两个字符串,用a==b的话比较的是a和b的内存地址.2,如果一个字符串是变量,另一个字符串是常量的话,一定要把常量写在前面,变量写 ...

  2. php执行shell更新svn文件的方法

    vim /etc/sudoers 修改内容如下: #Defaults !visiblepw Defaults visiblepw #Defaults requiretty <?php set_t ...

  3. Hive与Hbase整合

    Hive与Hbase整合 1.文档 Hive HBase Integration 2.拷贝jar文件 2.1.把Hbase的lib目录下面的jar文件全部拷贝到Hive的lib目录下面 cd /hom ...

  4. springboot整合mybatis(使用MyBatis Generator&rpar;

    引入依赖 <dependencies> <dependency> <groupId>org.springframework.boot</groupId> ...

  5. 分页插件通用处理,以asp&period;net mvc为例

    Model: public class PaggerModel { public PaggerModel() { BarSize = ; } public PaggerModel(int total, ...

  6. Server&period;Transfer VS Response&period;Redirect – Simplified

    https://www.codeproject.com/Articles/775221/Server-Transfer-VS-Response-Redirect-Simplified Introduc ...

  7. 【Python爬虫实战】 图片爬虫-淘宝图片爬虫--千图网图片爬虫

    所谓图片爬虫,就是从互联网中自动把对方服务器上的图片爬下来的爬虫程序.有些图片是直接在html文件里面,有些是隐藏在JS文件中,在html文件中只需要我们分析源码就能得到如果是隐藏在JS文件中,那么就 ...

  8. Jsonpath的基本使用

    JSONPath - 是xpath在json的应用. xml最大的优点就有大量的工具可以分析,转换,和选择性的提取文档中的数据.XPath是这些最强大的工具之一.   如果可以使用xpath来解析js ...

  9. Java常见的乱码解决方式

    JAVA几种常见的编码格式(转)   简介 编码问题一直困扰着开发人员,尤其在 Java 中更加明显,因为 Java 是跨平台语言,不同平台之间编码之间的切换较多.本文将向你详细介绍 Java 中编码 ...

  10. RGB&lpar;16进制&rpar;&lowbar;转&lowbar;TColor

    ZC:内存中 COLORREF就是一个DWORD(从定义"COLORREF = DWORD;"就可以看出来),但是 具体的byte R/G/B 的位置是怎么方式的? ZC:Wind ...