how to create an array of 2 images ,and then create a timer that changes a variable that just alternately switches between the two image array? ( for a walk cycle for a 2d game… while player moves to vk_right…)
-sry if the speech is too confuse, i would highly apreciate a code sample and exmaple,
-thanks
:persecutioncomplex:
You mean an animation? Can you tell us what you are using (Java2D, LibGDX, LWJGL, JOGL)?
(Though it doesn’t differ from your last topic question :point: )
I don’t understand… What is this question?
What do you mean an array of images? Just make an array of class image?
Image[] images=new Image[2];
Then somewhere in your game class make a variable out site game loop called updateCounter or something, and every time you update the game, increase its value by 1. Then check if that value is greater than something (15 or whatever the frame change frequency you want) and change the index from which you pick the image.
You should give more information
That’s the first topic: http://www.java-gaming.org/topics/making-walk-cycle-for-2d-game/32641/msg/305638/view.html#msg305638
// Somewhere in initialization, should be accessible:
// Whatever your "Image" implementation is:
Image[] animationFrames = loadMyImages();
int animationFrameTime = 1000; // In milliseconds
Image getAnimationFrame() {
int calculatedFrame = (System.currentTimeMilliseconds() / animationFrameTime) % animationFrames.length;
return animationFrames[calculatedFrame];
}
This is a pretty bad answer for a pretty badly asked question…
well yeah it doesnt…its cause im stuck…
Your asking how to code basic Java…
Google Java arrays.
I refer you back to my original advice in your other thread. Seriously, you can’t rush programming! It takes a lot of time to really get a foundation going, it sounds like you’re at step 3, trying to do step 187.
Disclaimers…
I did not test this. I don’t mean to say that one should do animation this way. I’ll let others talk about what is best.
However, for grins, I thought it would still be interesting to see an implementation that does what was requested.
import java.awt.Graphics;
import java.awt.Image;
import java.util.Timer;
import java.util.TimerTask;
public class SimpleAnimationWithTimer
{
Image[] images = new Image[2];
Timer timer = new Timer();
TimerTask task;
public volatile int stage;
SimpleAnimationWithTimer()
{
task = new SimpleAnimationTask();
}
public void draw(Graphics g, int xLoc, int yLoc)
{
g.drawImage(images[stage], xLoc, yLoc, null);
}
public void loadImages(String[] imagePaths)
{
// to be written
}
public void startWalkAnimation()
{
timer.schedule(task, 0, 500); // two steps per second
}
public void stopWalkAnimation()
{
timer.cancel();
}
class SimpleAnimationTask extends TimerTask
{
@Override
public void run()
{
stage ++;
stage %= 2; // mod div by two keeps it in range 0, 1
}
}
}
Once the class has been initialized and loaded with the two images, to animate it, you would call the startWalkAnimation() method. The timer local to that object would then flip the state at whatever rate you specify. (I put in 2 changes per second == typical marching tempo.)
The code controlling the main screen would display your pair of images by passing its Graphic object to the draw() method. These draw() calls would be made 60 times per second or at whatever animation rate you are using. For clarity’s sake, I put the management of the position of the animation external to its class. Usually if something moves around on the display, you’d put the xLoc & yLoc and movement coding within the object as well, instead of passing the location via the draw() method as done here.
Having a personal Timer is not needed, though. You could also determine the state by a function that is called at the time of display, such as either a calculation based on the current time, or on a count of elapsed frames.
The main issue is how the thread overhead compares to the cost of a method that has to be consulted on a per frame basis. I can see that there might be some appeal to having the code that performs the flip only run a couple times in a given second vs checking whether to flip or not on every frame. Maybe this isn’t a terrible way to do the task.
Some folks might object to using the util timer, due to having to misplaced fears about multi-threading. But the very simple and confined usage here shouldn’t cause any problems. The actions are background, not part of the rendering process. The use of the volatile keyword helps keep the threads properly aligned.