c# - Score not changing when collision occurs -


i have been trying score work correctly when player collides 1 of collectibles score not seem change when collision occurs, not sure why happening.

in collectible have this:

class blueball : obj {     public blueball(vector2 pos)          : base(pos)     {         position = pos;         spritename = "blueball";         solid = false;         score = 0;     }      public override void update()     {         if (!alive) return;          player player = checkcollisionagainst<player>();         if (player != null)         {             score = score + 20;             alive = false;         } 

i drawing in game1 class with:

spritebatch.drawstring(font, "score: " + score.tostring(), scorepos, color.white); 

so if player collides blueball, 20 should added score , blueball should disappear, disappear score not change, why this?

at moment declaring score in game1 class public int score , vector2 scorepos position it. initialize score score = 0; , load in scorepos values in update.

from code, , comments, clear there two score variables. 1 owned game:

public class game {     private int score;      void draw(...)     {        spritebatch.drawstring(font, "score: " + score.tostring(), scorepos, color.white);     } } 

and 1 owned ball:

public class blueball : obj {     private int score;      public void checkcollisions()     {         if (collision)            score += 20;     } } 

now, 2 score variables have absolutely no relation each other. fact named same is totally irrelevant.

to make quick analogy, imagine programming objects boxes. each variable "counter". put counter in "game" box , call "score". put another counter in "ball" box , call "score". obviously, modifying counter in "ball" box won't affect 1 in "game" box , vice versa. in general, concept called "scoping" in programming. each variable scoped box, can class, function, or using block (scopes created {}).

now understand problem, there couple ways fix it:

  1. create global variable. can't in c#, can create object both other objects have access to. both objects reference/modify it's "score" property. simple (and poor practice) example be:

    public static class globalstate {     public static int gamescore { get; set; } } 

all references score become globalstate.gamescore. again, going, kind of code is bad practice, , can cause bugs (especially in multi-threaded situations).

  1. have ball raise event on collision. game register event , increment "score" variable appropriately.

  2. pass game's "score" variable collision method ref, can change it. warning: included completeness, , should done in specific circumstances, of yours not 1 of them.

  3. pass delegate ball on creation invoke when needs increment score. variation of event method.

i'm sure there others. given experience level, start global approach (as simplest , requires least amount of knowledge) , move learning events , go approach in future.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -