Slick Forums

Discuss the Slick 2D Library
It is currently Fri May 24, 2013 5:20 am

All times are UTC




Post new topic Reply to topic  [ 9 posts ] 
Author Message
PostPosted: Tue Sep 25, 2012 9:12 am 
Offline

Joined: Thu Aug 30, 2012 12:13 am
Posts: 43
Hi everyone, it's lavago and I need help.
I've been trying to get it so that when I click on the entity "crow", it gets rid of it.

I'm using Libraries:
Slick2d
and
MarteEngine

My code is:

Crow.java
Code:
package gamepackage;

import org.lwjgl.input.Mouse;
import org.newdawn.slick.*;
import it.randomtower.engine.ME;
import it.randomtower.engine.entity.*;


public class Crow extends Entity {
   
   public static String CROW = "crow";
   private final String corn = (Corn.CORN);
   private int frameCount = 0;
   private int frame = 0;
   private int nextFrame = 300;
   private int health;
   public int corncount;

   public Crow(float x, float y) throws SlickException {
      super(x, y);
      Image img = new Image("Resources/crow1.png");
      setHitBox(0, 0, img.getWidth(), img.getHeight());
      setGraphic(img);
      addType(CROW);   
      health = 90;
   }
   
   @Override
   public void update(GameContainer gc, int delta) throws SlickException  {
      super.update(gc, delta);
      Input input = gc.getInput();
      int posX = Mouse.getX();
      int posY = Mouse.getY();
      Image crow = new Image("Resources/crow1.png");
      Image crow2 = new Image("Resources/crow2.png");
      Entity corncollide = collide(corn,x,y);
      
      if(corncollide != null) {
         corncount -= 1;
         ME.world.remove(this);
      }
      
      //animation
      frameCount += delta;
      if(frameCount >= nextFrame) {
         frameCount -= nextFrame;
         frame++;
         if (frame % 2 == 0) {
            setGraphic(crow2);
         } else {
            setGraphic(crow);
         }
      }
      
      if(posX == this.x && posY == this.y) {
         if(input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
            health -= 45;
            if(health <= 0) {
               this.destroy();
            }
         }
      }
      
      //Remove image if it's out of the screen.
      y += (.1 * delta);
      if(y > ME.world.getHeight()) {
         this.destroy();
      }
      
   }
   
   public void render(GameContainer gc, Graphics g) throws SlickException {
      super.render(gc, g);
   }
   
   @Override
   public void collisionResponse(Entity other) {
      ME.world.remove(this);
   }

}


Corn.java
Code:
package gamepackage;

import org.newdawn.slick.*;
import org.newdawn.slick.state.StateBasedGame;


import it.randomtower.engine.ME;
import it.randomtower.engine.entity.*;

public class Corn extends Entity {
   
   public static final String CORN = "corn";
   private final String crows = (Crow.CROW);
   
   public Corn(float x, float y) throws SlickException {
      super(x, y);
      Image img = new Image("Resources/Corn.png");
      setHitBox(0, 0, img.getWidth(), img.getHeight());
      setGraphic(img);
      
      addType(CORN);
   }
   
   public void init(GameContainer gc, StateBasedGame game) throws SlickException {
      
   }
   
   @Override
   public void update(GameContainer gc, int delta) throws SlickException {
      super.update(gc, delta);
      Entity crowcollide = collide(crows,x,y);
      if(crowcollide != null) {
         ME.world.remove(this);
      }
   }
   
   @Override
   public void render(GameContainer gc, Graphics g) throws SlickException {
      super.render(gc, g);
   }

}


EasyWorld.java
Code:
package gamepackage;

import it.randomtower.engine.*;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

public class EasyWorld extends World {
   
   Image Background;
   public int corncount = 3;

   public EasyWorld(int id, GameContainer gc) {
      super(id, gc);      
      
   }
   
   @Override
   public void init(GameContainer gc, StateBasedGame game) throws SlickException {
      super.init(gc, game);
      Background = new Image("Resources/Background.png");
      
      //The Corn
      Corn corn = new Corn(50,500);
      add(corn);
      Corn corn2 = new Corn(350,500);
      add(corn2);
      Corn corn3 = new Corn(650,500);
      add(corn3);
      
      //The Crows
      Crow crow1row1 = new Crow(50,100);
      add(crow1row1);
      Crow crow2row1 = new Crow(350,100);
      add(crow2row1);
      Crow crow3row1 = new Crow(650,100);
      add(crow3row1);
   }
   
   @Override
   public void update(GameContainer gc, StateBasedGame game, int delta) throws SlickException {
      super.update(gc, game, delta);
      if(corncount <= 0) {
         gc.exit();
      }
   }
   
   @Override
   public void render(GameContainer gc, StateBasedGame game, Graphics g) throws SlickException {
      Background.draw();
      super.render(gc, game, g);
   }
   
   public int getID(){
         return 0;
      }
   
}


Game.java
Code:
package gamepackage;

import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

public class Game extends StateBasedGame {

   public static int GAME_STATE = 0;
   public static int EASY = 1;

   public Game(String title) {
      super(title);
   }

