c# - 如何将信息从一个类传递到另一个类

标签 c# c++ oop interface

我正在使用一个函数,该函数将获取两个类的状态我应该如何生成该函数,现在我正在这样做

 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/

相关文章:

c# - 从 C# 调用接受调用者分配的结构数组的 C 函数

c# - 过滤器 IQueryable 的动态表达式

c# - 基类 Generic 和继承类 .NET

java - 如何有效控制实现多接口(interface)的类对象?

php - 有没有办法在 php 中使用 $result->fetch_row 显示数据库中的图像?

php - 如何避免在 PHP 中重复实现 getter 函数

c# - 为什么中间件组件在 .Net Core 管道中被调用两次?

c++ - 编辑和播放实时 PCM

c++ - 在 C/C++ 良好实践中使用 '!!' 是否常见?

c++ - std::stack 是使用双链表实现的吗?