帮助我了解如何在子类中使用超类变量并能够对其进行更改,我正在使用 SDL 库制作克隆 Space Invaders 2D 游戏
首先,我有一个从 SDL_Rect 继承的 Rectangle 类,看起来像这样,我将把不重要的部分排除在外
//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,但是这样做时使用我在 r.x++ 之下的结构;在函数内部的行为,并且在退出函数时正在清除对矩形 x 的更改,当尝试在 Ship 类中到达 rect 时,我得到错误无法到达,当通过 r = getRect(); 获取 rect 时 对 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);
}
}
现在我这样做是这样的:
#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);
}
}
但这只是一种解决方法,因此我不会永远陷入困境。