There is a class Airplane

public class Airplane extends BasicObject { public Airplane(){ // по центру экрана x = MyMainClass.WIDTH/2; y = MyMainClass.HEIGHT/2; dx = speed = 200; // делаю прямоугольник rectangle = new Rectangle(); rectangle.x = x; rectangle.y = y; rectangle.width = 64; rectangle.height = 64; // как самолет будет выглядеть texture = new Texture("AIRPLANE.png"); sprite = new Sprite(texture); // позиция спрайта соответствует позиции прямоугольника sprite.setPosition(rectangle.x, rectangle.y); public void update(float dt){ // если нажать B, то x сдвинется вправо, прямоугольник движется за x, // а позиция спрайта становится равной позиции прямоугольника if(Gdx.input.isKeyJustPressed(Input.Keys.В)) { x += dx * dt; rectangle.x = x; sprite.setPosition(rectangle.x, rectangle.y); } } 

Now the question is - didn’t I addicted to update() too much, or is this normal and must be done? If you remove from it sprite.setPosition(rectangle.x, rectangle.y); or rectangle.x = x; , then no movement occurs (only x changes). In general, is it normal or is there a better solution?

  • If the square not only shuffles to the right, as well as to the left, upwards, etc., it is better to make a class that will be responsible for keyboard / display input - SlandShow

1 answer 1

It may be better to add some class manager, which will click to catch and send a message (or call the interface callback) to all signatories, indicating a state, such as the Publisher-Subscriber template.