現狀:設定值
- 子彈從遊戲對象繼承。
- 遊戲對象有方法 '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);
我看不出明顯的原因,爲什麼只是'SetCoords(coords);'和'GetCoords();'不會工作,因爲它們都是公共成員的對象。 – George
你可以縮小你的班級,只有所需的功能在那裏,看看這是否仍然如此?也只是一個一般的提示:寫你的變量小寫,而不是以大寫字母開頭。這使大多數人閱讀你的代碼時感到困惑。 – Hayt
@Hayt完成了。謝謝你的提示。 –