c# - How do I access higher level variables/objects in a child object? -
something haven't ever figured out how in c# following:
say have 2 classes: game.cs, , player.cs.
i find belongs game.cs must passed on through arguments of function belongs player.cs. example, if had sound effect loaded in game, , player child of game, , player has function "playsoundeffect()", way player access sound if pass through arguments: "playsoundeffect(soundeffect sound)"
what want know this:
- how can access object(s) without including them argument?
- why can't access them so: "game.sound", though sound in game?
just make global variables in game class static
- meaning, aren't part of game instance, , instead globally accessible.
eg, in game.cs
soundeffect sound;
becomes
static soundeffect sound;
now player class can do
game.sound.play();
or whatever needs.
Comments
Post a Comment