cocos2dx学习笔记-给精灵类添加事件
来源:互联网 发布:淘宝雪梨的质量 编辑:程序博客网 时间:2024/05/17 22:12
看了看cocos2dx测试项目TouchesTest代码学到的一点东西吧。
在最开始设计自己的精灵类的时候,只是单单继承Sprite,在类里面然后直接添加事件监听的回调方法。
class ObjSth : public cocos2d::Sprite{public: virtual void onEnter() override; virtual void onExit() override;public: //添加事件监听的三个回调成员函数 bool onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event); void onTouchMoved(cocos2d::Touch* touch, cocos2d::Event* event); void onTouchEnded(cocos2d::Touch* touch, cocos2d::Event* event);};
在OnEnter()方法里实现事件的初始化。
void ObjSth::onEnter(){ Sprite::onEnter(); // Register Touch Event auto listener = EventListenerTouchOneByOne::create(); listener->setSwallowTouches(true); listener->onTouchBegan = CC_CALLBACK_2(ObjSth::onTouchBegan, this); listener->onTouchMoved = CC_CALLBACK_2(ObjSth::onTouchMoved, this); listener->onTouchEnded = CC_CALLBACK_2(ObjSth::onTouchEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);}
比如在屏幕中添加一个精灵,添加事件实现拖动。
在游戏场景中这么写:
bool Game Scene::init(){ // 1. super init first if ( !Layer::init() ) { return false; } auto Obj = Sprite::create("001.png"); Obj->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y)); this->addChild(Ball, 0); return true;}
自己想当然写的,果然怎么运行也不成功。
设置断点压根不会运行到回调函数那里。
研究了下,做了如下改变。
类部分,添加了两个和初始化有关的方法:
class ObjSth : public cocos2d::Sprite{public: virtual void onEnter() override; virtual void onExit() override;public: //添加如下两个关于初始化成员函数 static ObjSth* createWithTexture(cocos2d::Texture2D* aTexture); //添加事件监听的三个回调成员函数 bool onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event); void onTouchMoved(cocos2d::Touch* touch, cocos2d::Event* event); void onTouchEnded(cocos2d::Touch* touch, cocos2d::Event* event);};
实现如下:
ObjSth* ObjSth::createWithTexture(Texture2D* aTexture){ ObjSth* pObjSth = new (std::nothrow) ObjSth(); pObjSth->initWithTexture(aTexture); pObjSth->autorelease(); return pObjSth;}
在游戏场景中这么写:
bool Game Scene::init(){ // 1. super init first if ( !Layer::init() ) { return false; } auto paddleTexture = Director::getInstance()->getTextureCache()->addImage("001.png"); auto Ball = ObjSth::createWithTexture(paddleTexture); Ball->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y)); this->addChild(Ball, 0); return true;}
终于运行成功。
阅读全文
0 0
- cocos2dx学习笔记-给精灵类添加事件
- cocos2dx 精灵点击事件
- 学习笔记--cocos2dx-3.0中LabelTTF添加触摸监听事件
- cocos2dx 精灵缓存学习
- cocos2dx 精灵监听触摸事件
- cocos2dx 处理精灵触摸事件
- Cocos2dx学习笔记(2)——精灵的创建
- (cocos2dx学习日记二)中文乱码。添加精灵。添加菜单。添加动画。
- Quick cocos2dx-Lua(V3.3R1)学习笔记(8) ---- 事件篇之单点触摸事件,让我们用精灵模仿一个按钮吧
- Cocos2dx 精灵类-Sprite
- 【COCOS2DX-LUA 学习】精灵创建
- Cocos2dx学习笔记(二)、点击事件
- 为精灵添加触摸事件
- Cocos2dx CrazyTetris 自定义精灵类 继承精灵
- cocos2d-x学习笔记【三】添加精灵及相关操作
- [windows+cocos2dx]CCSprite精灵类
- Cocos2dX精灵类的封装
- 【cocos2dx】cocos2dx学习笔记
- 《应用拆分与平台搭建最佳实践》- 拆分中的工作内容
- Json Web Token
- 证明接口中量和方法的默认访问修饰符及其信息
- leetcode485
- 树形动态规划 hdu1502 poj2342 Anniversary party
- cocos2dx学习笔记-给精灵类添加事件
- c语言实现静态顺序表
- Python3 输入和输出
- 命令模式
- Codeforces 题目合集+分类+蒟蒻的代码 【Updating...】【206 in total】
- JS 中关于this 执行环境的问题_part2
- 【python2.7初学日记】最初学习和编写的几个简单代码
- MySQL在单表上创建视图
- 《应用拆分与平台搭建最佳实践》- 拆分中两个核心问题