2016-09-08 10 views
3

現狀:設定值

  • 子彈從遊戲對象繼承。
  • 遊戲對象有方法 'GetCoords' 和 'SetCoords'
  • 子彈有方法 'MoveObject'
  • 'MoveObject' 從 'GetCoords' 得到COORDS,設定成 'SetCoords'
  • 座標不改變

GameObject.h

struct Coordinates { 
    float xPos; 
    float yPos; 
}; 

//GameObject 
class GameObject { 
    public: 
    GameObject() {}; 
    GameObject(unsigned int a_id, float a_xPos, float a_yPos, float a_speed, States a_state = State::Idle); 
    ~GameObject(); 

    //Methods 
    void MoveObject(float changeXPos, float changeYPos); 
    void MoveObject(float changeYPos); 

    Coordinates GetCoords() { return Coords; } 

    void SetCoords(Coordinates a_Coords) { Coords = a_Coords; } 

    private: 
     Coordinates Coords; 
}; 

Bullet.h

#pragma once 
#include "GameObject.h" 

class Bullet : public GameObject { 
public: 
    Bullet(unsigned int a_Id, float a_xPos, float a_yPos, float a_speed, States a_state = State::Idle) : GameObject(a_Id, a_xPos, a_yPos, a_speed, a_state) {} 

    void MoveObject(float changeYPos); 
}; 

Bullet.cpp

#include "Bullet.h" 

void Bullet::MoveObject(float changeYPos) 
{ 
    Coordinates coords = GameObject::GetCoords(); 
    coords.yPos += changeYPos; 
    this->SetCoords(coords); 
} 

我試圖 '這 - > SetCoords();'和'GameObject :: GetCoords();'無濟於事。

我只是嘗試這樣做:

void GameObject::MoveObject(float changeYPos) 
{ 
    Coordinates coords = GetCoords(); 
    coords.yPos += changeYPos; 
    SetCoords(coords); 
} 

主要遊戲類

其中MoveObject獲取調用點:

for each (auto bullet in bullets) 
{ 
    Coordinates coords = bullet.GetCoords(); 
    std::cout << bullet.GetCoords().xPos << ", " << bullet.GetCoords().yPos << std::endl; 
    bullet.MoveObject(.3f); 
    if (bullet.GetCoords().yPos > m_iScreenHeight) { 
     bullets.erase(bullets.begin()); 
     DestroySprite(bullet.GetId()); 
     break; 
    } 

    coords = bullet.GetCoords(); 
    std::cout << bullet.GetCoords().xPos << ", " << bullet.GetCoords().yPos << std::endl; 
    MoveSprite(bullet.GetId(), bullet.GetCoords().xPos, bullet.GetCoords().yPos); 

    CheckHitEnemy(bullet.GetCoords().xPos, bullet.GetCoords().yPos, bullet.GetId()); 
} 

第二COUT 確實有不同的座標。

當子彈獲得創建點,按下空格鍵時,將調用此方法:

Bullet bullet(CreateSprite("./images/bullet.jpg", 3, 20, true), xPos, yPos, 1); 

MoveSprite(bullet.GetId(), bullet.GetCoords().xPos, bullet.GetCoords().yPos); 

bullets.push_back(bullet); 
+0

我看不出明顯的原因,爲什麼只是'SetCoords(coords);'和'GetCoords();'不會工作,因爲它們都是公共成員的對象。 – George

+1

你可以縮小你的班級,只有所需的功能在那裏,看看這是否仍然如此?也只是一個一般的提示:寫你的變量小寫,而不是以大寫字母開頭。這使大多數人閱讀你的代碼時感到困惑。 – Hayt

+0

@Hayt完成了。謝謝你的提示。 –

回答

1

在我看來,你是在基類的MoveObject聲明之前缺少virtual關鍵字。

的方式,這是現在,當你有一個GameObject指針/引用,它會調用MoveObjectGameObject,而不是從Bullet

因此,也許它更改爲

virtual void MoveObject(float changeXPos, float changeYPos); 

我不確定它是否能解決您的問題,但後來您可能會遇到其他問題。如果你有C++ 11,你也應該看看override關鍵字

+0

我應該如何訪問'SetCoords'和'GetCoords'功能呢?使用'GameObject :: Function()'或'this-> Function()'? –

+0

它們應該可以通過'SetCoords'和'GetCoords'訪問。你應該聲明你想要在派生類中重寫的每個函數聲明'虛擬',儘管在你的基類中。 – Hayt

+0

但我不必重寫它們吧?它們在GameObject類中具有一個與Bullet類相同的主體。正如你所說的,我不能在沒有宣佈它們的情況下訪問它們。 –