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)

thanks
javanoob101

Recommended Answers

All 4 Replies

Please show your code. Just telling us that you have a problem is not very helpful.

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)

Hello! Long time no see. I believe the code you are looking for is the setDelay() command for the timer.

Here is the documentation: http://docs.oracle.com/javase/6/docs/api/javax/swing/Timer.html

Also if you wish to have multiple timers for different monsters then set an actionCommand for each timer

Arrrgh!!!!

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.setDelay(speed);
(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
Javanoob101

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.