cocos 2dx 3.12 学习笔记(六) DartGameDemo
来源:互联网 发布:华强北淘宝店 编辑:程序博客网 时间:2024/06/05 02:06
使用 计时器,根据Tag不同进行碰撞检测,做了一个投掷飞镖,攻击小鸟。
GameDefine.h
#ifndef __GAME_DEFINE_H__#define __GAME_DEFINE_H__//定义每一个层的static const int ZORDER_BG = 0; static const int ZORDER_PLAYER = 1;static const int ZORDER_ENERMY = 2;static const int ZORDER_DART = 5;//定义 enermy和dart的Tagstatic const int TAG_ENERMY = 10;static const int TAG_DART = 20;#endif
#ifndef __DART_GAME_LAYER_H__#define __DART_GAME_LAYER_H__#include "cocos2d.h"USING_NS_CC;class DartGameLayer : public cocos2d::Layer{public :static cocos2d::Scene * createScene();CREATE_FUNC(DartGameLayer);virtual bool init();virtual bool onTouchBegan(Touch * touch, Event *unused_event);virtual void onTouchMoved(Touch *touch, Event *unused_event);virtual void onTouchEnded(Touch *touch, Event *unused_event);cocos2d::Size getContentSizeByImageName(std::string imageName); //定义一个通过图片名字过去图片大小的方法。void scheduleCreateEnermy(float dt); //创建敌人计时器void scheduleCollisionCheck(float dt);//碰撞检测计时器private:cocos2d::Sprite * m_pPlayerSprite = nullptr; //m_pPlayerSprite 丢飞镖小人Sprite};#endif
#include "DartSprite.h"USING_NS_CC;DartSprite * DartSprite::create(const cocos2d::Rect & bounds, cocos2d::Node * pParent, int Zorder){DartSprite * pRet = new (std::nothrow)DartSprite;//当NEW不成功时,会返回一个空,而不是未命名的名称空间pRet->setAnchorPoint(Vec2(0.5f, 0.5f));//pisitionpRet->setPosition(bounds.origin);//sizepRet->setContentSize(bounds.size);if (pRet->init()){pParent->addChild(pRet, Zorder);pRet->autorelease();}else{//delete pRet;//pRet = nullptr;//C++代码,和宏的作用相同CC_SAFE_DELETE(pRet);}return pRet;}DartSprite::DartSprite(){}DartSprite::~DartSprite(){}bool DartSprite::init(){if (!Node::init()){return false;}auto size = this->getContentSize();float x = 0;float y = 0;x = size.width / 2;y = size.height / 2;m_pDisplay = Sprite::create("Projectile.png");this->addChild(m_pDisplay);m_pDisplay->setPosition(Vec2(x, y));return true;}void DartSprite::moveToPos(cocos2d::Vec2 pos){Vec2 pos_dar = this->getPosition();float dis = pos_dar.getDistance(pos);float time = dis / DART_SPEED;float dis_x = pos.x - pos_dar.x;float dis_y = pos.y - pos_dar.y;m_speedX = (dis_x / time) / 60.0f;m_speedY = (dis_y / time) / 60.0f;this->schedule(schedule_selector(DartSprite::scheduleUpdatePosition));}void DartSprite::scheduleUpdatePosition(float dt){float x = this->getPositionX() + m_speedX;float y = this->getPositionY() + m_speedY;this->setPosition(Vec2(x,y));auto screntSize = Director::getInstance()->getVisibleSize();if (x < 0 || x > screntSize.width || y < 0 || y>screntSize.height){unschedule(schedule_selector(DartSprite::scheduleUpdatePosition));this->removeFromParent();}}
DartSprite.h
#ifndef __DART_SPRITE_H__#define __DART_SPRITE_H__#include "cocos2d.h"USING_NS_CC;#define DART_SPEED 200.0fclass DartSprite : public cocos2d::Node{public:static DartSprite * create(const cocos2d::Rect & bounds, cocos2d::Node * pParent, int Zorder);//用const修饰 rect win32下无所谓,到移动端不用const修饰会报错。// Rect包含 Position size //传入父节点 和Zorder//在create中吧所有的东西做完。DartSprite();~DartSprite();virtual bool init();void moveToPos(cocos2d::Vec2 pos);void scheduleUpdatePosition(float dt);private://定义x,y轴方向的速度 float m_speedX = 0;float m_speedY = 0;cocos2d::Sprite * m_pDisplay = nullptr;};#endif
DartSprite.app
#include "DartSprite.h"USING_NS_CC;DartSprite * DartSprite::create(const cocos2d::Rect & bounds, cocos2d::Node * pParent, int Zorder){DartSprite * pRet = new (std::nothrow)DartSprite;//当NEW不成功时,会返回一个空,而不是未命名的名称空间pRet->setAnchorPoint(Vec2(0.5f, 0.5f));//pisitionpRet->setPosition(bounds.origin);//sizepRet->setContentSize(bounds.size);if (pRet->init()){pParent->addChild(pRet, Zorder);pRet->autorelease();}else{//delete pRet;//pRet = nullptr;//C++代码,和宏的作用相同CC_SAFE_DELETE(pRet);}return pRet;}DartSprite::DartSprite(){}DartSprite::~DartSprite(){}bool DartSprite::init(){if (!Node::init()){return false;}auto size = this->getContentSize();float x = 0;float y = 0;x = size.width / 2;y = size.height / 2;m_pDisplay = Sprite::create("Projectile.png");this->addChild(m_pDisplay);m_pDisplay->setPosition(Vec2(x, y));return true;}void DartSprite::moveToPos(cocos2d::Vec2 pos){Vec2 pos_dar = this->getPosition();float dis = pos_dar.getDistance(pos);float time = dis / DART_SPEED;float dis_x = pos.x - pos_dar.x;float dis_y = pos.y - pos_dar.y;m_speedX = (dis_x / time) / 60.0f;m_speedY = (dis_y / time) / 60.0f;this->schedule(schedule_selector(DartSprite::scheduleUpdatePosition));}void DartSprite::scheduleUpdatePosition(float dt){float x = this->getPositionX() + m_speedX;float y = this->getPositionY() + m_speedY;this->setPosition(Vec2(x,y));auto screntSize = Director::getInstance()->getVisibleSize();if (x < 0 || x > screntSize.width || y < 0 || y>screntSize.height){unschedule(schedule_selector(DartSprite::scheduleUpdatePosition));this->removeFromParent();}}
EnermySprite.h
#ifndef __ENERMY_SPRITE_H__#define __ENERMY_SPRITE_H__#include "cocos2d.h"#define ENERMY_SPEED 100.0f // 定义了第人的速度class EnermySprite : public cocos2d::Node{public:static EnermySprite * create(const cocos2d::Rect & bounds, cocos2d::Node * pParent, int zOrder); //重写了EnermySprite的create方法public:EnermySprite();~EnermySprite();virtual bool init();void move(); // enermy移动方法void scheduleUpdatePosition(float dt); //更新enermy计时器private:cocos2d::Sprite * m_pDisplay = nullptr; // m_pDdisplay 精灵float m_speedX = 0; //x轴速度};#endif
EnermySprite.app
#include "EnermySprite.h"USING_NS_CC;EnermySprite * EnermySprite::create(const cocos2d::Rect & bounds, cocos2d::Node * pParent, int zOrder){EnermySprite * pRet = new (std::nothrow)EnermySprite();pRet->setAnchorPoint(Vec2(0.5f, 0.5f));pRet->setContentSize(bounds.size);pRet->setPosition(bounds.origin);if (pRet->init()){pParent->addChild(pRet, zOrder);pRet->autorelease();}else{CC_SAFE_DELETE(pRet);}return pRet;}EnermySprite::EnermySprite(){}EnermySprite::~EnermySprite(){}bool EnermySprite::init(){Node::init();auto size = this->getContentSize();float x = 0;float y = 0;x = size.width / 2;y = size.height / 2;m_pDisplay = Sprite::create("bird.png");this->addChild(m_pDisplay);m_pDisplay->setPosition(Vec2(x, y));return true;}void EnermySprite::move(){m_speedX = ENERMY_SPEED / 60.0f;this->schedule(schedule_selector(EnermySprite::scheduleUpdatePosition));}void EnermySprite::scheduleUpdatePosition(float dt){float pos_x = this->getPositionX() - m_speedX;float pos_y = this->getPositionY();this->setPosition(Vec2(pos_x, pos_y));if (pos_x < 0){unschedule(schedule_selector(EnermySprite::scheduleUpdatePosition));//this->removeFromParentAndCleanup(true);this->removeFromParent();}}
0 0
- cocos 2dx 3.12 学习笔记(六) DartGameDemo
- cocos 2dx 3.12 学习笔记(一)
- cocos 2dx 3.12 学习笔记(二) Sprite
- cocos 2dx 3.12 学习笔记(三) TouchTest
- cocos 2dx 3.12 学习笔记(四) ButtonTest
- cocos 2dx 3.12 学习笔记(五) BallTest
- cocos 2dx 3.12 学习笔记(七) ActionTutorial
- cocos-2dx 渲染(2)
- cocos-2dx OPENGL渲染(1)
- cocos-2dx 渲染(3)
- cocos-2dx-getChildByTag
- cocos 2dx 更新函数
- cocos 2dx 更新函数
- Cocos-2dx 错误汇总
- 用COCOS-2DX做一个RPG游戏(一)
- 2dx学习笔记-----常用宏
- DX学习笔记 一
- DX学习笔记
- 基础总结
- Windows平台分布式架构实践 - 负载均衡(下)
- ARM的位置无关程序设计
- RecycleView和CardView的简单介绍和用法
- 遍历二叉树的全部方法(递归+非递归)
- cocos 2dx 3.12 学习笔记(六) DartGameDemo
- Java笔记 - 接口
- 数组
- QUTOJ 1218: 核电站问题 递推
- [滴滴]秋招笔试(2016年9月6号)
- Android 数据存储----SQLite
- PCB课程设计1(学习使用Altium Designer Release 10)
- Linux命令练习第三关(6)
- 运算符与优先级、数据类型转换、注释