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 "); }