繁体   English   中英

如何在继承的类中使用抽象方法?

[英]How to use abstract methods in inherited classes?

由于星期五,我目前正在做一个学校项目,还有很多工作要做。 任务是使用Monogame使用XNA框架制作视频游戏。 我目前正在处理碰撞。

游戏对象的结构如下所示: 在此处输入图片说明

对于碰撞,我有一个简单的碰撞课程

     class Collision{

public GameObject Other;

public GameObject Obj1;

public Collision(GameObject obj1, GameObject other)
{
    Other = other;
    Obj1 = obj1;
}}

冲突是通过GameObject类中的静态方法处理的:

public static void UpdateCollisions()
{
    //Empty the list
    AllCollisions.Clear();

    for (int a = 0; a < AllGameObjectsWithCollision.Count; a++)
    {
        GameObject obja = AllGameObjectsWithCollision[a];
        for (int b = 0; b < AllGameObjectsWithCollision.Count; b++)
        {
            GameObject objb = AllGameObjectsWithCollision[b];

            if (obja.Mask != null & objb.Mask!= null && obja != objb)
            {
                if (obja.Mask.CollisionRectangle.Intersects(objb.Mask.CollisionRectangle))
                    AllCollisions.Add(new Collision(obja, objb));
            }
        }
    }

}

到目前为止,游戏正在寻找所有应有的碰撞。 但是,现在我需要让我的对象知道它们正在碰撞,并告诉他们该怎么做。 为此,我使实体类成为抽象类,以便能够声明一个名为“ OnCollision(Collisionflict)”的抽象方法。

 abstract class Entity : GameObject { public float Health; public float MaxHealth; public bool Alive; public float OriginalDmg; public float Dmg; public abstract void OnCollision(Collision collision); } 

然后我在继承Entity类的类中重写方法

防爆。 弹丸

 class Projectile : Entity { Entity Owner; ProjectileType pType; public Projectile(Texture2D image, float maxSpeed, Entity owner, float dmg, ProjectileType type) { Image = image; MaxSpeed = maxSpeed; AccelerationSpeed = MaxSpeed; Owner = owner; Dmg = dmg; pType = type; } public override void OnCollision(Collision collision) { #region If this projectile friendly if (pType == ProjectileType.Friendly) { //If colliding with an enemy if (collision.Other.GetType() == typeof(Enemy)) { var enemy = (Enemy)collision.Other; enemy.Health -= Dmg; Destroy(this); } } #endregion #region If this projectile is hostile if (pType == ProjectileType.Hostile) { } #endregion } } 

然后,我尝试从GameObject类中的Update调用OnCollision方法。 这是我尝试通知对象是否碰撞以及与谁碰撞的方法:

  if (GetType().IsAssignableFrom(typeof(Entity))) { Entity entity = (Entity)this; if (GetType() == typeof(Player)) entity = (Player)this; if (GetType() == typeof(Enemy)) entity = (Enemy)this; if (GetType() == typeof(Projectile)) entity = (Projectile)this; var entityCol = FindCollision(entity); if (entityCol != null) entity.OnCollision(entityCol); } 

我是抽象类和重写的新手,所以我可能把整个想法弄错了。 但是似乎在我尝试Debug.WriteLine时未达到OnCollision方法,但是在输出窗口中没有任何显示。

感谢您的阅读,并尝试帮助我:)

如果您想查看所有代码,请使用Mediafire链接下载项目。

您应该阅读接口 接口提供了派生类必须实现的协定(一堆方法和属性)。 抽象类比接口更具体,因为它们还可以提供派生类的基本实现。 您只能从一个抽象类派生,而您可以从多个接口派生。 从帖子中的代码看来,您正在使用抽象类(如接口)。

您正在使用反射进行类型检查。 is关键字用于测试类型兼容性。 例如:

if(entity is Player)
{
    var player = (Player)entity;

    // player specific code
}

最后; 从我可以从您的帖子中收集到的信息来看,您似乎不太正确地使用继承。 看起来您正在正确地使用继承来构建类型层次结构,然后将所有逻辑都放在基类中。

继承的目的是让您将专用逻辑放入适当的类中。

public interface IGameObject
{
    void OnCollision(IGameObject target);
}

public class Player : IGameObject
{
    public void OnCollision(IGameObject target)
    {
         Console.WriteLine("Player collision");
    }
}

public class Projectile : IGameObject
{
    public void OnCollision(IGameObject target)
    {
         Console.WriteLine("Projectile collision");
    }
}

然后,当我们有对IGameObject的引用并调用OnCollision ,将自动调用相应的OnCollision函数。 例如:

IGameObject player = new Player();
IGameObject projectile = new Projectile();

player.OnCollision(projectile);
projectile.OnCollision(player);

输出:

Player collision
Projectile collision

嗯,如果声明错了...

使用

if (GetType().IsSubclassOf(typeof(Entity)))

修复。

愚蠢的错误,我不好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM