0

I'm making a game and I'm having trouble getting variables from another class. I have a Sprite class and a JPanel class. In my game, I want my score to go up once I hit a coin. My intersect coin code is in my Sprite class. How would I make it so when I hit the coin, my score goes up my 1? This code is from my JPanel class. These are variables declared all the way up top.

    public static int     CoinScore = 0;
    private String  Score = "Score :";
    private String  TextOnScreen=""; 

This is my paintcomponent method from the JPanel.

    public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;


    g2d.drawImage(BackgroundImage,0,0,720,480,0,0,720,480,null);


      g2d.setColor(Color.black);    
      g2d.fillRoundRect(25, 2, 100, 30, 10, 10); 
      g2d.setColor(Color.orange);      
      Font cp = new Font("Courier", Font.BOLD, 20); 
      g2d.setFont(cp); 
      TextOnScreen = Score + Integer.toString(CoinScore);
      g2d.drawString(TextOnScreen,30,30);  30,30.


    for (int i=0; i<mySprites.length; i++)
        mySprites[i].drawSprite(g2d);
} 

This is from my Sprite class.

    if (SpriteFilename == "Resources/sprite-concepts.png" && sprites[i].SpriteFilename == "Resources/coin2.png")
    {
    sprites[i].SpriteVisible = false; 
    sprites[i].SpriteBounds.width = 0; 
    sprites[i].SpriteBounds.height = 0; 
    } 

I tried putting CoinScore = CoinScore + 1 before the } in my if from the sprite class, but it says that CoinScore cannot be resolved to a variable? How would I fix it?

  • 2
    did you call it with the class name? `Class.Coinshare = Class.Coinshare +1;` Something similar to that? Since it is static and public you must call it with the class's name that it belongs to first, to access it. – 3kings May 07 '14 at 17:52
  • @3kings - you should list that as an answer. – Xynariz May 07 '14 at 17:55
  • since you have such a simple question there is no need for a whole game. You could use simple code to demonstrate what you don't know. That is -basic- troubsleshooting. – barlop May 07 '14 at 17:56
  • Create a model which can be shared between the view and controller – MadProgrammer May 07 '14 at 19:01

4 Answers4

1

did you call it with the class name? If not that is your problem.

Class.Coinshare = Class.Coinshare +1;

Something similar to that? Since it is static and public you must call it with the class's name that it belongs to first, to access it.

3kings
  • 838
  • 2
  • 13
  • 28
  • I made it public static cause I read that it should be static. In my case, would it be `GameJPanel.CoinScore+1; ` ? – user3542484 May 07 '14 at 17:58
  • @user3542484 If that is the class name it belongs to yes. – 3kings May 07 '14 at 17:59
  • @user3542484 - Assuming that your `JPanel` class is called `GameJPanel`, then yes. In Java, static means that there is only one variable for the whole class, no matter how many instances of that class you have. Hence, it needs to be accessed by class name. – Xynariz May 07 '14 at 18:00
1

If you're asking about basic variable scope in Java must read oracle documentation

CoinScore has been declared as static isn't it? so it's a class variable so it's possible to so smth similar to

ClassName.publicStaticVariable
Manu Artero
  • 9,238
  • 6
  • 58
  • 73
0

Does your drawSprite() method return anything? This could return the result of the intersection and you could check that to increment the score.

Or simply have another method in Sprite that let you know if an intersection occurred.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
larryboymi
  • 656
  • 6
  • 3
  • 2
    I would not support that design. I can't see `drawSprite()` returning changes in the score leading to a well designed system and clear code. – DSquare May 07 '14 at 17:58
  • I wasn't saying it would return a change to the score. It would return a boolean about intersection (which I also mentioned could be its own method). – larryboymi May 07 '14 at 18:00
  • Nope, it just draws the sprites. – user3542484 May 07 '14 at 18:00
0

There are plenty of answers here that will let you do exactly what you asked for (static access through class name, maybe make a singleton etc.)

However I would like to point out that
This is only the first of a load of problems you will soon be facing
(Unless, of course, the score is the only game state you have)

This is because you are mixing your model and your view. MVC is not always an easy answer (and not always the right answer either), but it will likely be best for you to think over your architecture. Having state altering logic in your draw logic is not a great idea. What if you later need to do some extra logic to determine bonuses for getting a sprite, and those turn out to be somewhat CPU-intensive? If you set an asych update in the model it would be fine, but since it's in the draw method your game will suddenly start freezing every now and again.

Another 2 arguments against such logic in the EDT to consider:

  • If the colliding sprite was covered by another panel or window during the collision - it will not count. Simply because the draw method was never called on the area.
  • Your movements may actually be fairly rare compared to the number of redraws per second (likely, unless you are building something realtime). Hence you are wasting a huge amount of resources doing the same collision check against objects that haven't even moved.

All in all, my advice would be to update the state of objects in a model and do collision checks there, while leaving the drawing thread, to, well, draw. You can even leave the collision logic in the Sprite class (that seems like a reasonable place for it), just not called from draw, but rather from a centralized state update method.

(Also, Global Variables are EVIL, most languages that have proper support for those have an according anti-pattern)

Ordous
  • 3,844
  • 15
  • 25