Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Sprite won't animate while moving! Please help

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Sprite won't animate while moving! Please help

    I'm using a 2d game engine called GTGE. You can find the API just by googling it. This issue has plagued me for days now and I need help. I initialize the different animation frames (when looking up, down, left, right) and then try to set them when the user inputs a direction command. The image will change but won't animate while the sprite moves. It's very odd.
    Here is my method:
    Note that going left and right (A/D) have not had AnimationSprite implemented and it was the old placeholder of simply changing the image.
    	public void movement(long elapsedTime){
     
    		BufferedImage[] player_walking_up = getImages("player_walking_up.png", 2, 1);
    		BufferedImage[] player_walking_down = getImages("player_walking_down.png", 2, 1);
    		BufferedImage[] playerU = getImages("player_still.png",1,1);
    		if (keyDown(KeyEvent.VK_A)){
    			//s1.setImage(playerL);
    			s1.addHorizontalSpeed(elapsedTime, -0.005, -0.1);
    		}
    		else if (keyDown(KeyEvent.VK_D)){
    			//s1.setImage(playerR);
    			s1.addHorizontalSpeed(elapsedTime,  0.005,  0.1);
    		}
    		//else if (s1.getHorizontalSpeed() > 0) s1.addHorizontalSpeed(elapsedTime, -0.0005, 0);
    		//else if (s1.getHorizontalSpeed() < 0) s1.addHorizontalSpeed(elapsedTime,  0.0005, 0);
    		else
    			s1.setImages(playerU);
    		if (keyDown(KeyEvent.VK_W)){
    			s1.setImages(player_walking_up);
    			//s1.addVerticalSpeed(elapsedTime, -0.005, -0.1);
    			s1.moveY(-1);
    		}
    		else if (keyDown(KeyEvent.VK_S)){
    			s1.setImages(player_walking_down);
    			//s1.addVerticalSpeed(elapsedTime,  0.005,  0.1);
    			s1.moveY(1);
    		}
    		else
    			s1.setImages(playerU);
    		//else if (s1.getVerticalSpeed() > 0) s1.addVerticalSpeed(elapsedTime, -0.0005, 0);
    		//else if (s1.getVerticalSpeed() < 0) s1.addVerticalSpeed(elapsedTime,  0.0005, 0);
    		//s1.setAnimationFrame(0, 1);
    		//s1.setAnimate(true);
    		//s1.setLoopAnim(true);

    And here is the top of the program. There is a lot more and please let me know if you need to see it.
    public class TestProject extends Game 
    {
     
    	// 1512 x 1134
    	Random rand = new Random(); 
    	Background background;
    	TileBackground tileBG;
     
    	Sprite     health_bar, enemy;
    	Sprite[] enemies;
     
    	AnimatedSprite s1;
     
    	SpriteGroup BULLET_GROUP;
    	SpriteGroup	ENEMY_GROUP;
    	SpriteGroup GRENADE_GROUP;
     
    	CollisionManager collisionType;
     
    	GameFont           font;
     
    	StopWatch timer = new StopWatch();
     
    	Timer timer_delay = new Timer(5000);
     
    	DecimalFormat df = new DecimalFormat("#.##");
     
     
     
    	int num_enemies_wave=0, num_enemies_spawned, num_enemies_left;
    	int current_wave = 0; int current_weapon = 1; int weapon_change;
    	int current_health = 10; int max_health = 10;
    	double kills = 0, bullets_fired = 0, accuracy = 0;
    	boolean show_highscore = false, player_dead = false;
    	String hpimage;
    	double speed = .05;
     
     
     
    	public void initResources() 
    	{
     
    		BufferedImage[] tileImages = getImages("tiles_dark_grass.png", 6, 2); 
    		int[][] tiles = new int[48][36];
    		for (int i=0;i < tiles.length;i++) {
    			for (int j=0;j < tiles[0].length;j++) {
    				tiles[i][j] = getRandom(0, tileImages.length-1);
    			}
    		}
    		tileBG  = new TileBackground(tileImages, tiles);
    		background = tileBG;
    		background.setClip(0, 0, 768, 576);
     
    		ENEMY_GROUP = new SpriteGroup("Enemy");
    		ENEMY_GROUP.setBackground(background);
    		s1 = new AnimatedSprite();
    		s1.setBackground(background);
    		s1.setLocation(200,200);
    		s1.setAnimate(true);
    		s1.setLoopAnim(true);
    		s1.getAnimationTimer().setDelay(200);
    		health_bar = new Sprite(getImage("healthbar10.png"));
    		health_bar.setLocation(10,10);
    		health_bar.setBackground(background);
     
    		BULLET_GROUP = new SpriteGroup("Bullet");
    		BULLET_GROUP.setBackground(background);
     
    		GRENADE_GROUP = new SpriteGroup("Grenade");
    		GRENADE_GROUP.setBackground(background);
     
    		collisionType = new  BulletHitEnemy();
    		collisionType.setCollisionGroup(ENEMY_GROUP, BULLET_GROUP);
     
    		font = fontManager.getFont(getImages("font.png", 20, 3),
    				" !            .,0123" +
    						"456789:   -? ABCDEFG" +
    				"HIJKLMNOPQRSTUVWXYZ ");
     
     
    	}


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Sprite won't animate while moving! Please help

    Welcome to Java Programming Forums. I didn't see your code too much but does .png support animation?

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sprite won't animate while moving! Please help

    I did google and found that .png does not support animation but it still worked, just not while moving. Changed the image to .gif and it acted the same way. Would animate fine, but not while moving.
    Last edited by sellers04; December 14th, 2011 at 05:01 PM.

  4. #4
    Junior Member
    Join Date
    Dec 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sprite won't animate while moving! Please help

    bump bump bump

Similar Threads

  1. Drawing 2 images of a the same Sprite
    By risen375 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 15th, 2011, 10:25 AM
  2. Sprite Animation with ArrayLists
    By FShounen in forum AWT / Java Swing
    Replies: 13
    Last Post: June 6th, 2011, 08:43 AM
  3. [SOLVED] How do you get this fountain to animate with paint method?
    By javapenguin in forum What's Wrong With My Code?
    Replies: 48
    Last Post: May 31st, 2011, 04:00 PM
  4. Moving sprite leaves a trail
    By chris2307 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 26th, 2011, 09:01 AM
  5. Alphabet from sprite sheet
    By Asido in forum Java Theory & Questions
    Replies: 2
    Last Post: September 18th, 2010, 11:49 AM