Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (601)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  [LWJGL] Tile System: How to makes only certain tiles change  (Read 1354 times)
0 Members and 1 Guest are viewing this topic.
Offline Mr.CodeIt
« Posted 2014-04-29 23:44:22 »

If you look at my projects in my profile, or the newest project in showcase, you'll see I have a basic tile engine. I spent most of my time trying to debug its' problems. I have a way of making the tiles load according to their size. The problem is, whenever I call the sizechanger method, it changes the size of all boxes.

Mainly I am asking if you can edit my code so that each rectangle is different when It is called or resized. I just couldn't think of a good way. Hope you can help on my first project.

Notice
Keep in mind the copyright comment is because I used his set up and basics. I implemented the resizing and location,  and how it is arranged.


The code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251  
252  
253  
254  
255  
256  
257  
258  
259  
260  
261  
262  
263  
264  
265  
266  
267  
268  
269  
270  
271  
272  
273  
274  
* Copyright (c) 2013, Oskar Veerhoek
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * The views and conclusions contained in the software and documentation are those
 * of the authors and should not be interpreted as representing official policies,
 * either expressed or implied, of the FreeBSD Project.
 */

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;



import java.util.ArrayList;
import java.util.List;



import static org.lwjgl.opengl.GL11.*;


/**
 * Shows how to use input to achieve cool results. Thanks to Azziplekkus1337 for the better way of handling the
 * cool-down!
 *
 * @author Oskar
 */

public class InputDemo {


   
    private static final List<Box> shapes = new ArrayList<Box>(16);
    private static boolean somethingIsSelected = false;
    private static long lastColourChange;

    public static void main(String args[]) {
        try {
            Display.setDisplayMode(new DisplayMode(640, 480));
            Display.setTitle("Input Demo");
            Display.create();
        } catch (LWJGLException e) {
            e.printStackTrace();
            Display.destroy();
            System.exit(1);
        }
       
        glMatrixMode(GL_PROJECTION);
        glOrtho(0, 640, 480, 0, 1, -1);
        glMatrixMode(GL_MODELVIEW);
        while (!Display.isCloseRequested()) {
            glClear(GL_COLOR_BUFFER_BIT);
            while (Keyboard.next()) {
               //RED
                if (Keyboard.getEventKey() == Keyboard.KEY_1 && Keyboard.getEventKeyState()) {
                   if (Box.size == 0){
                    shapes.add(new Box(0, 463, "r"));
                   }
                   if (Box.size == 1){
                        shapes.add(new Box(0, 448, "r"));
                    }
                   if (Box.size == 2){
                        shapes.add(new Box(0 , 430, "r"));
                    }
                   if (Box.size == 3){
                        shapes.add(new Box(0 , 430, "r"));
                    }
                }
                //BLUE
                if (Keyboard.getEventKey() == Keyboard.KEY_3 && Keyboard.getEventKeyState()) {
                   if (Box.size == 0){
                        shapes.add(new Box(129, 463, "b"));
                       }
                       if (Box.size == 1){
                            shapes.add(new Box(129, 448, "b"));
                        }
                       if (Box.size == 2){
                            shapes.add(new Box(129 , 430, "b"));
                        }
                       if (Box.size == 3){
                            shapes.add(new Box(129, 430, "b"));
                        }
                }
                //GREEN
                if (Keyboard.getEventKey() == Keyboard.KEY_2 && Keyboard.getEventKeyState()) {
                   if (Box.size == 0){
                        shapes.add(new Box(64, 463, "g"));
                       }
                       if (Box.size == 1){
                            shapes.add(new Box(64, 448, "g"));
                        }
                       if (Box.size == 2){
                            shapes.add(new Box(64, 430, "g"));
                        }
                       if (Box.size == 3){
                            shapes.add(new Box(64, 430, "g"));
                        }
                }
                //WHITE
                if (Keyboard.getEventKey() == Keyboard.KEY_0 && Keyboard.getEventKeyState()) {
                   if (Box.size == 0){
                        shapes.add(new Box(590, 463, "w"));
                       }
                       if (Box.size == 1){
                            shapes.add(new Box(590 - 32, 448, "w"));
                        }
                       if (Box.size == 2){
                            shapes.add(new Box(590 , 430, "w"));
                        }
                       if (Box.size == 3){
                            shapes.add(new Box(590 , 430, "w"));
                        }
                }
            }
            if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
                Display.destroy();
                System.exit(0);
            }
            for (final Box box : shapes) {
                if (Mouse.isButtonDown(0) && box.isInBounds(Mouse.getX(), 480 - Mouse.getY()) && !somethingIsSelected) {
                    somethingIsSelected = true;
                    box.selected = true;
                }
                if (Mouse.isButtonDown(2) && box.isInBounds(Mouse.getX(), 480 - Mouse.getY()) && !somethingIsSelected) {
                    if ((System.currentTimeMillis() - lastColourChange) >= 200 /* milliseconds */) {
                        box.addSize();
                        lastColourChange = System.currentTimeMillis();
                    }
                }
                if (Mouse.isButtonDown(1)) {
                    box.selected = false;
                    somethingIsSelected = false;
                }


                if (box.selected) {
                    box.update(Mouse.getDX(), -Mouse.getDY());
                }

                box.checkSize();
                box.draw();
            }


            Display.update();
            Display.sync(60);
        }


        Display.destroy();
    }


    private static class Box {


       
        public static int size = 3;
      public int x, y;
        public boolean selected = false;
        private int colorRed, colorBlue, colorGreen;
        @SuppressWarnings("unused")
      public String color;
       
       


        Box(int x, int y, String color) {
            this.x = x;
            this.y = y;
           


          //  Random randomGenerator = new Random();
            if (color == "r") {
               colorRed = 255;
               colorBlue = 0;
               colorGreen = 0;
               
               }
            if (color == "b") {
               colorBlue = 255;
               colorRed = 0;
               colorGreen = 0;
               }
           
           
            if (color == "g") {
               colorBlue = 0;
               colorRed = 0;
               colorGreen = 255;
               }
           
            if (color == "w") {
               colorRed = 255;
               colorBlue = 255;
               colorGreen = 255;
               
               }
        }


        boolean isInBounds(int mouseX, int mouseY) {
            return mouseX > x && mouseX < x + 50 && mouseY > y && mouseY < y + 50;
        }


        void update(int dx, int dy) {
            x += dx;
            y += dy;
        }
        public void checkSize() {

            if (size > 3) {
               size = 0;
            }
        }


        void addSize() {
           size++;
        }


        void draw() {
            glColor3f(colorRed, colorGreen, colorBlue);
            glBegin(GL_QUADS);
            glVertex2f(x, y);
            if(size == 2){
            glVertex2f(x + 32, y);
            glVertex2f(x + 32, y + 32);
            glVertex2f(x, y + 32);
            }
            if(size == 0) {
                glVertex2f(x + 8, y);
                 glVertex2f(x + 8, y + 8);
                 glVertex2f(x, y + 8);
            }
            if(size == 1) {
               glVertex2f(x + 16, y);
                glVertex2f(x + 16, y + 16);
                glVertex2f(x, y + 16);
           }
            if(size == 3) {
                  glVertex2f(x + 64, y);
                   glVertex2f(x + 64, y + 64);
                   glVertex2f(x, y + 64);
              }
            glEnd();
        }
    }
}


 Pointing Pointing Pointing Pointing Pointing Pointing Pointing Pointing Pointing Pointing
Offline Slyth2727
« Reply #1 - Posted 2014-04-30 00:00:26 »

We aren't going to write your code for you. That's a fact.

However, we can give you suggestions  Pointing
Your problem is that size is static, which means that it will contain the same value in all instances, among other things.

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline Mr.CodeIt
« Reply #2 - Posted 2014-04-30 00:03:55 »

We aren't going to write your code for you. That's a fact.

What a way with words...

Quote
However, we can give you suggestions  Pointing
Your problem is that size is static, which means that it will contain the same value in all instances, among other things.

Anyways, I actually don't know the proper meaning of static. I put "Look up static, abstract, etc.." on my TODO list but never did it. Who knew the answer was following my own orders.
Thanks
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mr.CodeIt
« Reply #3 - Posted 2014-04-30 00:24:28 »

We aren't going to write your code for you. That's a fact.

However, we can give you suggestions  Pointing
Your problem is that size is static, which means that it will contain the same value in all instances, among other things.

I did research on static and the int size has to be static when I create it. Is there any other way to call it without static errors/exceptions?
Offline Slyth2727
« Reply #4 - Posted 2014-04-30 00:39:18 »

Understanding static is very important to coding in Java, and some other OOP languages for that matter.

Why does size have to be static? With your design, it shouldn't be static, but be a private variable that you can retrieve with a getSize method. Said method would look like this:

1  
2  
3  
public int getSize() {
return size;
}


So just a basic getter. I don't know how much experience you have in Java, but from what it looks like, you might want to take some time to learn the language a bit better. That's not an insult, it's just a suggestion based on what I've seen Smiley
Of course I could be completely wrong, and you could know more about the language than I and others in other areas of it. However knowing the basic keywords is a very important thing.

But could you explain why size must be static?

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline Mr.CodeIt
« Reply #5 - Posted 2014-04-30 00:58:20 »

Understanding static is very important to coding in Java, and some other OOP languages for that matter.

Why does size have to be static? With your design, it shouldn't be static, but be a private variable that you can retrieve with a getSize method. Said method would look like this:

1  
2  
3  
public int getSize() {
return size;
}


So just a basic getter. I don't know how much experience you have in Java, but from what it looks like, you might want to take some time to learn the language a bit better. That's not an insult, it's just a suggestion based on what I've seen Smiley
Of course I could be completely wrong, and you could know more about the language than I and others in other areas of it. However knowing the basic keywords is a very important thing.

But could you explain why size must be static?

Thanks for the suggestion, I always took that as an insult XD. I do need to work on java a bit more. I Learned java stubbornly and wouldn't learn anything unless I needed to which made things confusing. Anyways if you delete the static part from my code it will give an exception where ever you use it in the InputDemo.

EXAMPLE:

