我与嵌入式微控制器(Arduino的)比赛继续进行,我对课堂互动的一个问题-这个问题从我刚才的问题继续在这里 ,我已经根据我的sheddenizen的建议代码(见对给定链接在“这里”):
我有一个从基类 - 继承三类
(ⅰ) 类的Sprite - (低音类)具有一个位图的形状和(X,Y)位置处的LCD上
(ⅱ) 类导弹:公共的Sprite -具有特定的形状,(X,Y),并且还需要一个物镜
(ⅲ) 类外来:公共的Sprite -具有特定的形状和(x,y)的
(四) 班的球员:公共雪碧 - “”
它们都具有移动并显示在液晶显示器上的不同(虚拟)方法:
我的精简代码如下-具体而言,我只希望该导弹在一定条件下火:创建导弹时,它需要一个对象(X,Y)的值,我怎么能继承的类中访问一个传递的对象的价值?
// Bass class - has a form/shape, x and y position
// also has a method of moving, though its not defined what this is
class Sprite
{
public:
Sprite(unsigned char * const spacePtrIn, unsigned int xInit, unsigned int yInit);
virtual void Move() = 0;
void Render() { display.drawBitmap(x,y, spacePtr, 5, 6, BLACK); }
unsigned int X() const { return x; }
unsigned int Y() const { return y; }
protected:
unsigned char *spacePtr;
unsigned int x, y;
};
// Sprite constructor
Sprite::Sprite(unsigned char * const spacePtrIn, unsigned int xInit, unsigned int yInit)
{
x = xInit;
y = yInit;
spacePtr = spacePtrIn;
}
/*****************************************************************************************/
// Derived class "Missile", also a sprite and has a specific form/shape, and specific (x,y) derived from input sprite
// also has a simple way of moving
class Missile : public Sprite
{
public:
Missile(Sprite const &launchPoint): Sprite(&spaceMissile[0], launchPoint.X(), launchPoint.Y()) {}
virtual void Move();
};
void Missile::Move()
{
// Here - how to access launchPoint.X() and launchPoint.Y() to check for
// "fire conditions"
y++;
Render();
}
// create objects
Player HERO;
Alien MONSTER;
Missile FIRE(MONSTER);
// moving objects
HERO.Move();
MONSTER.Move();
FIRE.Move();