• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Problem with global variables

 
Ranch Hand
Posts: 71
Mac Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to rewrite a tutorial for slick2d in my own way, but I've met a block. I want to use this code in a void method, while sheet is declared in another class. Is this possible? It says sheet cannot be resolved. I'm on Eclipse. Thanks in advance.

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'll need to pass sheet to your method. Methods can't automatically see member variables defined in other classes.

Maybe this is an indication that you should be thinking more about the design of your classes. Think about which classes you have, and which methods belong in which classes.
 
Marshal
Posts: 74025
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That isn’t a global variable. It is a private field in another class. You should avoid global variables like the plague, but if you are trying to get a field from another class, you should reconsider your design.
It might be appropriate to give that other class a getSheet() method, or getSpritesFromSheet(), but I don’t know enough about your application to tell that sort of thing. Don’t give the field more visibility in order to placate the compiler.
 
Alix Ollivier
Ranch Hand
Posts: 71
Mac Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sheet is a SpriteSheet, a .png with many sprites on it. I want to only load one, to make my game load faster and to keep all the assets in one location. Each entity that I will make hopefully will have an public void init() method in which the class initializes all of the variables and assets needed. The method below allows my player class to initialize where his sprites are.
 
Bras cause cancer. And tiny ads:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic