Global or local variables (J2ME)

Started by
0 comments, last by Woody FX 20 years, 6 months ago
I know its quicker to have variables declared local if they are going to be used in a class more than once. the structure of my class is as follows int box = 2; int square =0; private image background; private Image object; Graphics buffer; boolean trueORfalse; public ConstructorofClass() { buffer = bufferImage.getGraphics(); background = Image.createImage("/newback1.png"); } public void run () { while( running) { // all game code } } protected void paint() { // paint to screen } protected void keyPressed() { // handle input } i would like to make the often used variables local as to optimize speed that games executes. Can i declare and initilise them in the constructor, would that speed things up? or do i have to declare them inside the run() function. As most of the values held by my varialbes change through out the game as it runs and loops constantly through the run() function will the game keep reseting the varialbles everytime it re-enters the loop thus loosing the values that were set by the game in the prevoius loop. Thanks Brian I
Advertisement
I had a blank for a few days when i posted this but i''ve got clarity now! Nothing like a few days away from the problem to let you see the answer.


What i need to do is


declare all my game code variables in the run() method.


void run()
{

int etc;
boolean etc;
long etc

while (running)
{

// have all game code in this loop so it stays looping have acess
// to all variables

}
}

this would be faster than having all variables as globals .


I was wondering could i declare and then load all my images as well at the top of the run() method as i do all my setting up of the back buffer in the while loop?
So they would be local also?

Thanks
Brian

This topic is closed to new replies.

Advertisement