Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

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: 74374
334
  • 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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic