cocos2d-x 自定义精灵类
来源:互联网 发布:网络直播视频 编辑:程序博客网 时间:2024/04/24 05:30
#ifndef _NEWSPRITE_H_#define _NEWSPRITE_H_#include "cocos2d.h"using namespace cocos2d;class MySprite : public CCSprite, public CCTargetedTouchDelegate{private:bool isContainsTouchPoint(CCTouch * touch);CCRect rect();CCPoint edge(CCTouch * touch);public:MySprite(void);~MySprite(void);static MySprite * createWithFileName(const char* name);void createAnimation();virtual void onEnter();virtual void onExit();virtual bool ccTouchBegan(CCTouch * touch, CCEvent * event);virtual void ccTouchMoved(CCTouch * touch, CCEvent * event);virtual void ccTouchEnded(CCTouch * touch, CCEvent * event);};#endif
#include "NewSprite.h"MySprite::MySprite(void){}MySprite::~MySprite(void){}MySprite * MySprite::createWithFileName(const char * name){MySprite * pobSprite = new MySprite();if(pobSprite && pobSprite->initWithFile(name)){pobSprite->autorelease();return pobSprite;}CC_SAFE_DELETE(pobSprite);return NULL;}void MySprite::createAnimation(){CCAnimation * anim =CCAnimation::create();char str[50] = {0};for(int i = 1; i < 9; i++){sprintf(str, "grossini_dance_%02d.png", i);anim->addSpriteFrameWithFileName(str);}anim->setDelayPerUnit(0.2f);anim->setRestoreOriginalFrame(true);this->runAction(CCAnimate::create(anim));}bool MySprite::isContainsTouchPoint(CCTouch * touch){CCPoint touchLocation = touch->getLocationInView();touchLocation = CCDirector::sharedDirector()->convertToGL(touchLocation);if(rect().containsPoint(touchLocation))return true;elsefalse;}CCRect MySprite::rect(){CCPoint thisPoint = this->getPosition();CCSize size = this->getContentSize();float x = thisPoint.x - size.width/2;float y = thisPoint.y - size.height/2;return CCRectMake(x, y, size.width, size.height);}CCPoint MySprite::edge(CCTouch * touch){CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();CCSize thisSize = this->getContentSize();CCPoint touchLocation = touch->getLocationInView();touchLocation = CCDirector::sharedDirector()->convertToGL(touchLocation);if(touchLocation.x < origin.x + thisSize.width/2)touchLocation.x = origin.x + thisSize.width/2;if(touchLocation.x > origin.x + visibleSize.width - thisSize.width/2)touchLocation.x = origin.x + visibleSize.width - thisSize.width/2;if(touchLocation.y < origin.y + thisSize.height/2)touchLocation.y = origin.y + thisSize.height/2;if(touchLocation.y > origin.y + visibleSize.height - thisSize.height/2)touchLocation.y = origin.y + visibleSize.height - thisSize.height/2;return touchLocation;}void MySprite::onEnter(){CCSprite::onEnter();CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);}void MySprite::onExit(){CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);CCSprite::onExit();}bool MySprite::ccTouchBegan(CCTouch * touch, CCEvent * event){if(isContainsTouchPoint(touch) && this->numberOfRunningActions() == 0){createAnimation();return true;}else return false;}void MySprite::ccTouchMoved(CCTouch * touch, CCEvent * event){if(isContainsTouchPoint(touch))this->setPosition(edge(touch));}void MySprite::ccTouchEnded(CCTouch * touch, CCEvent * event){}
0 0
- cocos2d-x 自定义精灵类
- cocos2d-x 自定义精灵类
- cocos2d-x(精灵类)
- Cocos2d-x 自定义可接收处理触摸消息精灵类
- cocos2d-x精灵自定义路径移动
- cocos2d-x中的精灵类
- 【Cocos2d-x】Sprite精灵类-创建Sprite精灵对象
- cocos2d-x学习日志(2) --精灵类
- Cocos2d-x(七)精灵类CCSprite
- cocos2d-x初探学习笔记--精灵类
- Cocos2d-x Lua中Sprite精灵类
- cocos2d-x创建精灵
- Cocos2d-x精灵创建
- Cocos2d-x 精灵贴图
- Cocos2d-x 精灵移动
- cocos2d-x创建精灵
- cocos2d-x之精灵
- cocos2d-x中的精灵
- PHPCMS安装流程图
- IE下Css圆角问题
- UILocalizedIndexedCollation 使用
- HTML中忽略的小问题
- 欧拉计划 第6题
- cocos2d-x 自定义精灵类
- PAT 1073
- 遠端存取SQL 2008 R2 Express方法
- 【linux学习笔记】sort|uniq | cut | wc (我的第一篇markdown笔记)
- 逐梦之路充满艰辛
- Nginx初识
- 常用正则表达式
- Dialect class not found: org.hibernate.dialect.Oracle10gDialect 报错解决
- 黑马程序员_Java基础总结