Java, Help cloning 2d animations

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 1323 Views - Last Post: 05 June 2010 - 04:43 PM Rate Topic: -----

#1 Guest_cjWildsmith*


Reputation:

Java, Help cloning 2d animations

Posted 04 June 2010 - 12:12 AM

Hello, I'm three weeks into learning Java btw, and I'm not too familiar with the language. Our teacher has assigned a project to make a 2d scolling videogame.
Currently I'm having issues working with a framework I have been given, and getting additional animation to work.

I have a creature class which my player and enemies inherit from, which has a constructor that takes four animations.
like so.
public Creature(Animation left, Animation right,
        Animation deadLeft, Animation deadRight)
    {
        super(right);
        this.left = left;
        this.right = right;
        this.deadLeft = deadLeft;
        this.deadRight = deadRight;
        
        state = STATE_NORMAL;
    }



The creature class then (from what I understand) creates a constructor and clones the animation into that new constructor???
public Object clone() {
        // use reflection to create the correct subclass
        Constructor constructor = getClass().getConstructors()[0];
        
        try {
            return constructor.newInstance(new Object[] {
                (Animation)left.clone(),
                (Animation)right.clone(),
                (Animation)deadLeft.clone(),
                (Animation)deadRight.clone(),
                
            });
        }
        catch (Exception ex) {
            // should never happen
            ex.printStackTrace();
            return null;
        }
    }


Then Creature has an update method which constantly checks to see if the animation should be played based on the players states
public void update(long elapsedTime) {
        // select the correct Animation
        setNewAnim(anim);
        if (getVelocityX() < 0) {
            setNewAnim(left);
        }
        else if (getVelocityX() > 0) {
            setNewAnim(right);
        }
        if (state == STATE_DYING && newAnim == left) {
            setNewAnim(deadLeft);
        }
        else if (state == STATE_DYING && newAnim == right) {
            setNewAnim(deadRight);
        }
        

        // update the Animation
        if (anim != getNewAnim()) {
            anim = getNewAnim();
            anim.start();
        }
        else {
            anim.update(elapsedTime);
        }

        // update to "dead" state
        stateTime += elapsedTime;
        if (state == STATE_DYING && stateTime >= DIE_TIME) {
            setState(STATE_DEAD);
        }
    }



The player class which inherrits creature then uses the creatures constructor, I wanted to add my animations to that constructor and change creatures. but my teacher said no, so then I suggested creating an overloaded constructor in player, and again he said no. So I suggested creating a method in side of player which takes two animations for jumping and then he said yes do that. So I've done that like so.

public void addJumpAnim(Animation jumpLeft,Animation jumpRight){
    	this.jumpLeft = jumpLeft;
    	this.jumpRight = jumpRight;
    }



then in players update goes like this.

public void update(long elapsedTime)
    {
    	super.update(elapsedTime);
    	
    	
    	setNewAnim(super.anim);
    	if(JUMP_STATE &&  getNewAnim() == left){
    		setNewAnim(jumpLeft);
    	}
    	else if(JUMP_STATE  && getNewAnim() == right){
    		setNewAnim(jumpRight);
    	}
    	
    	
    	if (super.anim != getNewAnim()) {
            super.anim = getNewAnim();
            anim.start();
        }
        else {
            super.anim.update(elapsedTime);
        }
     	stateTime += elapsedTime;
    	if(JUMP_STATE && onGround){
    		setState(STATE_NORMAL);
    		
    	}
    	
    }



Now the way the animations are loaded are onto a tile map and they need a clone like in creature. But I would need to clone the method addJumpAnim(Animation, Animation), and I don't know how. The clone in creature, clones a constructor(I think correct me if I'm wrong) and takes the creature constructors arguments.

If I don't clone in the player which I'm not right now, I get a java.lang.NullPointerException. (If I don't have any jump animations, and don't create an update in player then it works fine, but I need my jump animation)

Any Help would be really appreciated. If anyone is willing to take my project and maybe look at it as well, That would be great.

Is This A Good Question/Topic? 0

Replies To: Java, Help cloning 2d animations

#2 Guest_cjWildsmith*


Reputation:

Re: Java, Help cloning 2d animations

Posted 04 June 2010 - 03:22 AM

Hello, I'm three weeks into learning Java btw, and I'm not too familiar with the language. Our teacher has assigned a project to make a 2d scolling videogame.
Currently I'm having issues working with a framework I have been given, and getting additional animation to work.

I have a creature class which my player and enemies inherit from, which has a constructor that takes four animations.
like so.
public Creature(Animation left, Animation right,
        Animation deadLeft, Animation deadRight)
    {
        super(right);
        this.left = left;
        this.right = right;
        this.deadLeft = deadLeft;
        this.deadRight = deadRight;
        
        state = STATE_NORMAL;
    }




The creature class then (from what I understand) creates a constructor and clones the animation into that new constructor???
public Object clone() {
        // use reflection to create the correct subclass
        Constructor constructor = getClass().getConstructors()[0];
        
        try {
            return constructor.newInstance(new Object[] {
                (Animation)left.clone(),
                (Animation)right.clone(),
                (Animation)deadLeft.clone(),
                (Animation)deadRight.clone(),
                
            });
        }
        catch (Exception ex) {
            // should never happen
            ex.printStackTrace();
            return null;
        }
    }



Then Creature has an update method which constantly checks to see if the animation should be played based on the players states
public void update(long elapsedTime) {
        // select the correct Animation
        setNewAnim(anim);
        if (getVelocityX() < 0) {
            setNewAnim(left);
        }
        else if (getVelocityX() > 0) {
            setNewAnim(right);
        }
        if (state == STATE_DYING && newAnim == left) {
            setNewAnim(deadLeft);
        }
        else if (state == STATE_DYING && newAnim == right) {
            setNewAnim(deadRight);
        }
        

        // update the Animation
        if (anim != getNewAnim()) {
            anim = getNewAnim();
            anim.start();
        }
        else {
            anim.update(elapsedTime);
        }

        // update to "dead" state
        stateTime += elapsedTime;
        if (state == STATE_DYING && stateTime >= DIE_TIME) {
            setState(STATE_DEAD);
        }
    }




The player class which inherrits creature then uses the creatures constructor, I wanted to add my animations to that constructor and change creatures. but my teacher said no, so then I suggested creating an overloaded constructor in player, and again he said no. So I suggested creating a method in side of player which takes two animations for jumping and then he said yes do that. So I've done that like so.
public void addJumpAnim(Animation jumpLeft,Animation jumpRight){
        this.jumpLeft = jumpLeft;
        this.jumpRight = jumpRight;
    }




