Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: LWJGL Game: Odd behavior in game. Need help.

  1. #1
    Member
    Join Date
    Jan 2013
    Posts
    34
    My Mood
    Busy
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Unhappy LWJGL Game: Odd behavior in game. Need help.

    Hello, all. Today I am facing a really confusing problem that I am not sure of.
    Its all with firing projectiles.
    The expected behavior (for testing) is to simple move left.
    The behavior I am getting instead is not only moving to the left, but drawing at the wrong size, in fact the same size as the player, plus dragging the player along with it.
    May one of you kind people analyze my code a little, and maybe point out what might be the cause of this?

    Here is the Entity class:

    package cjaf.entity;
     
    import java.awt.Rectangle;
     
    import org.newdawn.slick.opengl.Texture;
     
    import cjaf.entity.event.EntityDamageEvent;
    import cjaf.item.Item;
    import cjaf.main.Location;
    import cjaf.tools.AdvancedDrawer;
    public abstract class Entity {
    	public Item[] drops;
    	protected boolean hasDrops;
    	protected Location loc;
    	protected Texture texture;
    	protected AdvancedDrawer renderer = new AdvancedDrawer();
    	public byte health, maxHealth;
    	public int width, height;
    	protected Rectangle bounds;
    	public int speed;
     
     
    	public Location getLocation(){
    		return loc;
    	}
     
    	public void setLocation(Location loc){
    		this.loc = loc;
    	}
     
    	public byte getHealth(){
    		return health;
    	}
     
    	public byte getMaxHealth(){
    		return maxHealth;
    	}
     
    	public void setTexture(String file){
    		texture = renderer.getTexture(file);
    	}
     
    	public void draw(){
    		renderer.drawTexture(texture, loc.getX(), loc.getY(), 64, 64);
    	}
     
    	public int getWidth(){
    		return width;
    	}
     
    	public int getHeight(){
    		return height;
    	}
     
    	//Should be replaced.
    	public void tick(){
     
    	}
    	public void damage(byte amount){
    		EntityDamageEvent ede = new EntityDamageEvent(this, amount, 1000);
    		ede.call();
    	}
    	public boolean intersects(Entity ent){
    		if(bounds.intersects(ent.bounds)&&!ent.isDead()){
    			return true;
    		}
    		return false;
    	}
    	public void setBounds(Location loc, int width, int height){
    		this.width = width;
    		this.height = height;
    		bounds = new Rectangle();
    		bounds.setBounds(loc.getX(), loc.getY(), width, height);
    	}
    	public boolean isDead(){
    		if(this.getHealth() <= 0)
    			return true;
    		else 
    			return false;
    	}
    	public void setHealth(byte b){
    		this.health = b;
     
    	}
    	public boolean isClose(Entity ent, int distance){
    		distance*=64;
    		if(ent.getLocation().getX() > this.getLocation().getX()-distance && ent.getLocation().getX() < this.getLocation().getX()+distance){
    			if(ent.getLocation().getY() > this.getLocation().getY()-distance && ent.getLocation().getY() < this.getLocation().getY()+distance){
    				return true;
    			}
    		}
    		return false;
    	}
    	public boolean isToLeftOf(Entity ent){
    		if(this.getLocation().getX() < ent.getLocation().getX())
    			return true;
    		else
    			return false;
    	}
    	public boolean isToRightOf(Entity ent){
    		if(this.getLocation().getX() > ent.getLocation().getX())
    			return true;
    		else
    			return false;
    	}
    	public boolean isAbove(Entity ent){
    		if(this.getLocation().getY() < ent.getLocation().getY())
    			return true;
    		else
    			return false;
    	}
    	public boolean isBelow(Entity ent){
    		if(this.getLocation().getY() > ent.getLocation().getY())
    			return true;
    		else
    			return false;
    	}
    	public void dropItem(Item item){
    		item.popAt(getLocation());
    	}
    	public boolean hasDrops(){
    		return hasDrops;
    	}
    	public void setCanHaveDrops(boolean val){
    		hasDrops = val;
    	}
    	public void updateLocation(){
    		bounds.setLocation(loc.getX(), loc.getY());
    	}
    	public void setSpeed(int speed){
    		this.speed = speed;
    	}
    	public Item[] getDrops(){
    		return drops;
    	}
    }
    The Player class (problem must lie here):
    package cjaf.entity;
     
     
     
    import java.util.ArrayList;
    import java.util.Random;
     
    import org.lwjgl.input.Keyboard;
     
    import cjaf.entity.entityais.EntityAI;
    import cjaf.entity.entityais.EntityAICollide;
    import cjaf.entity.entityais.EntityAIInteractEntities;
    import cjaf.entity.entityais.EntityAIShoot;
    import cjaf.item.Item;
    import cjaf.main.Location;
    /**
     * Player class.
     * @author JavaCakess@CJAF
     *
     */
    public class Player extends Entity 
    		implements EntityAI, EntityAIInteractEntities, EntityAICollide, EntityAIShoot{
     
    	int fire = Keyboard.KEY_SPACE;
    	private int shootTicks;
    	private ArrayList<Bullet> bullets;
    	public Player(Location loc){
     
    		this.loc = loc;
    		setBounds(getLocation(), 52, 52);
    		setTexture("entities/player/player.png");
    		this.maxHealth = 10;
    		this.health = 10;
    		bullets = new ArrayList<Bullet>();
    	}
     
    	public void tick() {
    		//Update entity
    		if(Keyboard.isKeyDown(Keyboard.KEY_W)){
    			getLocation().add(0, -2);
    		}
    		if(Keyboard.isKeyDown(Keyboard.KEY_A)){
    			getLocation().add(-2, 0);
    		}
    		if(Keyboard.isKeyDown(Keyboard.KEY_S)){
    			getLocation().add(0, 2);
    		}
    		if(Keyboard.isKeyDown(Keyboard.KEY_D)){
    			getLocation().add(2, 0);
    		}
     
     
    		bounds.setLocation(loc.getX(), loc.getY());
     
    	}
     
    	public void respawn(){
    		this.health = maxHealth;
    		Random r = new Random();
    		int x = r.nextInt(640);
    		int y = r.nextInt(480);
    		this.setLocation(new Location(x, y));
     
    	}
     
     
    	public void tick(ArrayList<Entity> e) {
     
    		for(Entity ent : e){
    			if(Keyboard.isKeyDown(fire)){
    				if(this.intersects(ent)&&!(ent == this)&&!ent.isDead()){
    					//ent.damage((byte) 1);
     
    				}
    			}
    		}
    	}
    	public void shoot(ArrayList<Entity> e) {
    		/*
    		 * 
    		 */
    		shootTicks++;
    		if(Keyboard.isKeyDown(fire)&&shootTicks > 40){
    			//TODO: Add firing logic.
    			bullets.add(new Bullet(this.getLocation()));
    		}
    		if(shootTicks == 60){
    			shootTicks = 0;
    		}
    		for(Bullet b : bullets){
    			b.draw();
    			b.tick();
    		}
    	}
    }

    And the Bullet class itself:
    package cjaf.entity;
     
    import cjaf.main.Location;
     
    public class Bullet extends Entity{
     
    	public Bullet(Location loc){
    		this.loc = loc;
    		setBounds(getLocation(), 8, 8);
    		setTexture("entities/projectiles/bullet.png");
    		this.maxHealth = 1;
    		this.health = 1;
    	}
    	public void tick(){
    		getLocation().add(0, 1);
    	}
    }

    The getLocation() returns a location object. The location class only has 2 variables, x and y.

    I should also probably show you the EntityHandler class, which handles all the onscreen entities.
    import java.util.ArrayList;
    import java.util.Random;
     
    import cjaf.entity.BasicEntity;
    import cjaf.entity.Entity;
    import cjaf.entity.Ghost;
    import cjaf.entity.Player;
    import cjaf.entity.Sword;
    import cjaf.entity.entityais.EntityAIAttackPlayer;
    import cjaf.entity.entityais.EntityAICollide;
    import cjaf.entity.entityais.EntityAIInteractEntities;
    import cjaf.entity.entityais.EntityAIShoot;
    import cjaf.tools.AdvancedDrawer;
     
    /**
     * Handles onscreen Entities
     * @author JavaCakess@CJAF
     *
     */
    public class EntityHandler {
    	AdvancedDrawer renderer = new AdvancedDrawer();
    	volatile ArrayList<Entity> e;
    	Player p;
    	BasicEntity b;
    	BasicEntity bee;
    	Ghost g;
    	Sword xs;
    	Random r = new Random();
    	int handleTicks = 0;
    	public EntityHandler(ArrayList<Entity> e){
    		this.e = e;
     
    	}
    	public void init(){
     
    		int x = r.nextInt(640);
    		int y = r.nextInt(480);
    		p = new Player(new Location(x, y));
    		b = new BasicEntity(new Location(1, 1));
    		g = new Ghost(renderer.getCenterOfScreen());
    		xs = new Sword(new Location(640, 475));
    		e.add(p);
    		//e.add(b);
    		e.add(xs);
    		//e.add(g);
     
    	}
    	public void handle(){
    		handleTicks++;
    		if(handleTicks == seconds(7)){
    			e.add(new BasicEntity(new Location(r.nextInt(640), r.nextInt(480))));
    			handleTicks = 0;
    		}
     
    		for(Entity entity : e){
    			if(!entity.isDead()){
    				entity.draw(); //Render entities.
    				entity.tick(); //Update entities by  calling each of their tick() methods.
     
    				if(entity instanceof EntityAIAttackPlayer){
    					EntityAIAttackPlayer e = (EntityAIAttackPlayer) entity;
    					e.tick(p);
    				}
    				if(entity instanceof EntityAICollide){
    					EntityAICollide e = (EntityAICollide) entity;
    					e.tick(this.e);
    				}
    				if(entity instanceof EntityAIInteractEntities){
    					EntityAIInteractEntities e = (EntityAIInteractEntities) entity;
    					e.tick(this.e);
    				}
    				if(p.isDead()){
    					System.err.println("You died!!!");
    					p.respawn();
    				}
    			}
    		}
     
     
     
    	}
     
    	public int seconds(int seconds){
    		return 60*seconds;
    	}
     
    	public Player getCurrentPlayer(){
    		return p;
    	}
    }


    Thanks for all the help


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: LWJGL Game: Odd behavior in game. Need help.

    The Problem in testing the code is that it uses third party packages and classes.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jan 2013
    Posts
    34
    My Mood
    Busy
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default Re: LWJGL Game: Odd behavior in game. Need help.

    Sorry, a lot of the classes I was importing were my own. I didn't want to show all of the classes because I have over 20 and I don't want to give anyone headaches.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: LWJGL Game: Odd behavior in game. Need help.

    Can you write a Short, Self Contained, Correct Example that shows the problem?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: March 1st, 2013, 11:01 PM
  2. Help with Snake Game Java Code: It's Impossible to Lose the Game
    By haruspex_icis in forum What's Wrong With My Code?
    Replies: 20
    Last Post: December 17th, 2012, 12:21 PM
  3. JButtons behavior in ActionListener
    By QuincyOrsot in forum Object Oriented Programming
    Replies: 3
    Last Post: April 26th, 2012, 03:00 PM
  4. Game Maker Language and Game Maker and Zelda Classic thread
    By Fira in forum Other Programming Languages
    Replies: 3
    Last Post: April 17th, 2012, 08:59 AM
  5. Simple game that requires me to load game settings from a file
    By 14fenix in forum Java Theory & Questions
    Replies: 5
    Last Post: December 1st, 2011, 09:21 PM

Tags for this Thread