1  
2  
3  
4  
5  
if (Keyboard.getEventKey() == Keyboard.KEY_1 && Keyboard.getEventKeyState()) {
                  //Box.size gets an error
                   if (Box.size == 0){
                    shapes.add(new Box(0, 463, "r"));
                   }


I'll be trying the get size. I tried before but that was when it was very buggy
Offline Slyth2727
« Reply #6 - Posted 2014-04-30 01:07:15 »

In that case you're going to make things called getters and setters, or you can make the variable public (not static). Getters are methods that let you access private variables in a class instance, and setters are methods that let you set private variables in a class instance. Using private variables w/ getters and setters is generally suggested.

So you could have your variable like this:

1  
private int size;


and then create these:

1  
2  
3  
4  
5  
6  
7  
8  
9  
//Getter
public int getSize() {
   return size;
}

//Setter
public void setSize(int size) {
   this.size = size;
}


Now you can have a public static (maybe final?) variable that acts as a default for the size. When variables are static final, we usually uppercase them. So, unless you're going to be editing the default tile size, you could have something like this in either your tile manager class or tile class itself:

1  
public static final int SIZE = (int) whatever size ya want;


Check out some basic Java tuts to get a good grip on the language, I highly suggest this one as it ranges from very basic to advanced topics: http://www.tutorialspoint.com/java/

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline Mr.CodeIt
« Reply #7 - Posted 2014-04-30 01:11:47 »

I'm familiar with getters and setters but had no idea they access only private variables. Ironically,  I use that site you linked.
Offline Slyth2727
« Reply #8 - Posted 2014-04-30 01:15:01 »

Oh, they don't have to only access private variables. It's just that there's no point in having them if the variable is public since you can just well, access it with just the instance.
About the site, just read it over a bit and think of small things that you can use the new/reviewed knowledge for. Nothing large or game-related, just a simple 10-50 line program. Or more, depending on what you want to do.

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline Mr.CodeIt
« Reply #9 - Posted 2014-04-30 01:27:25 »

in the if statements, it tells me to use static in the get methods, but I'll figure something out. Undecided
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Slyth2727
« Reply #10 - Posted 2014-04-30 01:34:17 »

What do you mean in the if statements?

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline Mr.CodeIt
« Reply #11 - Posted 2014-04-30 01:45:58 »

What do you mean in the if statements?

In here:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
//RED
                if (Keyboard.getEventKey() == Keyboard.KEY_1 && Keyboard.getEventKeyState()) {
                   if (Box.size == 0){
                    shapes.add(new Box(0, 463, "r"));
                   }
                   if (Box.size == 1){
                        shapes.add(new Box(0, 448, "r"));
                    }
                   if (Box.size == 2){
                        shapes.add(new Box(0 , 430, "r"));
                    }
                   if (Box.size == 3){
                        shapes.add(new Box(0 , 430, "r"));
                    }
                }



I'll probably have to mess around with the List so each box has its' own size based on what it is set to.
1  
private static final List<Box> shapes = new ArrayList<Box>(16);

Offline opiop65

JGO Kernel


Medals: 161
Projects: 7
Exp: 4 years


JumpButton Studios


« Reply #12 - Posted 2014-04-30 01:50:38 »

Well first off, why is your list 'final'? That means you cannot change the object after it is created.

Offline Elusivehawk

Junior Devvie


Medals: 1
Projects: 1



« Reply #13 - Posted 2014-04-30 01:57:59 »

Well first off, why is your list 'final'? That means you cannot change the object after it is created.

Actually, in Java at least, it means you can't change the value stored within that field.
Offline opiop65

JGO Kernel


Medals: 161
Projects: 7
Exp: 4 years


JumpButton Studios


« Reply #14 - Posted 2014-04-30 01:59:25 »

Yeah that's true. There actually is a big difference there... thanks!

Offline Slyth2727
« Reply #15 - Posted 2014-04-30 01:59:41 »

The Box.size is where you use the public static final SIZE variable that you literally just talked about right before the List code. However, the way you are talking about it, it sounds like you will just want to accept a size into the constructor and remove the final SIZE variable that I said you may want to add.
And what opiop said.

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline SwordsMiner

JGO Coder


Medals: 8
Projects: 2
Exp: 1 year


The one and only.


« Reply #16 - Posted 2014-04-30 02:01:41 »

Quote
Anyways, I actually don't know the proper meaning of static. I put "Look up static, abstract, etc.." on my TODO list but never did it.

STOP! You obviously dont know enough of java to be making anything.  Pointing

Know those things are essential, and can help you A LOT and save A LOT of code, and especially A LOT of headaches like this. I suggest stopping, and look at youtube tutorials like "How to make a 2d java game" and do a few of them that pop up- Thats how I learned all of my java, and then restart.

Let the flaming of others on me begin.

If I made you laugh, helped you at all, or did something cool, I only ask that you smash that appreciate button with your nose.
Offline Longarmx
« Reply #17 - Posted 2014-04-30 02:18:29 »

I suggest stopping, and look at youtube tutorials like "How to make a 2d java game" and do a few of them that pop up- Thats how I learned all of my java, and then restart.

Actually, before you even do this, you should look up basic language tutorials. You should be comfortable with Java before you try to make any game in it. Therefore, I would suggest looking up tutorials like "beginner java tutorial." The reason is, most (if not all) good 2d Java game tutorials assume that you know the language already, and therefore, they aren't going to try to teach you the language.

Offline Mr.CodeIt
« Reply #18 - Posted 2014-04-30 02:23:57 »

I suggest stopping, and look at youtube tutorials like "How to make a 2d java game" and do a few of them that pop up- Thats how I learned all of my java, and then restart.

Actually, before you even do this, you should look up basic language tutorials. You should be comfortable with Java before you try to make any game in it. Therefore, I would suggest looking up tutorials like "beginner java tutorial." The reason is, most (if not all) good 2d Java game tutorials assume that you know the language already, and therefore, they aren't going to try to teach you the language.

What I decided to do is look through long java series and select the ones that have something I don't know about, so I get the knowledge of anything I skipped or didn't learn properly
Offline opiop65

JGO Kernel


Medals: 161
Projects: 7
Exp: 4 years


JumpButton Studios


« Reply #19 - Posted 2014-04-30 02:26:11 »

Well to be honest with you... it seems like you don't know the basics at all. I think you really should take the time to go through a language tutorial front to back. It really will help.

Offline Nausica
« Reply #20 - Posted 2014-04-30 02:46:02 »

Been wondering this for a while...have you actually done basic java? Like, the Sun tutorial, maybe some coding practice over at codingbat.com?
Offline Gibbo3771
« Reply #21 - Posted 2014-04-30 04:40:15 »

I was going to post another "Step back and learn basic Java" replies but I think I've done 3 or so if those for the op and it never sinks in.

When you start using private statics without knowing what static or private means, your really in a bad spot.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline SwordsMiner

JGO Coder


Medals: 8
Projects: 2
Exp: 1 year


The one and only.


« Reply #22 - Posted 2014-04-30 12:01:36 »

When you start using private statics without knowing what static or private means, your really in a bad spot.

..and those of us that do know laugh, because thats just stupid.  Pointing  Grin

If I made you laugh, helped you at all, or did something cool, I only ask that you smash that appreciate button with your nose.
Offline Gibbo3771
« Reply #23 - Posted 2014-04-30 12:16:44 »

When you start using private statics without knowing what static or private means, your really in a bad spot.

..and those of us that do know laugh, because thats just stupid.  Pointing  Grin

Yeah, I rarely use static fields except for maybe a camera or a Box2D world, I remember when I first made Pong I had set the paddles to static, couldn't figure out why only 1 could be controlled.

It is best to avoid it unless absolutely necessary.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline SwordsMiner

JGO Coder


Medals: 8
Projects: 2
Exp: 1 year


The one and only.


« Reply #24 - Posted 2014-04-30 12:25:58 »

Yeah, I rarely use static fields except for maybe a camera or a Box2D world
I also use it for like standard things, like the name of the window, icons, a block's size (if I know it shouldnt be changed, i do it so its easy to access by collision engine)

If I made you laugh, helped you at all, or did something cool, I only ask that you smash that appreciate button with your nose.
Offline opiop65

JGO Kernel


Medals: 161
Projects: 7
Exp: 4 years


JumpButton Studios


« Reply #25 - Posted 2014-04-30 15:16:00 »

Static isn't a terrible thing like you make it out to be...

If you have a value or field which is the same across a group of objects, and it will not change in the individual objects, make it static. It makes no sense to have every object have an instance of the same object?

Static isn't evil. Please don't encourage that thinking.

Offline jonjava
« Reply #26 - Posted 2014-05-01 01:40:06 »

Static is evil. As we have witnessed here today on this altar (thread) of blood. If you don't know what static is, don't use it. Even if you know what static is, you probably shouldn't use it.

Exceptions include:
1. You know what you're doing.
2. You're lazy and you know you're shaking hands with the devil.

If you have a value or field which is the same across a group of objects, and it will not change in the individual objects, make it static. It makes no sense to have every object have an instance of the same object?

This falls under the lazy category. Lazy doesn't necessarily mean wrong, though. Especially if you want to get things done.

2cents

Offline opiop65

JGO Kernel


Medals: 161
Projects: 7
Exp: 4 years


JumpButton Studios


« Reply #27 - Posted 2014-05-01 03:49:17 »

I think its part of a design pattern and is actually good coding. How is it lazy to utilize the language more and conserve precious memory in the process? I see no issue using static as long as you know what you're doing.

Offline Nausica
« Reply #28 - Posted 2014-05-01 19:24:33 »

Static is evil. As we have witnessed here today on this altar (thread) of blood. If you don't know what static is, don't use it. Even if you know what static is, you probably shouldn't use it.

Exceptions include:
1. You know what you're doing.
2. You're lazy and you know you're shaking hands with the devil.

If you have a value or field which is the same across a group of objects, and it will not change in the individual objects, make it static. It makes no sense to have every object have an instance of the same object?

This falls under the lazy category. Lazy doesn't necessarily mean wrong, though. Especially if you want to get things done.

2cents

How is static lazy in any way? Name a situation in which static would be a viable solution to a problem where there is a more efficient solution that does not involve static.
Offline opiop65

JGO Kernel


Medals: 161
Projects: 7
Exp: 4 years


JumpButton Studios


« Reply #29 - Posted 2014-05-01 19:27:53 »

How is static lazy in any way? Name a situation in which static would be a viable solution to a problem where there is a more efficient solution that does not involve static.
Well... that might be stretching it a little. The key idea I was trying to get across was that static isn't evil like swords and gibbo are saying, it should just be used with caution and moderation because it can and will break code very quickly and frequently. Doesn't mean you can't use it though, its not as if something static is going to wipe your hard drive and cause your computer to explode. Its your fault really if you can't use statics correctly, not the language's fault. Learn to use statics, and then you can decide whether or not its a bad part of the language.

Pages: [1] 2
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (29 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (42 views)
2014-12-09 22:41:13

BurntPizza (76 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (50 views)
2014-12-03 16:27:13

CopyableCougar4 (47 views)
2014-11-29 21:32:03

toopeicgaming1999 (114 views)
2014-11-26 15:22:04

toopeicgaming1999 (102 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!