then in players update goes like this.
public void update(long elapsedTime)
    {
        super.update(elapsedTime);
        
        
        setNewAnim(super.anim);
        if(JUMP_STATE &&  getNewAnim() == left){
                setNewAnim(jumpLeft);
        }
        else if(JUMP_STATE  && getNewAnim() == right){
                setNewAnim(jumpRight);
        }
        
        
        if (super.anim != getNewAnim()) {
            super.anim = getNewAnim();
            anim.start();
        }
        else {
            super.anim.update(elapsedTime);
        }
        stateTime += elapsedTime;
        if(JUMP_STATE && onGround){
                setState(STATE_NORMAL);
                
        }
        
    }




Now the way the animations are loaded are onto a tile map and they need a clone like in creature. But I would need to clone the method addJumpAnim(Animation, Animation), and I don't know how. The clone in creature, clones a constructor(I think correct me if I'm wrong) and takes the creature constructors arguments.

If I don't clone in the player which I'm not right now, I get a java.lang.NullPointerException. (If I don't have any jump animations, and don't create an update in player then it works fine, but I need my jump animation)

Any Help would be really appreciated. If anyone is willing to take my project and maybe look at it as well, That would be great.
Was This Post Helpful? 0

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10185
  • View blog
  • Posts: 37,600
  • Joined: 27-December 08

Re: Java, Help cloning 2d animations

Posted 04 June 2010 - 05:22 PM

Quote

Now the way the animations are loaded are onto a tile map and they need a clone like in creature. But I would need to clone the method addJumpAnim(Animation, Animation), and I don't know how. The clone in creature, clones a constructor(I think correct me if I'm wrong) and takes the creature constructors arguments.

You really shouldn't be cloning a Method. Yes- there is the reflection Method class, but I think for a newbie, you are trying to use way too much reflection here. Java Reflection is a pretty thick topic, and one you really don't want to get into if you have only been working in Java for three weeks.

Actually, the clone() method clones the current Object, not the constructor. Can you explain more specifically why you need a clone() here? Good OO Practice here would be to have the Tile class encapsulate a Creature object, and have getter/setter methods for Creature. Then when the animation goes to move the creature, you use the getter/setter methods to place the Creature onto the new Tiles.
Was This Post Helpful? 0
  • +
  • -

#4 Guest_cjWildsmith*


Reputation:

Re: Java, Help cloning 2d animations

Posted 04 June 2010 - 06:08 PM

well creature and pretty much anything that moves, inherrits from a Sprite class. sprites in my game update there own animations, so sprites can't share the same Animation object; the Animation object would get updated too many times. If I'm using alot of Animations, that are the same, I add a clone() method to the animation to make them easier to duplicate.

I understand there are a lot better ways of doing this. But I've been instructed to do so this way.

Also, I'm picking Java up pretty quickly. I'm not saying I know it all, I know a bit of c++ and AS3, and Java in terms of syntax and what have you seems like a meld of the two.
Was This Post Helpful? 0

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10185
  • View blog
  • Posts: 37,600
  • Joined: 27-December 08

Re: Java, Help cloning 2d animations

Posted 04 June 2010 - 06:47 PM

I'm guessing Animation probably encapsulates AffineTransform, so it updates position, direction, and rotation. I don't see any reason why you couldn't just create a new Animation object and update it with the attributes of the existing Animation object via the getter and setter methods.

If you want to use clone(), one- you might as well implement Cloneable as it is a marker interface (meaning Animation instanceof Cloneable), and two- you probably will want to use my above suggestion rather than using Reflection.
Was This Post Helpful? 0
  • +
  • -

#6 Guest_cjWildsmith*


Reputation:

Re: Java, Help cloning 2d animations

Posted 04 June 2010 - 07:52 PM

I would have done this whole project completely different dude. But I'm working with a framework and restrictions. Meaning certain things have to be done a certain way. Just like this cloning object.

I'm am definatly taking your advice for future reference but right now, I need to now how to do what I did with the clone() in creature, and implement it to the player class using the addJumpAnim method I have in player. I don't know what methods Java has to do that or how to implement them.
Was This Post Helpful? 0

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10185
  • View blog
  • Posts: 37,600
  • Joined: 27-December 08

Re: Java, Help cloning 2d animations

Posted 04 June 2010 - 07:58 PM

I completely understand the frustrations of working within your teacher's terrible code. Been there, done that. :) However, I'm not exactly clear on what you are/aren't allowed to do based on my suggestions.

Quote

I'm am definatly taking your advice for future reference but right now, I need to now how to do what I did with the clone() in creature, and implement it to the player class using the addJumpAnim method I have in player. I don't know what methods Java has to do that or how to implement them.

Using Reflection to create a new Object is one thing; however, cloning a method isn't a good idea. If you can get the Animations from player1 and clone them as I described in my last post, you can use the addJumpAnim() method to pass the new cloned Animations to player2.
Was This Post Helpful? 0
  • +
  • -

#8 Guest_cjWildsmith*


Reputation:

Re: Java, Help cloning 2d animations

Posted 04 June 2010 - 08:16 PM

I'm starting to think I'm a little over my head with this. Would it be possible to show you the project I'm working on, and send it to you. I think this whole cloning thing has me lost. And would you mind if I sent it to you with the error I'm having?
Was This Post Helpful? 0

#9 Guest_cjWildsmith*


Reputation:

Re: Java, Help cloning 2d animations

Posted 04 June 2010 - 08:18 PM

add I'll send it with as many comments I can do.
Was This Post Helpful? 0

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10185
  • View blog
  • Posts: 37,600
  • Joined: 27-December 08

Re: Java, Help cloning 2d animations

Posted 04 June 2010 - 08:21 PM

If by send, you mean post in this thread, then feel free. :)

Remember to properly indent and space your code, as well as use code tags, like so: :code:.
Was This Post Helpful? 0
  • +
  • -

#11 Guest_cjWildsmith*


Reputation:

Re: Java, Help cloning 2d animations

Posted 04 June 2010 - 08:54 PM

there's quite a bit of code but here we go.

Animation class
package com.brackeen.javagamebook.graphics;

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

/**
    The Animation class manages a series of images (frames) and
    the amount of time to display each frame.
*/
public class Animation {

    private ArrayList frames;
    private int currFrameIndex;
    private long animTime;
    private long totalDuration;


