幫助我瞭解如何使用超變量的子類,並能夠進行更改,我使用SDL庫C++繼承子類修改變量
首先我有一個製作克隆太空入侵者2D遊戲是從SDL_Rect繼承這樣看我會留下不重要的部分出
//Rectangle.h
namespace galaxy{
struct Rectangle : public SDL_Rect{
Rectangle();
Rectangle(int xx, int yy, int hh, int ww);
Rectangel centeredRect(int width, int height) const
bool overlaps(const Rectangle& other) const;
};
}
我會離開的.cpp,因爲它很容易看到rectangel在這一點,我不打什麼部位矩形類不想讓你們失望,
然後,我有一個Sprite類是在遊戲中人物超,
namespace galaxy{
class Sprite{
public:
virtual void draw() = 0;
virtual ~Sprite();
virtual void keyLeft(SDLKey k);
virtual void keyRight(SDLKey k);
......more buttons
protected:
Sprite(int x, int y, int h, int w);
private:
Rectangle rect;
Sprite (const Sprite&);
const Sprite& operator=(const Sprite&);
};
}
在.cpp文件我有以下代碼
namespace galaxy{
Sprite::Sprite{int x, int y, int h , int w) : rect (x, y, h, w){}
Sprite::~Sprite(){}
const Rectangel& Sprite::getRect() const{
return rect;
}
void Sprite::keyLeft(SDLKey k){}
void Sprite::keyRight(SDLKey k){}
void Sprite::keyDown(SDLKey k){}
...more buttons
}
然後到問題的所在,我有另一個類Ship,在那裏我想從超類中重載keyLeft,矩形矩形和座標一致,並且我需要改變子類中的x和y,但是當這樣做的時候, .X ++;在函數內部行爲,並且在退出函數時正在清除對矩形x的更改,當試圖在Ship類中達到矩形時,當通過r = getRect()獲取矩形時,我得到錯誤,但不可達。對r的改變只在函數內部,但是船不能在屏幕上移動。
//Ship.h
namespace galaxy {
class Ship : public Sprite{
public:
void keyLeft(SDLKey k);
void keyRight(SDLKey k);
void keyDown(SDLKey k);
void keyUp(SDLKey k);
void space(SDLKey k);
Ship(int x, int y, int hits);
private:
SDL_Surface* ship;
int hits;
};
}
//Ship.cpp
using namespace std;
namespace galaxy{
Rectangel r;
Ship::Ship(int x, int y, int hits) : Sprite(x, y, NULL, NULL), hits(hits){
ship = IMG_Load("Ship.png");
}
//Here is where my problem is.....
void Ship::keyLeft(SDLKey k){
std::cout << "Im here!\n";
std::cout << r.getX(); // outputs 250
r.x--;
std::cout << r.getX(); // outputs 251
}
void Ship::keyRight(SDLKey k){
std::cout << "Im here!\n";
}
void Ship::keyDown(SDLKey k){
std::cout << "Im here!\n";
}
void Ship::keyUp(SDLKey k){
std::cout << "Im here!\n";
}
void Ship::space(SDLKey k){
std::cout << "Im here!\n";
}
void Ship::draw(){
r = getRect();
SDL_BlitSurface(ship, NULL, sys.screen, &r);
}
}
現在Im做此類似:
#ifndef SHIP_H
#define SHIP_H
#include "Sprite.h"
#include <string>
namespace galaxy {
class Ship : public Sprite{
public:
/*static Ship* getInstance(int x, int y, int hits);*/
void draw();
//virtual void perform() = 0;
/*int getHits() const;*/
int getX() const;
int getY() const;
const Rectangel& getRect() const;
void keyLeft(SDLKey k);
void keyRight(SDLKey k);
void keyDown(SDLKey k);
void keyUp(SDLKey k);
void space(SDLKey k);
//~Ship();
//protected:
Ship(int x, int y, int hits);
private:
SDL_Surface* ship;
int hits;
Rectangel rect;
};
}
#endif
#include "Ship.h"
#include "Globals.h"
#include "Sprite.h"
#include <SDL_image.h>
#include <iostream>
using namespace std;
namespace galaxy{
Rectangel r;
/*Ship* Ship::getInstance(int x, int y, int hits){
return new Ship(x, y, hits);
}*/
Ship::Ship(int x, int y, int hits) : Sprite(x, y, NULL, NULL), hits(hits){
ship = IMG_Load("Ship.png");
}
const Rectangel& Ship::getRect() const{
return rect;
}
void Ship::keyLeft(SDLKey k){
std::cout << "Im here!\n";
rect.x--;
}
void Ship::keyRight(SDLKey k){
std::cout << "Im here!\n";
}
void Ship::keyDown(SDLKey k){
std::cout << "Im here!\n";
}
void Ship::keyUp(SDLKey k){
std::cout << "Im here!\n";
}
void Ship::space(SDLKey k){
std::cout << "Im here!\n";
}
/* int Ship::getHits() const{
return hits;
}*/
int Ship::getX() const{
return r.getX();
}
int Ship::getY() const{
return r.getY();
}
void Ship::draw(){
r = getRect();
SDL_BlitSurface(ship, NULL, sys.screen, &r);
}
}
但是,這只是一種變通方法,使我不能永遠停留。
得使用虛函數的多態性。 – IdeaHat