Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (753)
Games in Android Showcase (228)
games submitted by our members
Games in WIP (842)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  creating an array of images and setting a timer  (Read 1888 times)
0 Members and 1 Guest are viewing this topic.
Offline srayleigh

Junior Newbie

« Posted 2014-04-02 18:06:32 »

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,
Offline ctomni231

JGO Wizard

Medals: 99
Projects: 1
Exp: 7 years

Not a glitch. Just have a lil' pixelexia...

« Reply #1 - Posted 2014-04-02 18:10:02 »

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 Pointing )

Offline trollwarrior1
« Reply #2 - Posted 2014-04-02 18:45:36 »

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.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Phibedy

Senior Devvie

Medals: 9

« Reply #3 - Posted 2014-04-02 20:12:18 »

You should give more information  Smiley

That's the first topic:
Offline matheus23

JGO Kernel

Medals: 138
Projects: 3

You think about my Avatar right now!

« Reply #4 - Posted 2014-04-02 20:23:04 »

// 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... :/

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline srayleigh

Junior Newbie

« Reply #5 - Posted 2014-04-02 21:38:38 »

well yeah it doesnt....its cause im stuck.....
Offline Gibbo3771

JGO Kernel

Medals: 128
Projects: 5
Exp: 1 year

Currently inactive on forums :(

« Reply #6 - Posted 2014-04-03 03:53:39 »

well yeah it doesnt....its cause im stuck.....

Your asking how to code basic Java...

Google Java arrays.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline Rayvolution

« JGO Spiffy Duke »

Medals: 379
Projects: 2
Exp: 2 years

Resident Crazyman

« Reply #7 - Posted 2014-04-03 04:26:21 »

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. Sad

Believe it or not, animation is a fairly complicated subject, even more so for someone who is brand new and doubly so if you're not using something like Lib or Slick.

But, basically what you need to do is make an image array the size of however many frames your image is for every angle you want to animate (IE: walkingRight, walkingLeft, walkingUp, walkingDown), and then load each image into it. Once you do that, you can put a timer in your update loop that swaps out between 1 image and the other. But you also have to have a way to know what direction you're moving/facing to load other images for other directions. Believe it or not, animation is a topic that is very complicated to explain from the bare basics all the way to the end. I highly suggest checking out some coding tutorials before progressing into graphic based games.

These videos go super slow, but will really help you get a foundation going, I suggest watching every single one, then coming back to your project Cheesy <--Total newbie stuff <--more advanced stuff <--Concepts on making a game from scratch <--Start here for more-specific info to your question, probably better explained than I can in text on a forum. Watch this video plus the next few after it and it'll explain the concepts of animation in pure java. <--Intro to using Slick2d (Not recommended since Slick2d is mostly not used anymore)

- Raymond "Rayvolution" Doerr.
Retro-Pixel Castles - Now on Steam!
Offline philfrei
« Reply #8 - Posted 2014-04-07 00:29:59 »

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;
      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()
   class SimpleAnimationTask extends TimerTask
      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.

music and music apps:
Pages: [1]
  ignore  |  Print  

ivj94 (586 views)
2018-03-24 14:47:39

ivj94 (49 views)
2018-03-24 14:46:31

ivj94 (383 views)
2018-03-24 14:43:53

Solater (63 views)
2018-03-17 05:04:08

nelsongames (110 views)
2018-03-05 17:56:34

Gornova (159 views)
2018-03-02 22:15:33

buddyBro (704 views)
2018-02-28 16:59:18

buddyBro (93 views)
2018-02-28 16:45:17

xxMrPHDxx (494 views)
2017-12-31 17:17:51

xxMrPHDxx (734 views)
2017-12-31 17:15:51
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!