    /**
        Creates a new, empty Animation.
    */
    public Animation() {
        this(new ArrayList(), 0);
    }


    private Animation(ArrayList frames, long totalDuration) {
        this.frames = frames;
        this.totalDuration = totalDuration;
        start();
    }


    /**
        Creates a duplicate of this animation. The list of frames
        are shared between the two Animations, but each Animation
        can be animated independently.
    */
    public Object clone() {
        return new Animation(frames, totalDuration);
    }


    /**
        Adds an image to the animation with the specified
        duration (time to display the image).
    */
    public synchronized void addFrame(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
        neccesary.
    */
    public synchronized void update(long elapsedTime) {
        if (frames.size() > 1) {
            animTime += elapsedTime;

            if (animTime >= totalDuration) {
                animTime = animTime % totalDuration;
                currFrameIndex = 0;
            }

            while (animTime > getFrame(currFrameIndex).endTime) {
                currFrameIndex++;
            }
        }
    }


    /**
        Gets this Animation's current image. Returns null if this
        animation has no images.
    */
    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;
        }
    }
}




sprite class
package com.brackeen.javagamebook.graphics;

import java.awt.Image;

public class Sprite {

    protected Animation anim;
    // position (pixels)
    private float x;
    private float y;
    // velocity (pixels per millisecond)
    private float dx;
    private float dy;

    /**
        Creates a new Sprite object with the specified Animation.
    */
    public Sprite(Animation anim) {
        this.anim = anim;
    }

    /**
        Updates this Sprite's Animation and its position based
        on the velocity.
    */
    public void update(long elapsedTime) {
        x += dx * elapsedTime;
        y += dy * elapsedTime;
        anim.update(elapsedTime);
    }

    /**
        Gets this Sprite's current x position.
    */
    public float getX() {
        return x;
    }

    /**
        Gets this Sprite's current y position.
    */
    public float getY() {
        return y;
    }

    /**
        Sets this Sprite's current x position.
    */
    public void setX(float x) {
        this.x = x;
    }

    /**
        Sets this Sprite's current y position.
    */
    public void setY(float y) {
        this.y = y;
    }

    /**
        Gets this Sprite's width, based on the size of the
        current image.
    */
    public int getWidth() {
        return anim.getImage().getWidth(null);
    }

    /**
        Gets this Sprite's height, based on the size of the
        current image.
    */
    public int getHeight() {
        return anim.getImage().getHeight(null);
    }

    /**
        Gets the horizontal velocity of this Sprite in pixels
        per millisecond.
    */
    public float getVelocityX() {
        return dx;
    }

    /**
        Gets the vertical velocity of this Sprite in pixels
        per millisecond.
    */
    public float getVelocityY() {
        return dy;
    }

    /**
        Sets the horizontal velocity of this Sprite in pixels
        per millisecond.
    */
    public void setVelocityX(float dx) {
        this.dx = dx;
    }

    /**
        Sets the vertical velocity of this Sprite in pixels
        per millisecond.
    */
    public void setVelocityY(float dy) {
        this.dy = dy;
    }

    /**
        Gets this Sprite's current image.
    */
    public Image getImage() {
        return anim.getImage();
    }

    /**
        Clones this Sprite. Does not clone position or velocity
        info.
    */
    public Object clone() {
        return new Sprite(anim);
    }
}



Game manager
package com.brackeen.javagamebook.tilegame;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.Iterator;

import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
import javax.sound.sampled.AudioFormat;

import com.brackeen.javagamebook.graphics.*;
import com.brackeen.javagamebook.sound.*;
import com.brackeen.javagamebook.input.*;
import com.brackeen.javagamebook.test.GameCore;
import com.brackeen.javagamebook.tilegame.sprites.*;

/**
    GameManager manages all parts of the game.
*/
public class GameManager extends GameCore {

    public static void main(String[] args) {
        new GameManager().run();
    }

    // uncompressed, 44100Hz, 16-bit, mono, signed, little-endian
    private static final AudioFormat PLAYBACK_FORMAT =
        new AudioFormat(44100, 16, 1, true, false);

    private static final int DRUM_TRACK = 1;

    public static final float GRAVITY = 0.002f;

    private Point pointCache = new Point();
    private TileMap map;
    private MidiPlayer midiPlayer;
    private SoundManager soundManager;
    private ResourceManager resourceManager;
    private Sound prizeSound;
    private Sound boopSound;
    private InputManager inputManager;
    private TileMapRenderer renderer;

    private GameAction moveLeft;
    private GameAction moveRight;
    private GameAction jump;
    private GameAction exit;


    public void init() {
        super.init();

        // set up input manager
        initInput();

        // start resource manager
        resourceManager = new ResourceManager(
        screen.getFullScreenWindow().getGraphicsConfiguration());

        // load resources
        renderer = new TileMapRenderer();
        renderer.setBackground(
            resourceManager.loadImage("background.png"));

        // load first map
        map = resourceManager.loadNextMap();

        // load sounds
        soundManager = new SoundManager(PLAYBACK_FORMAT);
        prizeSound = soundManager.getSound("sounds/prize.wav");
        boopSound = soundManager.getSound("sounds/boop2.wav");

        // start music
        midiPlayer = new MidiPlayer();
        Sequence sequence =
            midiPlayer.getSequence("sounds/music.midi");
        midiPlayer.play(sequence, true);
        toggleDrumPlayback();
    }


    /**
        Closes any resurces used by the GameManager.
    */
    public void stop() {
        super.stop();
        midiPlayer.close();
        soundManager.close();
    }


    private void initInput() {
        moveLeft = new GameAction("moveLeft");
        moveRight = new GameAction("moveRight");
        jump = new GameAction("jump", 
        	GameAction.DETECT_INITAL_PRESS_ONLY);
        exit = new GameAction("exit",
            GameAction.DETECT_INITAL_PRESS_ONLY);

        inputManager = new InputManager(
            screen.getFullScreenWindow());
        inputManager.setCursor(InputManager.INVISIBLE_CURSOR);

        inputManager.mapToKey(moveLeft, KeyEvent.VK_LEFT);
        inputManager.mapToKey(moveRight, KeyEvent.VK_RIGHT);
        inputManager.mapToKey(jump, KeyEvent.VK_SPACE);
        inputManager.mapToKey(exit, KeyEvent.VK_ESCAPE);
    }


