I'm learning how to use java and am follow thenewboston's java game development tutorials.
I've just learn how to add scenes to the animation and switch between them, with each scene lasting a set amount of time. However, i want to make it so each scene lasts for a random amount of time.
For example...
The first time you open the program this happens:
Start program,
Face with eyes open - 2.7 seconds,
Face with eyes closed - 0.5 seconds.
Face with eyes open - 3.3 seconds,
Face with eyes closed - 1.2 seconds.
Face with eyes open - 2.7 seconds,
Program closes.
And when you open the same program a second time without changing the code this happens:
Start program,
Face with eyes open - 3.4 seconds,
Face with eyes closed - 0.4 seconds.
Face with eyes open - 2.8 seconds,
Face with eyes closed - 1.0 seconds.
Face with eyes open - 3.1 seconds,
Program closes.
As you can see the length of time each scene appears for is randomised.
However, the code the tutorial shows you how to do makes it so each scene time is put into the main method and won't allow me to randomise it.
Here is the main class:
import java.awt.*; import javax.swing.ImageIcon; import javax.swing.JFrame; public class Ravi { public static void main (String args[]){ Ravi r = new Ravi(); r.run(); } private Animation a; private ScreenManager s; private Image bg; private static final DisplayMode modes1[] = { new DisplayMode (800,600,32,0), new DisplayMode (800,600,24,0), new DisplayMode (800,600,16,0), new DisplayMode (640,480,32,0), new DisplayMode (640,480,24,0), new DisplayMode (640,480,16,0), }; //Load images and add scenes public void loadImages(){ bg = new ImageIcon("C:\\test\\back.jpg").getImage(); Image face1 = new ImageIcon("C:\\test\\Animation1.png") .getImage(); Image face2 = new ImageIcon("C:\\test\\Animation2.png") .getImage(); a = new Animation(); a.addScene(face1, 200); a.addScene(face2, 200); } //main method called form main public void run(){ s = new ScreenManager(); try{ DisplayMode dm = s.findFirstCompatibleMode(modes1); s.setFullScreen(dm); loadImages(); movieLoop(); }finally{ s.restoreScreen(); } } //play movie public void movieLoop(){ long startingTime = System.currentTimeMillis(); long cumTime = startingTime; while(cumTime - startingTime <10000){ long timePassed = System.currentTimeMillis() - cumTime; cumTime += timePassed; a.update(timePassed); //draw and update screen Graphics2D g = s.getGraphics(); draw(g); g.dispose(); s.update(); try{ Thread.sleep(20); }catch(Exception ex){} } } //draws graphics public void draw(Graphics g){ g.drawImage(bg, 0, 0, null); g.drawImage(a.getImage(), 350, 250, null); } }
And here is the Animation class.
import java.awt.Image; import java.util.ArrayList; public class Animation { private ArrayList scenes; private int sceneIndex; private long movieTime; private long totalTime; //CONSTRUCTOR public Animation (){ scenes = new ArrayList(); totalTime = 0; start(); } //add scene to ArrayList and set time for each scene public synchronized void addScene(Image i, long t){ totalTime += t; scenes.add(new OneScene(i, totalTime)); } //start animation from beginning public synchronized void start(){ movieTime = 0; sceneIndex = 0; } //change scenes public synchronized void update(long timePassed){ if(scenes.size()>1){ movieTime += timePassed; if(movieTime >= totalTime){ movieTime = 0; sceneIndex = 0; } while(movieTime > getScene(sceneIndex).endTime){ sceneIndex++; } } } //get animations current scene (aka image) public synchronized Image getImage(){ if(scenes.size()==0){ return null; }else{ return getScene(sceneIndex).pic; } } //get scene private OneScene getScene(int x){ return (OneScene)scenes.get(x); } //////// PRIVATE INNER CLASS //////// private class OneScene{ Image pic; long endTime; public OneScene(Image pic, long endTime) { this.pic = pic; this.endTime = endTime; } } }
here is the last episode of thenewboston's animation tutorials:
I'm not sure where to put the random number generator to make it effect the scene time and change it every time it cycles through.
Any help would be greatly appreciated!
Vaderico