I am writing a script that will cause the players HP to decrease at an interval as long as they are within a range of the item/monster/lava whatever it is. I have the detection just fine, but I cant seem to get the interval to run. I know this is probably because I am creating a new TimerTask as I render, but I cant seem to figure it out.
for(Monster monster : monsters) {
renderer.processEntity(monster);
if(player.withinDistance(10, monster.getPosition()))
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
System.out.println("tick");
Player.PLAYER_HEALTH -= 10;
}
}, 2000, 2000);
}
So for all monsters, it checks the positions, if you are near it opens a timer task that should tick every 2 seconds while that condition is true. How can I make this work properly? Is a timer task optimal for this situation?
The problem here is that you are updating the PLAYER_HEALTH static property of the Player class. You should update the Player instance!
for(Monster monster : monsters) {
renderer.processEntity(monster);
if(player.withinDistance(10, monster.getPosition()))
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
System.out.println("tick");
player.decreaseHealth(10); // Use the instance
}
}, 2000, 2000);
}
Also, if your program ends, the TimerTask will end too. Make sure your program is still running.
Related
So I'm making a game and I need a timer to regend/deregen stamina from the player.
Here's my current code:
Timer staminaTimer = new Timer(); //Java.util timer
public void update() {
staminaTimer.schedule(new TimerTask(){
public void run() {
//regen/Deregen stamina
}
}, 0L, 10L * 1000L);
}
So this timer is in an method that is getting called every game tick. I don't know why but if I put a print in the run() its starts normally then becomes really fasts and crashes everything. I would really like to know another way of making a repeating timer or the fix of this... Thanks for help in advance.
I have got a Timer Task, which updates all player positions:
public void run(){
updateWatcher = new TimerTask() {
public void run() {
update(0.1);
}
};
t.scheduleAtFixedRate(updateWatcher, 5, 5);
}
And I also have another TimerTask right under that one, which updates all projectile positions:
UpdateWatcher = new TimerTask() { //Different variable. Note the capital U.
public void run() {
for (Bullet b : bullets){
b.update();
}
}
};
t.scheduleAtFixedRate(UpdateWatcher, 5, 5);
}
However, the second TimerTask is slowing down the first one. If I delete the iteration, like this:
UpdateWatcher = new TimerTask() {
public void run() {
}
};
t.scheduleAtFixedRate(UpdateWatcher, 5, 5);
}
The player moves at the correct speed. However, as soon as I re-add the code (using Eclipse de-bugging to add it live), the delayed task executes a lot less often, causing players to be moving more than 10 times slower than usual.
What could be causing this, and how could I fix it?
I'm creating 2 timers in my code. One is the logic timer that updates the logic every 0.017 seconds:
logicTimer = new Timer();
logicTimer.scheduleTask(new Timer.Task() {
#Override
public void run() {
updateLogic();
}
}, 0f, timePerProcessing);
And the other one is for generating obstacles every 3 seconds:
meteoroidTimer = new Timer();
meteoroidTimer.scheduleTask(new Timer.Task() {
#Override
public void run() {
generateMeteoroids();
}
},1f,3f);
When I pause my game and resume afterwards, my logic timer still works but my obstacle timer doesn't. I thought it was because I use a Random object in my method that I call in the timer, but I tried with a simple:
System.out.println("It is showing");
and it still doesn't resume.
My code for pause and resume:
#Override
public void pause(){
meteoroidTimer.stop();
logicTimer.stop();
}
#Override
public void resume(){
meteoroidTimer.start();
logicTimer.start();
}
You dont need to use the timer.stop and star on resume because when you leave the app libgdx stops rendering/updating anyway so your timers wont be updated anyway.
My Goal: Make one script wait .5 seconds, run, make another script wait .5 seconds, run.
The problem I am running into is that Timer.instance().clear(); makes the run() method only run once, on it's own it runs more than once. But, it also deletes Timer2 because it does not wait .5 seconds to run the code after the timer you scheduled (clear() removes all Timers/scheduled tasks). So, it ends up deleting the the Timer2 after .5 seconds.
Edit: Made a dumb error, I was not recognizing how the method was being called. I got it fixed :)
//Timer1
Timer.schedule(new Task(){
#Override
public void run() {
// \/ removes both Timer1 and Timer2
Timer.instance().clear();
}
}, .5f);
//Timer2
Timer.schedule(new Task(){
#Override
public void run() {
Timer.instance().clear();
}
}, 1f);
This works: "Make instance of timers: Timer timer1 = new Timer(); and Timer timer2 = new Timer(); then assign your schedules and code will run only once automatically. Like that: timer1.scheduleTask(....)" – aloupas
i wan't a timer do a job every 2.5 seconds (at the start of the program),
that is working with the following code.
Timer timer = new Timer();
timer.schedule(new TimerTask() {
// #Override
#Override
public void run() {
here is the code, and i do Speed = Speed-500
}, Speed,Speed);
Speed is a int:
public int Speed=2500;
buth the problem is that the speed of the timer stays on the 2500, while the variable speed lowers each time with 500, so that part is working. Only the timer doesn't check if Speed has changed.
Can somebody help me with this?
you cant do that because it will fix that with Timer once you done the schedule.
Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay. Subsequent executions take place at approximately regular intervals separated by the specified period.
In this case you can cancel the previous one and schedule new TimerTask.
Timer timer = new Timer();
initialize the speed here
loop based on time
timer.schedule(new TimerTask() {
// #Override
#Override
public void run() {
here is the code, and i do Speed = Speed-500
}, Speed,Speed);