- Joined
- Mar 12, 2002
Ok guys I am in the middle of building out a simple platformer as the next extension of my game engine. In my previous experiments particularly with snake I have used the Thread class to launch a cooldown timer and come back and release the cooldown.
In snake I used it to prevent buttons from being clicked twice via this code:
Alright now moving forward I am trying to do something similar with my Hero class to prevent erratic jumping behaviors/exploits.
now I have tried both putting the call to jumpCooldown in jump() and at the end of the action step currently residing in the draw() method. The problem is that as soon as the call to jumpCooldown() happens the entire duration of the sleep call hits the entire game. This did not happen in snake when a button was clicked everything else still moved fluidly.
Am I missing something in my knowledge of how a new thread is supposed to work? My assumption was that the new thread goes off into the Java VM does what it does and then terminates, meanwhile the rest of the primary execution thread continue to operate independently. I even went one step further and put the call to jumpCooldown in its own thread which did not fix my issue.
Well thanks in advance for taking the time to look at this, and I welcome your input on the matter.
note: 3 vertical dots indicate an omission of code.
In snake I used it to prevent buttons from being clicked twice via this code:
Code:
public class Button extends Label {
private Texture active=null, inactive=null;
private Actions action;
private boolean cooldown = false;
.
.
.
protected void buttonCooldown(){
cooldown = true;
new Thread(new Runnable() {
public void run(){
try {
Thread.sleep(200);
}
catch (InterruptedException e) {
e.printStackTrace();
}
finally{
cooldown = false;
}
}
} ).run();
}//end buttonCooldown
protected void buttonCooldown(final int howLong){
cooldown = true;
new Thread(new Runnable() {
public void run(){
try {
Thread.sleep(howLong);
}
catch (InterruptedException e) {
e.printStackTrace();
}
finally{
cooldown = false;
}
}
} ).run();
}//end buttonCooldown( how long )
@Override
public boolean isLeftClicked(){
if(super.isLeftClicked() && !cooldown){
buttonCooldown();
return true;
}
else { return false; }
}//end isClicked
.
.
.
}
Alright now moving forward I am trying to do something similar with my Hero class to prevent erratic jumping behaviors/exploits.
Code:
public class Hero extends Dynamic_Tile {
protected String heroName;
protected int jumpCounter = 0, jumpSpeed = -18;
protected boolean jumping = false, jumpCooldown = false;
.
.
.
public void setJumpSpeed(int s){
jumpSpeed = s;
}//jump speed properties
public int getJumpSpeed(){ return jumpSpeed; }
public void jump(){
if (!jumpCooldown) {
jumping = true;
jumpCounter = 0;
//jumpCooldown();
}
}//end jump
public boolean isJumping(){ return jumping; }
protected void jumpCooldown(){
jumpCooldown = true;
new Thread(new Runnable() {
public void run(){
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
finally{
jumpCooldown = false;
}
}
} ).run();
}//end jump cooldown
@Override
public void draw(){
super.draw();
if(jumping){
this.moveDynamic(0, jumpSpeed);
this.jumpCounter++;
if(this.topCollide() || jumpCounter > 15){
jumping = false;
jumpCooldown();
}
}
}//end draw
}//end hero
now I have tried both putting the call to jumpCooldown in jump() and at the end of the action step currently residing in the draw() method. The problem is that as soon as the call to jumpCooldown() happens the entire duration of the sleep call hits the entire game. This did not happen in snake when a button was clicked everything else still moved fluidly.
Am I missing something in my knowledge of how a new thread is supposed to work? My assumption was that the new thread goes off into the Java VM does what it does and then terminates, meanwhile the rest of the primary execution thread continue to operate independently. I even went one step further and put the call to jumpCooldown in its own thread which did not fix my issue.
Well thanks in advance for taking the time to look at this, and I welcome your input on the matter.
note: 3 vertical dots indicate an omission of code.
Last edited: