Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
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 511 times)
0 Members and 1 Guest are viewing this topic.
Offline srayleigh

Junior Newbie





« Posted 2014-04-02 20: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,
-thanks
 persecutioncomplex
Offline ctomni231

JGO Wizard


Medals: 98
Projects: 1
Exp: 7 years


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


« Reply #1 - Posted 2014-04-02 20: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 20: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?

1  
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 Member


Medals: 8



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

You should give more information  Smiley

That's the first topic: http://www.java-gaming.org/topics/making-walk-cycle-for-2d-game/32641/msg/305638/view.html#msg305638
Offline matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


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

1  
2  
3  
4  
5  
6  
7  
8  
9  
// 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 23:38:38 »

well yeah it doesnt....its cause im stuck.....
Offline Gibbo3771
« Reply #6 - Posted 2014-04-03 05: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 Kernel


Medals: 142
Projects: 2
Exp: 1 year


Resident Crazyman


« Reply #7 - Posted 2014-04-03 06: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
http://thenewboston.org/list.php?cat=31 <--Total newbie stuff

http://thenewboston.org/list.php?cat=25 <--more advanced stuff

http://thenewboston.org/list.php?cat=30 <--Concepts on making a game from scratch
http://thenewboston.org/watch.php?cat=30&number=7 <--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.

http://thenewboston.org/list.php?cat=54 <--Intro to using Slick2d (Not recommended since Slick2d is mostly not used anymore)

- Raymond "Rayvolution" Doerr.
Retro-Pixel Castles - Survival Sim/Builder/Roguelike!
LIVE-STREAMING DEVELOPMENT: http://www.twitch.tv/SG_Rayvolution
Offline philfrei
« Reply #8 - Posted 2014-04-07 02:29:59 »

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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
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.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (11 views)
2014-07-24 01:59:36

Riven (10 views)
2014-07-23 21:16:32

Riven (11 views)
2014-07-23 21:07:15

Riven (12 views)
2014-07-23 20:56:16

ctomni231 (42 views)
2014-07-18 06:55:21

Zero Volt (38 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (34 views)
2014-07-16 23:30:00

Cero (50 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org 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‑gaming.org
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!