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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Flascher

Pages: [1]
1
Java / 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();
        }
}

 

Pages: [1]
anything