    private void checkInput(long elapsedTime) {

        if (exit.isPressed()) {
            stop();
        }

        Player player = (Player)map.getPlayer();
        if (player.isAlive()) {
            float velocityX = 0;
            if (moveLeft.isPressed()) {
                velocityX-=player.getMaxSpeed();
            }
            if (moveRight.isPressed()) {
                velocityX+=player.getMaxSpeed();
            }
            if (jump.isPressed()) {
                player.jump(false);
                
                
            }
            player.setVelocityX(velocityX);
        }

    }


    public void draw(Graphics2D g) {
        renderer.draw(g, map,
            screen.getWidth(), screen.getHeight());
    }


    /**
        Gets the current map.
    */
    public TileMap getMap() {
        return map;
    }


    /**
        Turns on/off drum playback in the midi music (track 1).
    */
    public void toggleDrumPlayback() {
        Sequencer sequencer = midiPlayer.getSequencer();
        if (sequencer != null) {
            sequencer.setTrackMute(DRUM_TRACK,
                !sequencer.getTrackMute(DRUM_TRACK));
        }
    }


    /**
        Gets the tile that a Sprites collides with. Only the
        Sprite's X or Y should be changed, not both. Returns null
        if no collision is detected.
    */
    public Point getTileCollision(Sprite sprite,
        float newX, float newY)
    {
        float fromX = Math.min(sprite.getX(), newX);
        float fromY = Math.min(sprite.getY(), newY);
        float toX = Math.max(sprite.getX(), newX);
        float toY = Math.max(sprite.getY(), newY);

        // get the tile locations
        int fromTileX = TileMapRenderer.pixelsToTiles(fromX);
        int fromTileY = TileMapRenderer.pixelsToTiles(fromY);
        int toTileX = TileMapRenderer.pixelsToTiles(
            toX + sprite.getWidth() - 1);
        int toTileY = TileMapRenderer.pixelsToTiles(
            toY + sprite.getHeight() - 1);

        // check each tile for a collision
        for (int x=fromTileX; x<=toTileX; x++) {
            for (int y=fromTileY; y<=toTileY; y++) {
                if (x < 0 || x >= map.getWidth() ||
                    map.getTile(x, y) != null)
                {
                    // collision found, return the tile
                    pointCache.setLocation(x, y);
                    return pointCache;
                }
            }
        }

        // no collision found
        return null;
    }


    /**
        Checks if two Sprites collide with one another. Returns
        false if the two Sprites are the same. Returns false if
        one of the Sprites is a Creature that is not alive.
    */
    public boolean isCollision(Sprite s1, Sprite s2) {
        // if the Sprites are the same, return false
        if (s1 == s2) {
            return false;
        }

        // if one of the Sprites is a dead Creature, return false
        if (s1 instanceof Creature && !((Creature)s1).isAlive()) {
            return false;
        }
        if (s2 instanceof Creature && !((Creature)s2).isAlive()) {
            return false;
        }

        // get the pixel location of the Sprites
        int s1x = Math.round(s1.getX());
        int s1y = Math.round(s1.getY());
        int s2x = Math.round(s2.getX());
        int s2y = Math.round(s2.getY());

        // check if the two sprites' boundaries intersect
        return (s1x < s2x + s2.getWidth() &&
            s2x < s1x + s1.getWidth() &&
            s1y < s2y + s2.getHeight() &&
            s2y < s1y + s1.getHeight());
    }


    /**
        Gets the Sprite that collides with the specified Sprite,
        or null if no Sprite collides with the specified Sprite.
    */
    public Sprite getSpriteCollision(Sprite sprite) {

        // run through the list of Sprites
        Iterator i = map.getSprites();
        while (i.hasNext()) {
            Sprite otherSprite = (Sprite)i.next();
            if (isCollision(sprite, otherSprite)) {
                // collision found, return the Sprite
                return otherSprite;
            }
        }

        // no collision found
        return null;
    }


    /**
        Updates Animation, position, and velocity of all Sprites
        in the current map.
    */
    public void update(long elapsedTime) {
        Creature player = (Creature)map.getPlayer();


        // player is dead! start map over
        if (player.getState() == Creature.STATE_DEAD) {
            map = resourceManager.reloadMap();
            return;
        }

        // get keyboard/mouse input
        checkInput(elapsedTime);

        // update player
        updateCreature(player, elapsedTime);
        player.update(elapsedTime);

        // update other sprites
        Iterator i = map.getSprites();
        while (i.hasNext()) {
            Sprite sprite = (Sprite)i.next();
            if (sprite instanceof Creature) {
                Creature creature = (Creature)sprite;
                if (creature.getState() == Creature.STATE_DEAD) {
                    i.remove();
                }
                else {
                    updateCreature(creature, elapsedTime);
                }
            }
            // normal update
            sprite.update(elapsedTime);
        }
    }


    /**
        Updates the creature, applying gravity for creatures that
        aren't flying, and checks collisions.
    */
    private void updateCreature(Creature creature,
        long elapsedTime)
    {

        // apply gravity
        if (!creature.isFlying()) {
            creature.setVelocityY(creature.getVelocityY() +
                GRAVITY * elapsedTime);
        }

        // change x
        float dx = creature.getVelocityX();
        float oldX = creature.getX();
        float newX = oldX + dx * elapsedTime;
        Point tile =
            getTileCollision(creature, newX, creature.getY());
        if (tile == null) {
            creature.setX(newX);
        }
        else {
            // line up with the tile boundary
            if (dx > 0) {
                creature.setX(
                    TileMapRenderer.tilesToPixels(tile.x) -
                    creature.getWidth());
            }
            else if (dx < 0) {
                creature.setX(
                    TileMapRenderer.tilesToPixels(tile.x + 1));
            }
            creature.collideHorizontal();
        }
        if (creature instanceof Player) {
            checkPlayerCollision((Player)creature, false);
        }

        // change y
        float dy = creature.getVelocityY();
        float oldY = creature.getY();
        float newY = oldY + dy * elapsedTime;
        tile = getTileCollision(creature, creature.getX(), newY);
        if (tile == null) {
            creature.setY(newY);
        }
        else {
            // line up with the tile boundary
            if (dy > 0) {
                creature.setY(
                    TileMapRenderer.tilesToPixels(tile.y) -
                    creature.getHeight());
            }
            else if (dy < 0) {
                creature.setY(
                    TileMapRenderer.tilesToPixels(tile.y + 1));
            }
            creature.collideVertical();
        }
        if (creature instanceof Player) {
            boolean canKill = (oldY < creature.getY());
            checkPlayerCollision((Player)creature, canKill);
        }

    }


