简体   繁体   English

如何从派生类获取变量到继承类

[英]How to get variables from a derived class into an inherited class

I'm currently developing PacMan as a school project.我目前正在开发 PacMan 作为一个学校项目。 I have a base class called ghosts that finds the shortest path from the ghost to the target location.我有一个名为 ghosts 的基类,它可以找到从 Ghost 到目标位置的最短路径。 I also have classes such as red ghost which inherit it, these will give the base class a target and the current location of the ghost as well.我也有继承它的类,例如 red ghost,它们会给基类一个目标和鬼魂的当前位置。 When I use the red ghost it works... the variables are changed in the base class and then can be used, but for any of the other ghosts it won't work, and I can't seem to see why.当我使用红色幽灵时,它会起作用……变量在基类中发生了变化,然后可以使用,但是对于其他任何幽灵,它都不起作用,而且我似乎不明白为什么。 Any help is appreciated :)任何帮助表示赞赏:)

    pinkghost = new PinkGhost();
    redghost = new RedGhost();

    pinkghost.SetVariables();
    pinkghost.ChaseMode();
    redghost.SetVariables();
    redghost.ChaseMode();

public abstract class Ghosts
{
    public int ScatterX;
    public int ScatterY;
    public int TargetX;
    public int TargetY;
    public int GhostPosX;
    public int GhostPosY;
    public int StartNodeX;
    public int StartNodeY;
    public float speed = 1.0f;

    abstract public void SetVariables();
    abstract public void ChaseMode();

    public Ghosts()
    {
        //Allows ghost to access pacman coords
        PacMan = GameObject.Find("pacman");
    }


public class RedGhost : Ghosts
{
    public override void SetVariables()
    {
        GhostMov = gameobject.Find("blinky");
        ScatterX = 24;
        ScatterY = 3;
        GhostPosX = 13 + Convert.ToInt32(GhostMov.transform.position.x);
        GhostPosY = 15 - Convert.ToInt32(GhostMov.transform.position.y);
    }
}

// This one works…
class PinkGhost : Ghosts
{
    public override void SetVariables()
    {
        GhostMov = gameobject.Find("pinky");
        ScatterX = 4;
        ScatterY = 3;
        GhostPosX = 13 + Convert.ToInt32(GhostMov.transform.position.x);
        GhostPosY = 15 - Convert.ToInt32(GhostMov.transform.position.y);
    }
}

So as I said the red Ghost setvariables and chasemode work... but the pink ghost does not for some reason?所以正如我所说的,红色 Ghost setvariables 和 Chasemode 工作......但粉红色 Ghost 出于某种原因不起作用?

我看到的唯一区别是PinkGhost不是公开的。

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

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