正如木头所说的,这一章将之前学的知识都使用上了
创建主角精灵
1.将类分类存放,创建筛选器,有点像eclipse的包名,不过vs的是虚拟的,没有改变文件路径的。
2.创建实体基类,当游戏中存在多种实体时,我们可以创建一个基类
//头文件 #ifndef _ENTITY_H_ #define _ENTITY_H_ #include "cocos2d.h" USING_NS_CC; class Entity : public Node { public: Entity(); ~Entity(); Sprite* getSprite(); void bindSprite(Sprite* sprite); private: Sprite* m_sprite; }; #endif
#include "Entity.h" Entity::Entity() { m_sprite = NULL; } Entity::~Entity() { } Sprite* Entity::getSprite(){ return this->m_sprite; } void Entity::bindSprite(Sprite* sprite){ this->m_sprite = sprite; this->addChild(m_sprite); }//实现了精灵的绑定
3.创建主角类
主角类暂时没有做什么事情。
#ifndef _PLAYER_H_ #define _PLAYER_H_ #include "cocos2d.h" #include "Entity.h" USING_NS_CC; class Player : public Entity { public: Player(); ~Player(); CREATE_FUNC(Player); virtual bool init(); private: }; #endif
#include "Player.h" Player::Player() { } Player::~Player() { } bool Player::init(){ return true; }
4.创建游戏场景
#ifndef _TOLLGATE_SCENE_H_ #define _TOLLGATE_SCENE_H_ #include "cocos2d.h" #include "Player.h" USING_NS_CC; class TollgateScene : public Layer { public: static Scene* createScene();//创建场景 virtual bool init();//初始化 CREATE_FUNC(TollgateScene);// virtual void update(float delta);//定时的操作 private: void initBG();//初始化关卡背景 private: Sprite* m_bgSprite1;//背景精灵1 Sprite* m_bgSprite2;//背景精灵2 Player* m_player;//主角 }; #endif
#include "TollgateScene.h" Scene* TollgateScene::createScene(){ Scene* scene = Scene::create(); scene->addChild(TollgateScene::create()); return scene; } bool TollgateScene::init(){ if (!Layer::init()) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Sprite* titleSprite = Sprite::create("title.png"); titleSprite->setPosition(Point(visibleSize.width/2,visibleSize.height - 50)); this->addChild(titleSprite,2); m_player = Player::create();//创建主角 m_player->bindSprite(Sprite::create("sprite.png")); m_player->setPosition(Point(200,visibleSize.height/4)); this->addChild(m_player,3);//加入到最前面的 initBG();//初始化背景 this->scheduleUpdate();//注册定时器 return true; } void TollgateScene::initBG(){ Size visibileSize = Director::getInstance()->getVisibleSize(); m_bgSprite1 = Sprite::create("tollgateBG.jpg"); m_bgSprite1->setPosition(visibileSize.width/2,visibileSize.height/2); this->addChild(m_bgSprite1,0); m_bgSprite2 = Sprite::create("tollgateBG.jpg"); //将背景往右边平移屏幕个宽度,完全移出屏幕了 m_bgSprite2->setPosition(visibileSize.width+visibileSize.width/2,visibileSize.height/2); m_bgSprite2->setFlippedX(true);//水平翻转,是图片衔接看起来更平滑 this->addChild(m_bgSprite2,0); } //启动定时器,让背景图片定时刷新,即移动 void TollgateScene::update(float delta){ log("%f",delta); int pos1 = m_bgSprite1->getPositionX();//获取当前位置,理解为中心点的位置 int pos2 = m_bgSprite2->getPositionX(); int speed = 1;//速度为1 pos1 -= speed;//像左移动1个速度 pos2 -= speed; //背景图片的size Size mapSize = m_bgSprite1->getContentSize(); //当背景图片的中心点移动到-mapSize.width/2的位置时,说明背景图片完全移出了屏幕, //就再次把背景图片放到右边的屏幕外 if (pos1<=-mapSize.width/2) { pos1 = mapSize.width + mapSize.width/2; } if (pos2<=-mapSize.width/2) { pos2 = mapSize.width + mapSize.width/2; } //更新位置 m_bgSprite1->setPositionX(pos1); m_bgSprite2->setPositionX(pos2); }
5.修改屏幕大小
背景图片是800x500的规格,所以我们也将窗口设置成800x500的
glview->setFrameSize(800,500);
原文链接:https://www.f2er.com/cocos2dx/346594.html