设为首页 加入收藏

TOP

仿《雷霆战机》飞行射击手游开发--GameObject(一)
2017-10-12 18:14:47 】 浏览:8051
Tags:《雷霆战机》 飞行 射击 开发 --GameObject

转载请注明:http://www.cnblogs.com/thorqq/p/5646509.html 

项目首页:https://www.oschina.net/p/raiden

 

   在上一篇中,我们介绍了各种游戏对象的功能及类的集成关系,现在我们来看看GameObject的源代码

碰撞体

GameObject.h

 1 class GameObject : public Sprite
 2 {
 3 public:
 4     GameObject();
 5 
 6     virtual void setBodySize(const Size& s);
 7     virtual void setBodySize(float w, float h);
 8     virtual const Size& getBodySize();
 9     virtual const Size& getOrignBodySize() const;
10 
11     virtual void setBodyCenter(const Vec2& v);
12     virtual void setBodyCenter(float x, float y);
13     virtual const Vec2& getBodyCenter() const;
14 
15     //获取世界坐标下的body的位置和大小
16     virtual Rect getBodyBox() const;
17 
18 
19 protected:
20     //用于碰撞检测的刚体大小和位置
21     Vec2 m_bodyCenter; //刚体的中心点坐标(相对于精灵锚点的坐标)
22     Size m_bodySize;   //刚体的宽高
23 };

GameObject.cpp

 1 void GameObject::setBodySize(const Size& s)
 2 {
 3     m_bodySize = s;
 4 }
 5 
 6 void GameObject::setBodySize(float w, float h)
 7 {
 8     setBodySize(Size(w, h));
 9 }
10 
11 const Size& GameObject::getBodySize()
12 {
13     return m_bodySize;
14 }
15 
16 void GameObject::setBodyCenter(const Vec2& v)
17 {
18     m_bodyCenter = v;
19 }
20 
21 void GameObject::setBodyCenter(float x, float y)
22 {
23     m_bodyCenter = Vec2(x, y);
24 }
25 
26 const Vec2& GameObject::getBodyCenter() const
27 {
28     return m_bodyCenter;
29 }
30 
31 //获取世界坐标下的body的位置和大小
32 Rect GameObject::getBodyBox() const
33 {
34     Vec2 pos = getPosition();
35 
36     return Rect(pos.x + m_bodyCenter.x - m_bodySize.width * getAnchorPoint().x,
37         pos.y + m_bodyCenter.y - m_bodySize.height * getAnchorPoint().y,
38         m_bodySize.width,
39         m_bodySize.height);
40 }

碰撞体的定义很简单,中心坐标+宽高,然后加上常见的get/set方法。其中比较有用的是getBodyBox()方法。由于碰撞体的中心坐标是相对于Sprite锚点的坐标,所以如果要用来判断两个碰撞体是否发生碰撞(是否有重叠区域),必须要获取两个碰撞体在世界坐标下的位置和大小,这时就要调用getBodyBox()方法来得到Rect对象,然后再调用Rect的bool intersectsRect(const Rect& rect)方法来判断两个碰撞体是否发生了碰撞。

 暂停/恢复

GameObject.h

1     virtual void pause() override;
2     virtual void resume() override;
3     void pause(Node *pNode);
4     void resume(Node *pNode);

GameObject.cpp

 1 void GameObject::pause()
 2 {
 3     this->pause(this);
 4 }
 5 
 6 void GameObject::resume()
 7 {
 8     this->resume(this);
 9 }
10 
11 void GameObject::pause(Node *pNode)
12 {
13     Node::pause();
14 
15     for (auto p : pNode->getChildren())
16     {
17         p->pause();
18     }
19 }
20 
21 void GameObject::resume(Node *pNode)
22 {
23     Node::resume();
24 
25     for (auto p : pNode->getChildren())
26     {
27         p->resume();
28     }
29 }

调用pause和resume的同时,会调用所有子节点的pause和resume。这样,当玩家飞机暂停时,僚机作为它的子节点,也会跟着暂停。

初始化 

先看代码

 1 bool GameObject::initSpriteWithFileList(const std::vector<std::string>& fileList, float dura)
 2 {
 3     SpriteFrame *frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(fileList.at(0));
 4     if (NULL == frame)
 5     {
 6         DEBUG_LOG("Error get frame of '%s'", fileList.at(0).c_str());
 7         CCASSERT(frame, "Error get frame");
 8     }
 9     Sprite::initWithSpriteFrame(frame);
10 
11     //动画
12     if (fileList.size() > 1)
13     {
14         Animation* animation = Animation::create();
15         animation->setDelayPerUnit(dura);
16         for (unsigned i = 0; i < fileList.size(); i++)
17         {
18             SpriteFrame* pFrame = CCSpriteFrameCache::get
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇习题:codevs 1035 火车停留解题.. 下一篇比较X与Y的大小,绝对精准!!!..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目