Jump to content
  • Advertisement
Sign in to follow this  
metorsummoner7

Sending Flags Across C# Class Flies

This topic is 2566 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Have three C# Class files one is for the player's sprite, another for the background, and one for the main logic. I want send a flag from the player's class file to the class file for my main logic. Then I want to send the flag from the main logic class file to the class for the background to update the background when the player reaches the edges of the screen. How would I do that?

Share this post


Link to post
Share on other sites
Advertisement
Firstly, a nitpick: just because you happen to have a single class per file doesn't mean that you want to send the flag between the files themselves. What you really want is a message to be passed between objects. For this, a convenient solution is the observer pattern, implemented in C# using delegates and events. On the other hand, I think this is massively overworked for what you're actually trying to do - I think you've got your class responsibilities confused. Why does the player know when it reaches the edge of the screen - why does it know about the screen at all? Have the logic update the player's position, and then test to see if the position is suitable and call 'background.Flip' (or whatever) if so.

Share this post


Link to post
Share on other sites

Firstly, a nitpick: just because you happen to have a single class per file doesn't mean that you want to send the flag between the files themselves. What you really want is a message to be passed between objects. For this, a convenient solution is the observer pattern, implemented in C# using delegates and events. On the other hand, I think this is massively overworked for what you're actually trying to do - I think you've got your class responsibilities confused. Why does the player know when it reaches the edge of the screen - why does it know about the screen at all? Have the logic update the player's position, and then test to see if the position is suitable and call 'background.Flip' (or whatever) if so.


The reason the player class needs to know where it place on the screen is because I set up collision detection to constrain the player to just the screen area. Also, the player class know about the screen is because I passed the values for the screen width and height from the main logic. Having those values let me offset the players sprite 128 pixels off of the vertical center to accommodate a hud.at the top of the screen. I don't think its all that massively worked since I have the files to organize my code instead of having a lot of data in one file. Since my background is broken down into 128 tiles over a 2D array wating for the update code. While my player class has an array that store 12 frames. 2 for animations in 4 directions and one frame for the players attack.

Share this post


Link to post
Share on other sites
The reason the player class needs to know where it place on the screen is because I set up collision detection to constrain the player to just the screen area. Also, the player class know about the screen is because I passed the values for the screen width and height from the main logic. Having those values let me offset the players sprite 128 pixels off of the vertical center to accommodate a hud.at the top of the screen.


Hm. It's just personal preference, but I don't like that. The player has a position, but because the constraints on its position aren't an inherent property of the player but rather of the game, I would rather these were at a higher level. Especially when the constraints on its position are being derived from properties of the physical representation, which is at least a couple layers of abstraction away. On the other hand, I can understand where this is coming from if you're having objects draw themselves...

I don't think its all that massively worked since I have the files to organize my code instead of having a lot of data in one file.[/quote]

That's not what I meant, sorry. I meant that the event system would be like using a sledgehammer to crack a nut since a tweaking of responsibilities would turn this into an if statement and a function call, which has the benefit of being simpler to understand since it's limited to a single point of use and has a lot less abstraction. (Abstraction is good, too much is bad.)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!