-1

Hello, I am trying to create a game. The movement in this game worked until i started with animations. I don't know why, but now it shows this error: "game cannot be resolved or is not a field. (sorry if this is a dumb question, I am pretty new to programming.)

package hra2.entities.creatures;

import java.awt.Graphics;
import java.awt.image.BufferedImage;

import hra2.Game;
import hra2.gfx.Animation;
import hra2.gfx.Assets;

public class Player extends Creature {

//ANIMATIONS 

private Animation animLeft;
private Animation animRight;

public Player(Game game, float x, float y) {
    super(x, y, 53, 79);
    this.game = game;

    //ANIMATIONS

    animLeft = new Animation(500, Assets.hrac_left);
    animRight = new Animation(500, Assets.hrac_right);

}


public void tick() {
    //ANIMATIONS
    animLeft.tick();
    animRight.tick();

    //MOVEMENT
    getInput();
    move();
}

private void getInput() {
    xMove = 0;
    float nula = 0;
    float tisic = 920;

    if(game.getKeyManager().left)
        xMove = -speed;

    if(game.getKeyManager().right)
        xMove = speed;

    if(x <= nula) {
        x += 5;
    }

    if(x >= tisic) {
        x -= 5;
    }

}
  • *"game [..] is not a field"*... have you tried to think about the meaning of this sentence? – Tom Sep 23 '17 at 16:10

2 Answers2

2

Well, game is not a field in your class. Make game a field of your class, just like animLeft and animRight.

Alvin L-B
  • 488
  • 2
  • 8
1

It's at this.game = game;, you don't have a game variable in your class.

0ddlyoko
  • 310
  • 4
  • 14