我正在使用一个函数,该函数将获取两个类的状态我应该如何生成该函数,现在我正在这样做
if((b1.fxmin>s.xmin&&s.ymin==b1.fymax)&&b1.fxmin<s.xmax&&s.ymin==b1.fymax))
{
collides=true;
b1.isFiring=false;
}
Else if((b1.fxmin>s1.xmin&&s1.ymin==b1.fymax)&&b1.fxmin<s1.xmax&&s1.ymin==b1.fymax))
{
collides1=true;
b1.isFiring=false;
}
In this project a bullet will hit a spider and if its collides then spider will vanish, b1 is the object of class bullet and s1 and s are spiders.
I have seven spiders in this game and I have created 7 collides variable and seven if statements this means that when ever i increase a spider i need to add this collide variable and if statement
我试图在子弹类中做到这一点,但未能成功。我应该如何将 spider 对象传递给 bullet 类?
最佳答案
您可以按如下方式将 Spider 传递给 bullet 类:
// Bullet.h
class Spider;
class Bullet {
...
bool isColliding(const Spider &s);
}
// Bullet.cpp
#include "Spider.h" // or whatever header you're using.
...
bool Bullet::isColliding(const Spider &s) {
// Collision logic
}
然后您可以通过执行以下操作从主函数调用此函数:
if(b1.isColliding(s1)) {
// Do something.
}
关于c# - 如何将信息从一个类传递到另一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29874372/