   @Override
   public void initStatesList(GameContainer gc) throws SlickException {
      
      //UnComment This If You Want To Use The Resource Manager.
      /*
       * try {
         ResourceManager.loadResources("Resources/resources.xml");
      } catch (IOException e) {
         e.printStackTrace();
      }
      */
      addState(new EasyWorld(GAME_STATE, gc));
      enterState(GAME_STATE);
   }
   
   public static void main(String[] args) throws SlickException {
      AppGameContainer app = new AppGameContainer (new Game("Hit Those Crows"));
      app.setDisplayMode(800, 600, false);
      app.setTargetFrameRate(60);
      app.start();
   }
   
}


Top
 Profile  
 
PostPosted: Thu Sep 27, 2012 7:45 pm 
Offline

Joined: Mon Sep 17, 2012 1:59 am
Posts: 35
use Listeners like so:

Code:

public class Game extends StateBasedGame implements KeyListener, MouseListener {



and add:

Code:

@Override
    public void keyPressed(int i, char c) {
    }

    @Override
    public void keyReleased(int i, char c) {
    }

    @Override
    public void mouseClicked(int button, int x, int y, int count) {
    }

    @Override
    public void mousePressed(int button, int x, int y) {
    }

    @Override
    public void mouseMoved(int oldX, int oldY, int newX, int newY) {
    }

    @Override
    public void mouseDragged(int oldX, int oldY, int newX, int newY) {
    }




Top
 Profile  
 
PostPosted: Sat Sep 29, 2012 9:42 pm 
Offline

Joined: Thu Aug 30, 2012 12:13 am
Posts: 43
Where do I add
Code:
@Override
    public void keyPressed(int i, char c) {
    }

    @Override
    public void keyReleased(int i, char c) {
    }

    @Override
    public void mouseClicked(int button, int x, int y, int count) {
    }

    @Override
    public void mousePressed(int button, int x, int y) {
    }

    @Override
    public void mouseMoved(int oldX, int oldY, int newX, int newY) {
    }

    @Override
    public void mouseDragged(int oldX, int oldY, int newX, int newY) {
    }
???


Top
 Profile  
 
PostPosted: Sat Sep 29, 2012 10:25 pm 
Offline
User avatar

Joined: Fri Dec 24, 2010 3:06 am
Posts: 90
Location: USA, Texas
You add those methods to any class that implements KeyListener and MouseListener. For example:

Code:
public class Game extends StateBasedGame implements KeyListener, MouseListener {
    ...
    @Override
    public void keyPressed(int i, char c) {
    }

    @Override
    public void keyReleased(int i, char c) {
    }

    @Override
    public void mouseClicked(int button, int x, int y, int count) {
    }

    @Override
    public void mousePressed(int button, int x, int y) {
    }

    @Override
    public void mouseMoved(int oldX, int oldY, int newX, int newY) {
    }

    @Override
    public void mouseDragged(int oldX, int oldY, int newX, int newY) {
    }
    ...
}


Top
 Profile  
 
PostPosted: Sat Sep 29, 2012 10:29 pm 
Offline

Joined: Sat Sep 29, 2012 10:06 pm
Posts: 13
Don't know if this is right, but you should put it in the update() method


Top
 Profile  
 
PostPosted: Sat Sep 29, 2012 11:24 pm 
Offline

Joined: Mon Sep 17, 2012 1:59 am
Posts: 35
dpek wrote:
You add those methods to any class that implements KeyListener and MouseListener. For example:

Code:
public class Game extends StateBasedGame implements KeyListener, MouseListener {
    ...
    @Override
    public void keyPressed(int i, char c) {
    }

    @Override
    public void keyReleased(int i, char c) {
    }

    @Override
    public void mouseClicked(int button, int x, int y, int count) {
    }

    @Override
    public void mousePressed(int button, int x, int y) {
    }

    @Override
    public void mouseMoved(int oldX, int oldY, int newX, int newY) {
    }

    @Override
    public void mouseDragged(int oldX, int oldY, int newX, int newY) {
    }
    ...
}


you literally copy pasted what I said...


@Lavago: add it anywhere in your Game class.


Top
 Profile  
 
PostPosted: Sun Sep 30, 2012 2:05 am 
Offline
User avatar

Joined: Fri Dec 24, 2010 3:06 am
Posts: 90
Location: USA, Texas
KudoMS wrote:
you literally copy pasted what I said...
Not exactly literally, but yeah, I put the two code blocks together because I thought the separation of the two code blocks confused lavago. Sorry about that.


Top
 Profile  
 
PostPosted: Tue Oct 02, 2012 10:08 am 
Offline

Joined: Thu Aug 30, 2012 12:13 am
Posts: 43
hmm, implemented KeyListener and MouseListener to Crow.java, Corn.java and Game.java, I also add the code to those classes as well, but it still doesn't work.


Top
 Profile  
 
PostPosted: Tue Oct 02, 2012 12:11 pm 
Offline
Slick Zombie

Joined: Wed Apr 02, 2008 1:32 pm
Posts: 1313
Location: Italy
take a look here: https://github.com/Gornova/MarteEngine/ ... ouse-input

_________________
Blog | Last game Gravity Duck tribute | In progress Gravity Duck tribute


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group