Is there a way to access a class's static methods/variables from an instance variable? I've tried searching for an answer, but my searches only find why you can't access an instance method/variable within a static method. I get why static can't access instance, but I don't get instance can't access static.
Here's my situation: I'm a student making a top-down shooter game in XNA and I'm trying to use static Texture2Ds for each game object. I have a class GameObject that lays the basics for every other class, with two other main classes GameBot and Projectile that lays the basics for bots and projectiles respectively. My problem also has to do with this inheritance. I have all the collision code inside the GameBot and Projectile classes, and other classes like PlayerShip/EnemyShip or Cannonball/Missile inherit from them.
The problem I'm having is that I want to access a class method/variable from an instance variable of which I don't know the class. What I mean is, I pass my method a GameBot variable, but it could be either PlayerShip, EnemyShip, or any other child of GameBot, and each has different static texture data.
class GameBot : GameObject
{
static protected Texture2D texture;
static internal Color[] textureData;
//etc...
internal bool DidHitEnemy(GameBot enemyGameBot)
{
//Here, I want to access enemyGameBot.textureData
//to do pixel-by-pixel collision
//but A) enemyGameBot.textureData doesn't work
//and B) enemyGameBot's class could be any child of GameBot
//so I can't just use GameBot.textureData
}
static internal virtual Color[] GetTextureData()
{
return textureData;
//I even thought about coding this function in each child
//but I can't access it anyway
}
}
This game is kind of an exercise in inheritance. I want to try to keep as much code as possible in classes higher up in the hierarchy, and only code the essential differences in each class. The reason I decided on static textures is so I can keep an array of Projectile in each GameBot, but be able to modify on-the-fly which Projectile (Cannonball, Missile, etc) is in some spot in that array. Without static projectiles, I'd have to assign the sprite every time I switch Projectiles. There reason I want one array of Projectile is so I can easily add another Projectile without having to add code everywhere.
Is there a way of accessing a static method/variable from an instance variable? If not, any suggestions of another way to keep the collision code as general as possible?
GameBot.textureDataworks. What do you mean when you say it "doesn't work"? And are you aware of the typo in your class identifier definition (Gamebotvs.GameBot)?