飞机大战开发01之添加主逻辑层,显示背景的移动
来源:互联网 发布:德勤财务咨询 知乎 编辑:程序博客网 时间:2024/05/29 12:29
GameLayer
负责游戏的主逻辑的实现:
代码:
GameLayer.h
#ifndef _GAME_LAYER_H_#define _GAME_LAYER_H_#include "cocos2d.h"USING_NS_CC ;class GameLayer : public Layer{public:static cocos2d::Scene* createScene();CREATE_FUNC(GameLayer);public:GameLayer();~GameLayer();virtual bool init();private:Sprite * m_pPlayerPlane ;Vec2 m_touchBeginPos;Vec2 m_touchMovePos ;};#endif
GameLayer.cpp
#include "GameLayer.h"cocos2d::Scene* GameLayer::createScene(){auto scene = Scene::create() ;auto layer = GameLayer::create();scene->addChild(layer);return scene;}GameLayer::GameLayer(){}GameLayer::~GameLayer(){}bool GameLayer::init(){Size screenSize = Director::getInstance()->getWinSize() ;return true;}
场景的移动:
雷电游戏的背景都是头尾相接的,所以,背景的移动动画只需要两张图就能搞定,当一张图移出屏幕的时候,设置它的坐标到屏幕的上方,如此循环就OK了。
代码:
BGLayer.h
#ifndef _BG_LAYER_H_#define _BG_LAYER_H_#include "cocos2d.h"USING_NS_CC ;class BGLayer : public Layer{public:CREATE_FUNC(BGLayer) ;public:BGLayer();~BGLayer();virtual bool init();void update(float dt);public:Sprite * m_pBG_1;Sprite * m_pBG_2;float m_speed ;};#endif
BGLayer.cpp
#include "BGLayer.h"BGLayer::BGLayer():m_pBG_1(nullptr),m_pBG_2(nullptr),m_speed(0){m_speed = 1.0f ;}BGLayer::~BGLayer(){}bool BGLayer::init(){float x = 0 ;float y = 0 ;m_pBG_1 = Sprite::create("img_bg_1.jpg") ;m_pBG_2 = Sprite::create("img_bg_1.jpg") ;x = m_pBG_1->getBoundingBox().size.width / 2 ;y = m_pBG_1->getBoundingBox().size.height / 2 ;m_pBG_1->setPosition(Vec2(x,y)) ;y = y + m_pBG_1->getBoundingBox().size.height ;m_pBG_2->setPosition(Vec2(x,y)) ;addChild(m_pBG_1) ;addChild(m_pBG_2) ;this->scheduleUpdate();return true ;}void BGLayer::update(float dt){float posY = m_pBG_1->getPositionY() - m_speed ;if (posY <= -m_pBG_1->getBoundingBox().size.height / 2){posY = Director::getInstance()->getWinSize().height + m_pBG_1->getBoundingBox().size.height / 2 ;}m_pBG_1->setPositionY(posY) ;posY = m_pBG_2->getPositionY() - m_speed ;if (posY <= -m_pBG_2->getBoundingBox().size.height / 2){posY = Director::getInstance()->getWinSize().height + m_pBG_2->getBoundingBox().size.height / 2 ;}m_pBG_2->setPositionY(posY) ;}
将场景添加到主逻辑层中
bool GameLayer::init(){addChild(BGLayer::create()) ;return true;}
0 0
- 飞机大战开发01之添加主逻辑层,显示背景的移动
- 基于cocos2dx的飞机大战学习[二]-添加移动背景与英雄
- 飞机大战开发02之添加玩家飞机
- 项目飞机大战01:主要实现背景的移动,飞机的动画和触控,子弹的生成和发射
- 飞机大战之四:飞机跟随触屏手指移动
- 飞机大战之三:添加子弹
- 飞机大战之五:添加一类敌机
- 飞机大战03之飞机发射子弹,添加敌人
- [cocos2d-x][游戏开发]通过cocos2d-x实现简易飞机大战 03.游戏界面 背景层实现滚动
- 基于cocos2dx的飞机大战学习[三]-为英雄添加飞行帧动作并控制飞机移动
- MFC飞机大战开发之绘制图像
- [cocos2d-x][游戏开发]通过cocos2d-x实现简易飞机大战 05.游戏界面 主角飞机的移动
- 飞机大战(下篇:逻辑)
- 微信经典飞机大战之三:飞机登场、帧动画、飞机层接口和属性
- 【cocos2d-x入门实战】微信飞机大战之六:子弹层的处理
- 微信经典飞机大战之四:子弹层的完全解析
- [cocos2d-x][游戏开发]通过cocos2d-x实现简易飞机大战 04.游戏界面 主角飞机的添加
- unity学习之飞机大战制作 关键点(三) 设置子弹的生成和移动
- 推荐一款适合程序猿使用的字体
- Android如何自定义视图属性总结
- ImageView的属性android:scaleType作用
- UIButton无响应问题详细记录
- iOS开发--毛玻璃效果,图片模糊效果
- 飞机大战开发01之添加主逻辑层,显示背景的移动
- 【OpenGL编程指南】之视图和模型变换
- [XML]sax中DefaultHander解析xml过程和先后顺序
- 2-5-2 对象作为数据成员
- 常用知识总结——模板Template
- 最近学习的感悟
- 深度学习介绍(六)趣闻
- Android 调用WebService 查手机号的应用
- UVA - 10635 Prince and Princess