Hi,
schade, dass mein Grips manchmal nicht dazu ausreicht... Seit einigen Tage komme ich einfach nicht weiter, weil ich ein Beispiel-Code nicht vollständig kapiere:
Dieser Code-Abschnitt macht mir Probleme:
}
}[/code]
Das Buch schreibt zu
Er erhöht es solange, bis animTime größer ist. Kann es aber nicht sein, dass currFrame größer wird, als das ArrayList Indexe hat?
Liebe Grüße
Reality
schade, dass mein Grips manchmal nicht dazu ausreicht... Seit einigen Tage komme ich einfach nicht weiter, weil ich ein Beispiel-Code nicht vollständig kapiere:
Code:
import java.awt.*;
import javax.swing.*;
public class AnimationTest {
public static void main(String[] args) {
DisplayMode displayMode;
if(args.length == 3){
displayMode = new DisplayMode(Integer.parseInt(args[0]),
Integer.parseInt(args[1]),
Integer.parseInt(args[2]),
DisplayMode.REFRESH_RATE_UNKNOWN);
}
else
displayMode = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
AnimationTest test = new AnimationTest();
test.run(displayMode);
}
private static final long DEMO_TIME = 5000;
private SimpleScreenManager screen;
private Image bgImage;
private Animation anim;
public void run(DisplayMode displayMode){
screen = new SimpleScreenManager();
try{
screen.setFullScreen(displayMode, new JFrame());
loadImages();
animationLoop();
}
finally{
screen.restoreScreen();
}
}
public void loadImages(){
bgImage = loadImage("images/background.jpg");
Image player1 = loadImage("images/player1.png");
Image player2 = loadImage("images/player.png");
Image player3 = loadImage("images/player3.png");
//create Animation
anim = new Animation();
anim.addFrames(player1, 250);
anim.addFrames(player2, 150);
anim.addFrames(player1, 150);
anim.addFrames(player2, 150);
anim.addFrames(player3, 200);
anim.addFrames(player2, 150);
}
private Image loadImage(String fileName){
return new ImageIcon(fileName).getImage();
}
public void animationLoop(){
long startTime = System.currentTimeMillis();
long currTime = startTime;
while(currTime - startTime < DEMO_TIME){
long elapsedTime =
System.currentTimeMillis() - currTime;
currTime += elapsedTime;
//update animation
anim.update(elapsedTime);
//draw to screen
Graphics g = screen.getFullScreenWindow().getGraphics();
draw(g);
g.dispose();
//take a nap
try{
Thread.sleep(20);
}catch(InterruptedException ex){}
}
}
public void draw(Graphics g){
//drawbackground
g.drawImage(bgImage, 0, 0, null);
//draw Image
g.drawImage(anim.getImage(), 0, 0, null);
}
}
Code:
import java.awt.*;
import java.util.ArrayList;
public class Animation {
private ArrayList frames;
private int currFrameIndex;
private long animTime;
private long totalDuration;
//Creates a new empty Animation
public Animation(){
frames = new ArrayList();
totalDuration = 0;
start();
}
//Adds an image to the animation with the specified duration
public synchronized void addFrames(Image image, long duration){
totalDuration += duration;
frames.add(new AnimFrame(image, totalDuration));
}
//Starts this animation over from the beginning
public synchronized void start(){
animTime = 0;
currFrameIndex = 0;
}
//Updates this animation´s current image (frame), if necessary
public synchronized void update(long elapsedTime){
if(frames.size() > 1){
animTime += elapsedTime; //elapsed = abgelaufen
if(animTime >= totalDuration){
animTime = animTime % totalDuration;
currFrameIndex = 0;
}
while(animTime > getFrame(currFrameIndex).endTime){
currFrameIndex++;
}
}
}
public synchronized Image getImage(){
if(frames.size() == 0)
return null;
else
return getFrame(currFrameIndex).image;
}
private AnimFrame getFrame(int i){
return (AnimFrame)frames.get(i);
}
private class AnimFrame{
Image image;
long endTime;
public AnimFrame(Image image, long endTime){
this.image = image;
this.endTime = endTime;
}
}
}
Dieser Code-Abschnitt macht mir Probleme:
Code:
public synchronized void update(long elapsedTime){
if(frames.size() > 1){
animTime += elapsedTime; //elapsed = abgelaufen
if(animTime >= totalDuration){
animTime = animTime % totalDuration;
currFrameIndex = 0;
}
[code]
while(animTime > getFrame(currFrameIndex).endTime){
currFrameIndex++;
}
}[/code]
Das Buch schreibt zu
Code:
animTime = animTime % totalDuration;
Ich weiss bis jetzt nicht, was er mit starts over meint und denn Sinn des Codes. Auch das verwirrt mich:It´s used here to make sure the animation time starts over when the animation is done so that the animation loops
Code:
while(animTime > getFrame(currFrameIndex).endTime){
currFrameIndex++;
}
Liebe Grüße
Reality