Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I don't know much about how this would work and Calendar, and this causes the game to freeze, but it is an essential part of the game and can not be chopped off. Trust me this is where the error is and this is what needs to be changed. I will also give you my imports. If you do need some more code just ask and I will revise the question.




if(hypspd == true && e.getKeyCode() == 16) {
          Calendar stop = Calendar.getInstance();
          Calendar start = Calendar.getInstance();
          long lstart = start.getTimeInMillis();
          long lstop = stop.getTimeInMillis();
          int istart = (int) (long) lstart;
          int istop = (int) (long) lstop;
          long elapsed = ((istop - istart) / 1000);
          istart = (int)start.getTimeInMillis();
          while(elapsed <= 10)
          {
          speed = 10;
                  t.setText("Score==>" + score + "         HYPER SPEED!!!!                           Speed: " + speed);
                  istop = (int)stop.getTimeInMillis();
          }
                  if(elapsed == 10)
                  {
                    speed = 50;
                    elapsed = 0;
                    elapsed = (istop - istart) / 1000;
                  t.setText("Score==>" + score + "                                                   Speed: " + speed);
                  }






Thanks in advance!
Posted
Updated 27-Jan-14 9:12am
v2
Comments
Gigabyte Giant 27-Jan-14 14:53pm    
...might not want to post all of your code, not many people have time to sit and look through every piece of code that you have written to try and find the problem.
Gigabyte Giant 27-Jan-14 16:35pm    
What exactly are you trying to do with the code? Is it supposed to give the game any certain functionality? We need some more information (to be able to help you).
Member 10553233 27-Jan-14 16:43pm    
Basically it is a snake game. What I want to do is when left shift is pressed, the speed changes from whatever it was to 10. However whenever I run the program and press shift, it freezes

1 solution

A couple changes I recommend you make, that might help, are to not use Calendar, it requires to much unnecessary work. As:
Java
Calendar stop = Calendar.getInstance();
Calendar start = Calendar.getInstance();
long lstart = start.getTimeInMillis();
long lstop = stop.getTimeInMillis();
int istart = (int) (long) lstart;
int istop = (int) (long) lstop;
long elapsed = ((istop - istart) / 1000);
istart = (int)start.getTimeInMillis();

can be done much easier using java.lang.System. See example:
Java
long startTime = System.currentTimeMillis();
long stopTime = System.currentTimeMillis();
long elapsedTime = (stopTime - startTime) / 1000;

See what I mean?

Another thing, is you do not need to use the key number like you did below:
Java
if(hypspd == true && e.getKeyCode() == 16) { }

Instead, take advantage of java.awt.event.KeyEvent. I also cleaned the code up, to make it easier to read for this example.
Java
int key = e.getKeyCode();
if (hypspd == true && key == KeyEvent.VK_SHIFT) {}


Basically, using the KeyEvent, you can get a more visual representation of what key you are choosing, instead of trying to use the actual KeyCode.

I am not sure if this will fix the problem with your game freezing, if not let me know, and I will investigate further. Also, please let me know if the changes help you at all!

Cheers!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900