Hello everyone,

I am using a timer to control the speed of the monsters in my game, however I can't change the delay of the timer because i'm using

Timer animation = new Timer (speed, this);

to make a timer...

How can I change the speed (speed is an int in the code) ?
(I can change the speed, but the timer still uses the default speed, not the new values I set via keyboard)


3 Years
Discussion Span
Last Post by javanoob101

You will need to pass the variables to the int(speed) of your Timer animation in the ActionListener of your Timer class. Your timer class will also need to implement KeyListener.
The Timer class has a setDelay method. setDelay() will not affect setInitialDelay(). So if you want your monster to begin moving at startup or when the event is fired you will set this up in your setInitialDelay() After the event is fired then you can use setDelay to control the monsters speed(Delay between the firing of your events such as keyPressed in your example)

Edited by Banderson



I can't belive that I missed the setDelay(); command in the java class documents from Oracle!

Anyway, It turns out that all I needed to do was use this in my code:
(animation is the name of my timer, speed is the integer that I want to control)

This tells my timer that the delay is the variable speed.

Thanks Banderson for the setDelay(); command i forgot, Rubberman I apologize for not posting my code, I was in college at the time...

And Sirlink99... Thanks for the help with my program again and the link to the Oracle documents! The program still has a ton of bugs in it (just like in high school) and I finally figured out how to fix the bullet and add music =)

Thanks again guys

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.