    /**
        Checks for Player collision with other Sprites. If
        canKill is true, collisions with Creatures will kill
        them.
    */
    public void checkPlayerCollision(Player player,
        boolean canKill)
    {
        if (!player.isAlive()) {
            return;
        }

        // check for player collision with other sprites
        Sprite collisionSprite = getSpriteCollision(player);
        if (collisionSprite instanceof PowerUp) {
            acquirePowerUp((PowerUp)collisionSprite);
        }
        else if (collisionSprite instanceof Creature) {
            Creature badguy = (Creature)collisionSprite;
            if (canKill) {
                // kill the badguy and make player bounce
                soundManager.play(boopSound);
                badguy.setState(Creature.STATE_DYING);
                player.setY(badguy.getY() - player.getHeight());
                player.jump(true);
            }
            else {
                // player dies!
                player.setState(Creature.STATE_DYING);
            }
        }
    }


    /**
        Gives the player the speicifed power up and removes it
        from the map.
    */
    public void acquirePowerUp(PowerUp powerUp) {
        // remove it from the map
        map.removeSprite(powerUp);

        if (powerUp instanceof PowerUp.Star) {
            // do something here, like give the player points
            soundManager.play(prizeSound);
        }
        else if (powerUp instanceof PowerUp.Music) {
            // change the music
            soundManager.play(prizeSound);
            toggleDrumPlayback();
        }
        else if (powerUp instanceof PowerUp.Goal) {
            // advance to next map
            soundManager.play(prizeSound,
                new EchoFilter(2000, .7f), false);
            map = resourceManager.loadNextMap();
        }
    }

}



K, the resource manager is whats loading all of the images and creating the animation. All of the images are loaded into a multidimentional array. Images [] []
the "Outer arrays" holding the images, and the inner arrays being the images themselves.
Resource manager
package com.brackeen.javagamebook.tilegame;

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.io.*;
import java.util.ArrayList;
import javax.swing.ImageIcon;

import com.brackeen.javagamebook.graphics.*;
import com.brackeen.javagamebook.tilegame.sprites.*;


/**
    The ResourceManager class loads and manages tile Images and
    "host" Sprites used in the game. Game Sprites are cloned from
    "host" Sprites.
*/
public class ResourceManager {

    private ArrayList tiles;
    private int currentMap;
    private GraphicsConfiguration gc;
    

    // host sprites used for cloning
   
    private Player playerSprite;
    private Sprite musicSprite;
    private Sprite coinSprite;
    private Sprite goalSprite;
    private Sprite knightSprite;
    private Sprite flySprite;

    /**
        Creates a new ResourceManager with the specified
        GraphicsConfiguration.
    */
    public ResourceManager(GraphicsConfiguration gc) {
        this.gc = gc;
        loadTileImages();
        loadPlayerSprites();
        loadCreatureSprites();
        loadPowerUpSprites();
    }


    /**
        Gets an image from the images/ directory.
    */
    public Image loadImage(String name) {
        String filename = "images/" + name;
        return new ImageIcon(filename).getImage();
    }


    public Image getMirrorImage(Image image) {
        return getScaledImage(image, -1, 1);
    }


    public Image getFlippedImage(Image image) {
        return getScaledImage(image, 1, -1);
    }


    private Image getScaledImage(Image image, float x, float y) {

        // set up the transform
        AffineTransform transform = new AffineTransform();
        transform.scale(x, y);
        transform.translate(
            (x-1) * image.getWidth(null) / 2,
            (y-1) * image.getHeight(null) / 2);

        // create a transparent (not translucent) image
        Image newImage = gc.createCompatibleImage(
            image.getWidth(null),
            image.getHeight(null),
            Transparency.BITMASK);

        // draw the transformed image
        Graphics2D g = (Graphics2D)newImage.getGraphics();
        g.drawImage(image, transform, null);
        g.dispose();

        return newImage;
    }


    public TileMap loadNextMap() {
        TileMap map = null;
        while (map == null) {
            currentMap++;
            try {
                map = loadMap(
                    "maps/map" + currentMap + ".txt");
            }
            catch (IOException ex) {
                if (currentMap == 1) {
                    // no maps to load!
                    return null;
                }
                currentMap = 0;
                map = null;
            }
        }

        return map;
    }


    public TileMap reloadMap() {
        try {
            return loadMap(
                "maps/map" + currentMap + ".txt");
        }
        catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
    }


    private TileMap loadMap(String filename)
        throws IOException
    {
        ArrayList lines = new ArrayList();
        int width = 0;
        int height = 0;

        // read every line in the text file into the list
        BufferedReader reader = new BufferedReader(
            new FileReader(filename));
        while (true) {
            String line = reader.readLine();
            // no more lines to read
            if (line == null) {
                reader.close();
                break;
            }

            // add every line except for comments
            if (!line.startsWith("#")) {
                lines.add(line);
                width = Math.max(width, line.length());
            }
        }

        // parse the lines to create a TileEngine
        height = lines.size();
        TileMap newMap = new TileMap(width, height);
        for (int y=0; y<height; y++) {
            String line = (String)lines.get(y);
            for (int x=0; x<line.length(); x++) {
                char ch = line.charAt(x);

                // check if the char represents tile A, B, C etc.
                int tile = ch - 'A';
                if (tile >= 0 && tile < tiles.size()) {
                    newMap.setTile(x, y, (Image)tiles.get(tile));
                }

                // check if the char represents a sprite
                else if (ch == 'o') {
                    addSprite(newMap, coinSprite, x, y);
                }
                else if (ch == '!') {
                    addSprite(newMap, musicSprite, x, y);
                }
                else if (ch == '*') {
                    addSprite(newMap, goalSprite, x, y);
                }
                else if (ch == '1') {
                    addSprite(newMap, knightSprite, x, y);
                }
                else if (ch == '2') {
                    addSprite(newMap, flySprite, x, y);
                }
            }
        }

        // add the player to the map
        Sprite player = (Sprite)playerSprite.clone();
        player.setX(TileMapRenderer.tilesToPixels(3));
        player.setY(0);
        newMap.setPlayer(player);

        return newMap;
    }


    private void addSprite(TileMap map,
        Sprite hostSprite, int tileX, int tileY)
    {
        if (hostSprite != null) {
            // clone the sprite from the "host"
            Sprite sprite = (Sprite)hostSprite.clone();

            // center the sprite
            sprite.setX(
                TileMapRenderer.tilesToPixels(tileX) +
                (TileMapRenderer.tilesToPixels(1) -
                sprite.getWidth()) / 2);

            // bottom-justify the sprite
            sprite.setY(
                TileMapRenderer.tilesToPixels(tileY + 1) -
                sprite.getHeight());

            // add it to the map
            map.addSprite(sprite);
        }
    }


    // -----------------------------------------------------------
    // code for loading sprites and images
    // -----------------------------------------------------------


    public void loadTileImages() {
        // keep looking for tile A,B,C, etc. this makes it
        // easy to drop new tiles in the images/ directory
        tiles = new ArrayList();
        char ch = 'A';
        while (true) {
            String name = "tile_" + ch + ".png";
            File file = new File("images/" + name);
            if (!file.exists()) {
                break;
            }
            tiles.add(loadImage(name));
            ch++;
        }
    }

    public void loadPlayerSprites(){
    	
    	Image[][] playerImages = new Image[6][];
    	
    	//loadplayerimages
    	playerImages[0] = new Image[]{
    		loadImage("PlayerMovement1.png"),
            loadImage("PlayerMovement2.png"),
            loadImage("PlayerMovement3.png"),
            
    	};
    	
    	playerImages[1] = new Image[playerImages[0].length];
    	playerImages[2] = new Image[]{
    			loadImage("PlayerDead1.png"),
                loadImage("PlayerDead2.png"),
                loadImage("PlayerDead3.png"),
                loadImage("PlayerDead4.png"),
                loadImage("PlayerDead5.png"),
                loadImage("PlayerDead6.png"),
                loadImage("PlayerDead7.png"),
                loadImage("PlayerDead8.png"),
                loadImage("PlayerDead9.png"),
                loadImage("PlayerDead10.png"),
                loadImage("PlayerDead11.png"),
    	};
    	playerImages[3] = new Image[playerImages[2].length];
    	playerImages[4] = new Image[]{
    			loadImage("PlayerJump1.png"),
    			loadImage("PlayerJump2.png"),
    			loadImage("PlayerJump3.png"),
    	};
    	playerImages[5] = new Image[playerImages[4].length];
    	for(int i=0; i<playerImages[0].length;i++){
    		//right facing images
    		playerImages[1][i] = getMirrorImage(playerImages[0][i]);
    	}
    	for(int i=0; i<playerImages[2].length;i++){
    		//right facing dead images
    		playerImages[3][i] = getMirrorImage(playerImages[2][i]);	
    	}
    	for(int i=0; i<playerImages[4].length;i++){
    		//right facing jump images
    		playerImages[5][i] = getMirrorImage(playerImages[4][i]);
    	}
    
    	//create player animations
    	Animation[] playerAnim = new Animation[2];
    	for(int i=0; i<2; i++){
    		playerAnim[i] = createPlayerAnim(
    			playerImages[i][0], playerImages[i][1], playerImages[i][2]);
    	}
    	//create dead animations
        Animation[] playerDeadAnim = new Animation[2];
    	for(int i=0; i<2; i++){
    		playerDeadAnim[i] = createPlayerDeadAnim(
                                // here I'm adding 2 to i so that i can accesses the 3rd and 4th indicies of the playerImage 
                                   array which is where the left facing and mirrored right facing images are.
    				playerImages[i+2][0],playerImages[i+2][1],playerImages[i+2][2],
    				playerImages[i+2][3],playerImages[i+2][4],playerImages[i+2][5],playerImages[i+2][6],
    				playerImages[i+2][7],playerImages[i+2][8],playerImages[i+2][9],playerImages[i+2][10]
    				);
    	}
    	//create jump animations
    	Animation[] playerJumpAnim = new Animation[2];
    	for(int i=0; i<2; i++){
    		playerJumpAnim[i] = createPlayerJumpAnim(
                                  //adding four for the same reason as before
    				playerImages[i+4][0], playerImages[i+4][1],playerImages[i+4][2]);
    	}
    	   	
    	playerSprite = new Player(playerAnim[0], playerAnim[1],
                playerDeadAnim[0], playerDeadAnim[1]) ;
        playerSprite.addJumpAnim(playerJumpAnim[0], playerJumpAnim[1]);
    

    }
    public void loadCreatureSprites() {

        Image[][] images = new Image[4][];

        // load left-facing images
        images[0] = new Image[] {
            
            loadImage("Flying1.png"),
            loadImage("Flying2.png"),
            loadImage("Flying3.png"),
            loadImage("Flying4.png"),
            loadImage("KnightEnemy1.png"),
            loadImage("KnightEnemy2.png"),
            loadImage("KnightEnemy3.png"),
            loadImage("KnightEnemy4.png"),
            loadImage("KnightEnemy5.png"),
            loadImage("KnightEnemy6.png"),
            loadImage("KnightEnemy7.png"),
            loadImage("KnightEnemy8.png"),
        };

        images[1] = new Image[images[0].length];
        images[2] = new Image[]{
        	
        	loadImage("EnemyDead1.png"),
        	loadImage("EnemyDead2.png"),
        	loadImage("EnemyDead3.png"),
        	loadImage("EnemyDead4.png"),
        	loadImage("EnemyDead5.png"),
        	loadImage("EnemyDead6.png"),
        	loadImage("EnemyDead7.png"),
        	loadImage("EnemyDead8.png"),
        	loadImage("EnemyDead9.png"),
        	loadImage("EnemyDead10.png"),
        	loadImage("EnemyDead11.png"),
        	loadImage("EnemyDead12.png"),
        	loadImage("EnemyDead13.png"),
        };
        images[3] = new Image[images[2].length];
        for (int i=0; i<images[0].length; i++) {
            // right-facing images
            images[1][i] = getMirrorImage(images[0][i]);
        }
        for (int i=0; i<images[2].length;i++){
            images[3][i] = images[2][i];
        }

        // create creature animations
        
        Animation[] flyAnim = new Animation[2];
        Animation[] knightAnim = new Animation[2];
        for (int i=0; i<2; i++) {
            
            flyAnim[i] = createFlyAnim(
                images[i][0], images[i][1], images[i][2], images[i][3]);
            knightAnim[i] = createKnightAnim(
                images[i][4], images[i][5], images[i][6],images[i][7],images[i][8],images[i][9],images[i][10],
                images[i][11]);
        }
        Animation[] deadAnim = new Animation[2];
        for (int i=0; i<2; i++){
        	deadAnim[i] = createDeadAnim(images[i+2][0],images[i+2][1],images[i+2][2],images[i+2][3],
        			                     images[i+2][4],images[i+2][5],images[i+2][6],images[i+2][7],
        			                     images[i+2][8],images[i+2][9],images[i+2][10],images[i+2][11],
        			                     images[i+2][12]);
        }

        // create creature sprites
                flySprite = new Fly(flyAnim[0], flyAnim[1],
            deadAnim[0], deadAnim[1]);
        knightSprite = new Knight(knightAnim[0], knightAnim[1],
            deadAnim[0], deadAnim[1]);
    }


    private Animation createPlayerAnim(Image player1,
        Image player2, Image player3)
    {
        Animation anim = new Animation();
        anim.addFrame(player1, 200);
        anim.addFrame(player2, 150);
        anim.addFrame(player3, 200);
        anim.addFrame(player2, 150);
        return anim;
    }
    
    private Animation createPlayerDeadAnim(Image dead1, Image dead2, Image dead3, Image dead4,
    									   Image dead5, Image dead6, Image dead7, Image dead8,
    									   Image dead9, Image dead10, Image dead11){
    	Animation anim = new Animation();
    	anim.addFrame(dead11, 100);
    	anim.addFrame(dead10, 100);
    	anim.addFrame(dead9, 100);
    	anim.addFrame(dead8, 100);
    	anim.addFrame(dead7, 100);
    	anim.addFrame(dead6, 100);
    	anim.addFrame(dead5, 100);
    	anim.addFrame(dead4, 100);
    	anim.addFrame(dead3, 100);
    	anim.addFrame(dead2, 100);
    	anim.addFrame(dead1, 495);
    	
    	return anim;
    } 
    
    private Animation createPlayerJumpAnim(Image jump1, Image jump2, Image jump3){
   	    
    	Animation anim = new Animation();
    	anim.addFrame(jump1, 60);
    	anim.addFrame(jump2, 60);
    	anim.addFrame(jump3, 80);   	
    	return anim;
    }


    private Animation createFlyAnim(Image img1, Image img2,
        Image img3, Image img4)
    {
        Animation anim = new Animation();
        anim.addFrame(img1, 80);
        anim.addFrame(img2, 80);
        anim.addFrame(img3, 80);
        anim.addFrame(img4, 80);
        return anim;
    }


    private Animation createKnightAnim(Image knight1, Image knight2, Image knight3, Image knight4,
    		                           Image knight5, Image knight6, Image knight7, Image knight8) {
        Animation anim = new Animation();
        anim.addFrame(knight1, 150);
        anim.addFrame(knight2, 150);
        anim.addFrame(knight3, 150);
        anim.addFrame(knight4, 75);
        anim.addFrame(knight5, 37);
        anim.addFrame(knight6, 75);
        anim.addFrame(knight7, 150);
        anim.addFrame(knight8, 150);
        return anim;
    }
    
    private Animation createDeadAnim(Image eDead1, Image eDead2, Image eDead3, Image eDead4, Image eDead5,
    		                         Image eDead6, Image eDead7, Image eDead8, Image eDead9, Image eDead10,
    		                         Image eDead11, Image eDead12, Image eDead13){
    	Animation anim = new Animation();
    	anim.addFrame(eDead1, 115);
    	anim.addFrame(eDead2, 115);
    	anim.addFrame(eDead3, 115);
    	anim.addFrame(eDead4, 115);
    	anim.addFrame(eDead5, 115);
    	anim.addFrame(eDead6, 115);
    	anim.addFrame(eDead7, 115);
    	anim.addFrame(eDead8, 115);
    	anim.addFrame(eDead9, 115);
    	anim.addFrame(eDead10, 115);
    	anim.addFrame(eDead11, 115);
    	anim.addFrame(eDead12, 115);
    	anim.addFrame(eDead13, 115);
    	return anim;
    }


    private void loadPowerUpSprites() {
        // create "goal" sprite
        Animation anim = new Animation();
        anim.addFrame(loadImage("heart1.png"), 150);
        anim.addFrame(loadImage("heart2.png"), 150);
        anim.addFrame(loadImage("heart3.png"), 150);
        anim.addFrame(loadImage("heart2.png"), 150);
        goalSprite = new PowerUp.Goal(anim);

        // create "star" sprite
        anim = new Animation();
        anim.addFrame(loadImage("star1.png"), 100);
        anim.addFrame(loadImage("star2.png"), 100);
        anim.addFrame(loadImage("star3.png"), 100);
        anim.addFrame(loadImage("star4.png"), 100);
        coinSprite = new PowerUp.Star(anim);

        // create "music" sprite
        anim = new Animation();
        anim.addFrame(loadImage("music1.png"), 150);
        anim.addFrame(loadImage("music2.png"), 150);
        anim.addFrame(loadImage("music3.png"), 150);
        anim.addFrame(loadImage("music2.png"), 150);
        musicSprite = new PowerUp.Music(anim);
    }

}



Creature class
package com.brackeen.javagamebook.tilegame.sprites;

import java.lang.reflect.Constructor;
import com.brackeen.javagamebook.graphics.*;

/**
    A Creature is a Sprite that is affected by gravity and can
    die. It has four Animations: moving left, moving right,
    dying on the left, and dying on the right.
*/
public abstract class Creature extends Sprite {

    /**
        Amount of time to go from STATE_DYING to STATE_DEAD.
    */
    protected static final int DIE_TIME = 1495;

    public static final int STATE_NORMAL = 0;
    public static final int STATE_DYING = 1;
    public static final int STATE_DEAD = 2;

    protected Animation left;
    protected Animation right;
    protected Animation deadLeft;
    protected Animation deadRight;
    protected Animation newAnim;
   
    
    public Animation getNewAnim() {
		return newAnim;
	}

	public void setNewAnim(Animation newAnim) {
		this.newAnim = newAnim;
	}


	private int state;
    protected long stateTime;

    /**
        Creates a new Creature with the specified Animations.
    */
  
    public Creature(Animation left, Animation right,
        Animation deadLeft, Animation deadRight)
    {
        super(right);
        this.left = left;
        this.right = right;
        this.deadLeft = deadLeft;
        this.deadRight = deadRight;
        
        state = STATE_NORMAL;
    }
    
	public Object clone() {
        // use reflection to create the correct subclass
        Constructor constructor = getClass().getConstructors()[0];
        
        try {
            return constructor.newInstance(new Object[] {
                (Animation)left.clone(),
                (Animation)right.clone(),
                (Animation)deadLeft.clone(),
                (Animation)deadRight.clone(),
                
            });
        }
        catch (Exception ex) {
            // should never happen
            ex.printStackTrace();
            return null;
        }
    }


    /**
        Gets the maximum speed of this Creature.
    */
    public float getMaxSpeed() {
        return 0;
    }


    /**
        Wakes up the creature when the Creature first appears
        on screen. Normally, the creature starts moving left.
    */
    public void wakeUp() {
        if (getState() == STATE_NORMAL && getVelocityX() == 0) {
            setVelocityX(-getMaxSpeed());
        }
    }


    /**
        Gets the state of this Creature. The state is either
        STATE_NORMAL, STATE_DYING, or STATE_DEAD.
    */
    public int getState() {
        return state;
    }


    /**
        Sets the state of this Creature to STATE_NORMAL,
        STATE_DYING, or STATE_DEAD.
    */
    public void setState(int state) {
        if (this.state != state) {
            this.state = state;
            stateTime = 0;
            if (state == STATE_DYING) {
                setVelocityX(0);
                setVelocityY(0);
            }
        }
    }


    /**
        Checks if this creature is alive.
    */
    public boolean isAlive() {
        return (state == STATE_NORMAL);
    }


    /**
        Checks if this creature is flying.
    */
    public boolean isFlying() {
        return false;
    }


    /**
        Called before update() if the creature collided with a
        tile horizontally.
    */
    public void collideHorizontal() {
        setVelocityX(-getVelocityX());
    }


    /**
        Called before update() if the creature collided with a
        tile vertically.
    */
    public void collideVertical() {
        setVelocityY(0);
    }


    /**
        Updates the animation for this creature.
    */
    public void update(long elapsedTime) {
        // select the correct Animation
        setNewAnim(anim);
        if (getVelocityX() < 0) {
            setNewAnim(left);
        }
        else if (getVelocityX() > 0) {
            setNewAnim(right);
        }
        if (state == STATE_DYING && newAnim == left) {
            setNewAnim(deadLeft);
        }
        else if (state == STATE_DYING && newAnim == right) {
            setNewAnim(deadRight);
        }
        

        // update the Animation
        if (anim != getNewAnim()) {
            anim = getNewAnim();
            anim.start();
        }
        else {
            anim.update(elapsedTime);
        }

        // update to "dead" state
        stateTime += elapsedTime;
        if (state == STATE_DYING && stateTime >= DIE_TIME) {
            setState(STATE_DEAD);
        }
    }
}



Player class This is where my error starts, in the players update.
package com.brackeen.javagamebook.tilegame.sprites;

import java.lang.reflect.Constructor;

import com.brackeen.javagamebook.graphics.Animation;

/**
    The Player.
*/
public class Player extends Creature {
	private boolean JUMP_STATE = true;
    
	private static float JUMP_SPEED = -.95f;
   
	//private int JUMP_TIME = 200;   
   	private Animation jumpLeft;
    private Animation jumpRight;
    
    private boolean onGround;

    public Player(Animation left, Animation right, Animation deadLeft, Animation deadRight){
    	super(left,right,deadLeft,deadRight);
    }
   
    public void collideHorizontal() {
        setVelocityX(0);
    }


    public void collideVertical() {
        // check if collided with ground
        if (getVelocityY() > 0) {
            onGround = true;
        }
        setVelocityY(0);
    }


    public void setY(float y) {
        // check if falling
        if (Math.round(y) > Math.round(getY())) {
            onGround = false;
        }
        super.setY(y);
    }


    public void wakeUp() {
        // do nothing
    }


    /**
        Makes the player jump if the player is on the ground or
        if forceJump is true.
    */
 
    
    public void jump(boolean forceJump) {
    	
   
    	if (onGround || forceJump) {
    		onGround = false;
    		setVelocityY(JUMP_SPEED);
    		JUMP_STATE = true;
    		
    	}
        
    }
    public void addJumpAnim(Animation jumpLeft,Animation jumpRight){
    	this.jumpLeft = jumpLeft;
    	this.jumpRight = jumpRight;
    }

    public float getMaxSpeed() {
        return 0.5f;
    }
    
    public void update(long elapsedTime)
    {
    	super.update(elapsedTime);
    	
    	
    	setNewAnim(super.anim);
    	if(JUMP_STATE &&  getNewAnim() == left){
    		setNewAnim(jumpLeft);
    	}
    	else if(JUMP_STATE  && getNewAnim() == right){
    		setNewAnim(jumpRight);
    	}
    	
    	//this if else block is where the program fails and I get my null pointer exception
    	if (super.anim != getNewAnim()) {
            super.anim = getNewAnim();
            anim.start();
        }
        else {
            super.anim.update(elapsedTime);
        }
    	
     	stateTime += elapsedTime;
    	if(JUMP_STATE == false){
    		setState(STATE_NORMAL);
    		stateTime = 0;
    		super.update(elapsedTime);
    		
    		
    	}
    	if (getState() == STATE_DYING && stateTime >= DIE_TIME) {
            setState(STATE_DEAD);
        }
    	
    }
   
}


Was This Post Helpful? 0

#12 Guest_cjWildsmith*


Reputation:

Re: Java, Help cloning 2d animations

Posted 04 June 2010 - 08:57 PM

theres a lot more code, but this is pretty much what I'm dealing with at the moment.
Was This Post Helpful? 0

#13 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10185
  • View blog
  • Posts: 37,600
  • Joined: 27-December 08

Re: Java, Help cloning 2d animations

Posted 05 June 2010 - 08:46 AM

As of Java 1.5, you should be using Generics for Collections like List to avoid a deprecation warning as well as having to do so much casting. With Generics, the casting is done internally in the Collection rather than in your code.

Also, what exactly is your question here?
Was This Post Helpful? 0
  • +
  • -

#14 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1633
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Java, Help cloning 2d animations

Posted 05 June 2010 - 09:06 AM

Topics merged, please dont create duplicate topics :)
Was This Post Helpful? 1
  • +
  • -

#15 Guest_cjWildsmith*


Reputation:

Re: Java, Help cloning 2d animations

Posted 05 June 2010 - 01:50 PM

In the players update update(elapsedTime) I get a null point exception on anim.start(). The only problem that I can see in my code is the Player class which inherits from the Creature class does not implement the Creatures clone() function, which is declared in the Sprite class. I need to know how to use the method addJumpAnim in the Player class and implement that into my clone() function.
Was This Post Helpful? 0

  • (2 Pages)
  • +
  • 1
  • 2