Java game development tutorials: Animation... HELP

0 Antonio Exposito · July 29, 2015
Can anyone tell me why the following code is incorrect? It's from java game development tutorials 8-10:

import java.awt.Image;
import java.util.ArrayList;

public class Cat {
    private ArrayList scenes;
    private int sceneIndex;
    private long movieTime;
    private long totalTime;
    
    //CONSTRUCTOR
    private Cat(){
    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 scene
    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;
         }
    }
    
    //getScene
    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;
    }
    }

Post a Reply

Replies

Oldest  Newest  Rating
0 Henry Kato · July 31, 2015
Hi Guys, I am a complete newbie both on this forum and in the java development world. However I have been contacted by a bunch of High School Head teachers who are interested in a virtual lab software application for physics, chemistry and Biology. At first I thought it will be approached like a game where the characters are the different lab equipment, but I am really not sure. How should I approach this, and how best can I get started?
+2 Rashad Kadado · July 29, 2015
You just have some minor errors that need to be fixed. Your errors are in the "getScene()" method and the private inner class "OneScene". In the getScene method you need to capitalize the o in "oneScene" because you are casting it and you called the class "OneScene". Also when you create that inner class you do not put parenthesis there. So instead of this:

private class OneScene(){ 
        Image pic;
        long endTime;

        public OneScene(Image pic, long endTime){
        this.pic = pic;
        this.endTime = endTime;
}
}
//It should be like this. No parenthesis when creating classes
private class OneScene{
Image pic;
        long endTime;

        public OneScene(Image pic, long endTime){
        this.pic = pic;
        this.endTime = endTime;
}
}

Those are the only reasons your code wasn't working, and it should be fine after that.
  • 1

Java / Android Development

106,967 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator