Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Can't get sprite to move  (Read 5669 times)

0 Members and 1 Guest are viewing this topic.

Flascher

  • Newbie
  • *
  • Posts: 1
    • View Profile
Can't get sprite to move
« on: March 25, 2014, 08:32:54 pm »
Hello everyone!

I'm having problems getting my player sprite to move based on input. I'm very new to game development in general, and SFML looks like a good starting point. I'm already most familiar with Java, and so I figured I would use the tools I've got to learn a bit more about what I'm interested in.

That being said I've had a few college computer science courses already, so I do understand most of what's going on, I'm just not sure how my coding is in general. I followed a tutorial from someone else on youtube and sort of adapted his base implementation to match my own. Please do inform me if my implementation on all of this is unorthodox, or could be improved!

Anyways, here are the relevant classes and methods I'm having problems with:

Player.java:
public class Player {
        private Sprite sprite;
        private int x;
        private int y;
        private int health;
        private int shield;
        private int speed;
        private double lookAngle;

        public Player(){
                try{
                        Texture playerTexture = new Texture();
                        playerTexture.loadFromFile(Paths.get("res/Player.png"));
                        playerTexture.setSmooth(true);
                        sprite = new Sprite(playerTexture);
                } catch(IOException e){
                        System.err.print("Could not locate Player.png");
                        e.printStackTrace();
                }
                x = Main.window.getSize().x/2;
                y = Main.window.getSize().y/2;
                health = 0;
                shield = 0;
                speed = 1;

                sprite.setOrigin(Vector2f.div(new Vector2f(sprite.getTexture().getSize()), 2));
                sprite.setPosition(x,y);
                sprite.setScale(.65f, .65f);

                ResourceManager.addSprite(sprite);
        }

        public void moveUp() {
                sprite.move(new Vector2f(x, y -= 10*speed));
        }

        public void moveDown() {
                sprite.move(new Vector2f(x, y += 10*speed));
        }

        public void moveLeft() {
                x -= 10*speed;
        }

        public void moveRight() {
                x += 10*speed;
        }
       
        public void update() {
                lookAtMouse();
        }

ResourceManager.java:
public class ResourceManager {

        public static ArrayList<Texture> textures;
        public static ArrayList<Sprite> sprites;
        //public ArrayList<Animation> animations;
        public static ArrayList<Text> texts;
        public static ArrayList<Button> buttons;

        public static LinkedList<Enemy> enemies;
        public static Player player;

        public ResourceManager(){
                textures = new ArrayList<Texture>();
                sprites = new ArrayList<Sprite>();
                //animations = new ArrayList<Animation>();
                texts = new ArrayList<Text>();
                buttons = new ArrayList<Button>();

                enemies = new LinkedList<Enemy>();
                player = new Player();
        }

        public static boolean addSprite(Sprite sprite){
                if(sprites.add(sprite))
                        return true;
                else return false;
        }

        public static void tick(Time t){
                player.tick(t);
                for(Enemy e : enemies){
                        e.tick(t);
                }
        }

        public static void update(Window w){
                player.update();
                for(Enemy e : enemies){
                        e.update();
                }
        }
 

 

Main.java:
import org.jsfml.graphics.RenderWindow;
import org.jsfml.graphics.Sprite;
import org.jsfml.system.Clock;
import org.jsfml.system.Time;
import org.jsfml.window.Keyboard;
import org.jsfml.window.Mouse;
import org.jsfml.window.VideoMode;
import org.jsfml.window.event.Event;

import java.util.ArrayList;

/**
 * Created by alex on 2/10/14.
 */

public class Main {


        static RenderWindow window = new RenderWindow(new VideoMode(800, 600), "Algebra Wars");
        private static ResourceManager res = new ResourceManager();
        private static Clock clock = new Clock();
        private static Player player = new Player();

        public static void main(String[] args){
                window.setFramerateLimit(60);
                while(window.isOpen()){
                        pollEvents();
                        getInput();
                        tick(clock.restart());
                        update();
                }
        }

        private static void tick(Time t){
                ResourceManager.tick(t);
        }

        private static void pollEvents(){
                for(Event e : window.pollEvents()){
                        switch(e.type){
                                case CLOSED:
                                        window.close();
                                        break;
                        }
                }
        }

        private static void getInput(){
                if(Keyboard.isKeyPressed(Keyboard.Key.W))
                        player.moveUp();
                if(Keyboard.isKeyPressed(Keyboard.Key.S))
                        player.moveDown();
                if(Keyboard.isKeyPressed(Keyboard.Key.A))
                        player.moveLeft();
                if(Keyboard.isKeyPressed(Keyboard.Key.D))
                        player.moveRight();
                if(Keyboard.isKeyPressed(Keyboard.Key.SPACE))
                        player.dropBomb();
                if(Mouse.isButtonPressed(Mouse.Button.LEFT))
                        player.fire(Mouse.getPosition());
                if(Mouse.isButtonPressed(Mouse.Button.RIGHT))
                        player.boost(Mouse.getPosition());
        }

        private static void update(){
                window.clear();
                ResourceManager.update(window);
                ArrayList<Sprite> sprites = res.getSprites();
                for(Sprite s : sprites){
                        window.draw(s);
                }
                window.display();
        }
}

 
« Last Edit: March 25, 2014, 08:40:15 pm by Flascher »

liesard

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Can't get sprite to move
« Reply #1 on: January 26, 2015, 02:16:14 am »
I hope that you managed to get it fixed after all this time, but it's late and I feel like solving problems.

The first thing I noticed was that in both moveLeft() and moveRight(), only the variable changes.  The player itself doesn't.  Apart from that, it should normally work, but I haven't tried it.

A few other notes:
  • You can make Player extend Sprite.  This makes it easier to manage as you will be dealing with the player as a drawable object and not as something which holds a drawable object.  It's much more direct this way.
  • You can put the getInput() function in the Player class as it doesn't rely on a RenderWindow and all the events are about the player.  You would then call the method from the player object in the Main class.
  • Don't make everything static! It's unnecessary.  The way to avoid conflicts is to put what's in the main method into Main's constructor.  You would then call new Main(); in the main method.
  • Work on your naming conventions.  It's a little hard to explain, but, for example, booleans should be in the form of questions.  id est: isJumping, isAdded.  There are other things, but it's a little hard to explain

Also, please refer to this for practicing good practice: http://www.roxie.org/books/bleeding/appendices/ap01.html