I have created a static class which needs to be able to change the sprite used in an object. However, in non-static classes, I was able to refer to the ContentManager in the object as this.Content
, but in a static class, it says "this" cannot be used.
Im a bit lost as to how to reference the content manager in this object from the static class. I tried using the object instead of this
( enemies[i].
), but this didnt work. I also tried using just ContentManager.
, but it tells me that doesnt exist as well.
I still dont fully understand the ContentManager and why it needs to be in each object, but I've had a hard time finding really detailed information about what it is and what it does (most tutorials seem to gloss over it, just saying that its necessary)
Heres a snippet of the code I have so far. Its simplified a bit (a lot more goes on), but only the this.Content
part is giving me trouble:
public static void fight(List<enemy> enemies)
{
for (int i = 0; i < enemies.Count; i++)
{
if (enemies[i].hp <= 0)
{
enemies[i].LoadContent(this.Content, "spr_enemy_dead");
}
This is the method that contains the content manager inside the enemy
object:
public void LoadContent(ContentManager theContentManager, string AssetName)
{
spr_enemy = theContentManager.Load<Texture2D>(AssetName);
}
There is probably a better way to do this, but I havent found anything in my searching.
If the ContentManager is static, probably declared like this:
class YourObject
{
static public ContentManager Content;
You are already within a static function of this object, so you can simply access it like this:
enemies[i].LoadContent(Content, ...
or:
enemies[i].LoadContent(YourObject.Content, ...
If your Content object is not declared statically, you can't access it within a static call unless you have a reference on YourObject...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.