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 3 of 3

Thread: A quick Question about classes

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default A quick Question about classes

    Hello and good day, i will try to explain my problem as well as i can.

    I am trying to make an RPG Game using the Java Applet. I currently as of now, have built myself a map builder, gat creator, a map reader, and a basic game engine (i can walk arround ).

    Right now i am trying to develop a collision system for my game, i actually try do it by locating the tile number my hero is at, at detecting a collision with a tile that has been set a collisiondetection boolean of 1 / true. ( I do this by using a GAT file or a file that stores 1's and 0's 1's for (it can not be walked on) and 0's for it can not be walked on). its actually all programmed now.

    But the problem is whenever i try to pass on a value from my MainGame class all over to my collisions class it doesent give me a number at all.

    it works like this: From my MainGame class i create a new object of the Hero Class. From within my Hero Class i create a new object of the Collission Class. wherein collission asks for values (x,y,vx,vy,mapwidth,mapheight,mapname).

    Whatever i seem to do, whenever i create a new class from my hero class, it doesent pass on the arguments correctly to my Collission class.

    Example code:

    in my MainGame.class
    public void start(){
    Hero myHero = new Hero(0,0,50,50,map);
    }

    in my Hero.class
    //The Constructor for my hero class is:
    public Hero(int x,int y, int hp,int mp,String map){
    this.x = x;
    this.y=  y;
    this.hp = hp;
    this.mp = mp;
    this.map = map;
    Collisions collision = new Collisions (this.x,this.y,this.vx,this.vy,32,32,this.map);
    }



    I tried to run some tests so i put a System.out.println(map);
    for each class,

    In classes, MainGame.class and Hero.class the system prints
    map
    map

    (which is the name of my String map variable)

    but for my Hero class it prints out
    null.

    i also tried this with mapwidth,and mapheight coordinates using a getter for the maingame class.
    it prints out:
    800, 600
    800, 600

    and for the collision class it prints out
    0,0

    any help would be great!

    --- Update ---

    Here is the whole code:

    MainGame.class
    package Game;
     
    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Frame;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.net.URL;
    import java.util.ArrayList;
     
    public class MainGame extends Applet implements Runnable, KeyListener {
     
    	/**
    	 * Version 1.0 of myGame Developed by
    	 * Richard Abear @author Abear
    	 * 2012-2013 Davao city Philippines
    	 */
     
     
    	private static final long serialVersionUID = 1L;
    	Graphics second;
    	private Image image;
    	private URL base;
    	private ArrayList tilearray = new ArrayList <Tiles> ();
    	private static Hero myHero;
    	private Image hero;
    	public static Image tilepic,tilepic2,tilepic3,tilepic4,tilepic5,tilepic6;
    	private static String map;
    	private int mapWidth,mapHeight;
    	@Override
    	public void init() {
    		// TODO Auto-generated method stub
    		setSize(800,480);
    		setBackground(Color.WHITE);
    		setFocusable(true);
    		setFont(new Font("Times new Roman",1,12));
    		addKeyListener(this);
    		Frame frame = (Frame) this.getParent().getParent();
    		frame.setTitle("Legendia");
    		this.map = "map";
    		System.out.println("MainGame init() map = " + map);
     
    		try {
    			base = getDocumentBase();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
     
    		// Image declarations
    		hero = getImage(base, "data/hero.png");
    		tilepic = getImage(base, "data/tile1.png");
    		tilepic2 = getImage(base,"data/tile2.png");
    		tilepic3 = getImage(base, "data/tile3.png");
    		tilepic4 = getImage(base,"data/tile4.png");
    		tilepic5 = getImage(base, "data/tile5.png");
    		tilepic6 = getImage(base,"data/tile6.png");
     
    	}
     
     
    	@Override
    	public void update(Graphics g) {
    		// TODO Auto-generated method stub
    		if (image == null) {
    			image = createImage(this.getWidth(), this.getHeight());
    			second = image.getGraphics();
    		}
     
    		second.setColor(getBackground());
    		second.fillRect(0, 0, getWidth(), getHeight());
    		second.setColor(getForeground());
    		paint(second);
     
    		g.drawImage(image, 0, 0, this);
    	}
     
    	@Override
    	public void paint(Graphics g) {
    		// TODO Auto-generated method stub
     
     
     
    		for(int i=0; i < tilearray.size(); i++){
    			Tiles t = (Tiles) tilearray.get(i);
    			g.drawImage(t.getT1(),t.getTilex()*32,t.getTiley()*32,this);
    		}
    		Tiles t= (Tiles) tilearray.get(1);
    		g.drawImage(t.getT1(), 50, 50, this);
    		g.drawImage(hero, myHero.getX()-32, myHero.getY()-32, this);
    		g.drawString("X-Position" + myHero.getX(), 200, 200);
    		g.drawString("Y-Position" + myHero.getY(),300,200);
    	}
     
    	@Override
    	public void start() {
    		// TODO Auto-generated method stub
    		myHero = new Hero(250, 250, 50, 50,this.map);
    		System.out.println("Main game Start() map = " + this.map);
    		Thread thread = new Thread(this);
    		thread.start();
     
    		try{
    			loadmap("data/" + map);
    			}catch(IOException e){
    				e.printStackTrace();
    			}
    	}
     
    	public MainGame() {
    		// TODO Auto-generated constructor stub
    	}
     
     
    	@Override
    	public void run() {
    		// TODO Auto-generated method stub
     
    		while (true) {
    			myHero.Update();
     
     
     
     
    			repaint();
    			try {
    				Thread.sleep(17);
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    	private void loadmap(String string) throws IOException {
    		// TODO Auto-generated method stub
    		ArrayList lines = new ArrayList();
    		int width = 0,height=0;
     
    		BufferedReader reader = new BufferedReader(new FileReader(string + ".txt"));
     
     
    		while(true){
    			String line = reader.readLine();
     
    			if(line == null){
    				reader.close();
    				break;
    			}
     
    			lines.add(line);
    			width = Math.max(width, line.length());
     
    		}
    	height = lines.size();
     
    	mapWidth = width*32;
    	mapHeight = height*32;
     
    	for(int j = 0; j < height; j++){
    		String line = (String) lines.get(j);
    		for(int i = 0; i < width; i++){
    			if(i < line.length()){
    				char ch = line.charAt(i);
    				Tiles t = new Tiles(i,j,Character.getNumericValue(ch));
    				tilearray.add(t);
    			}
    		}
    	}
    	}
     
     
    	// KeyListener
     
    	@Override
    	public void keyPressed(KeyEvent e) {
    		// TODO Auto-generated method stub
    		switch (e.getKeyCode()) {
    			case KeyEvent.VK_UP:
    				myHero.setMoveUp(true);
    				System.out.println("entered UP");
    				break;
    			case KeyEvent.VK_DOWN:
    				myHero.setMoveDown(true);
    				break;
    			case KeyEvent.VK_LEFT:
    				myHero.setMoveLeft(true);
    				break;
    			case KeyEvent.VK_RIGHT:
    				myHero.setMoveRight(true);
    				break;
    		}
     
    	}
     
    	@Override
    	public void keyReleased(KeyEvent e) {
    		// TODO Auto-generated method stub
     
    		switch (e.getKeyCode()) {
    		case KeyEvent.VK_UP:
    			myHero.setMoveUp(false);
    			myHero.setVy(0);
    			break;
    		case KeyEvent.VK_DOWN:
    			myHero.setMoveDown(false);
    			myHero.setVy(0);
    			break;
    		case KeyEvent.VK_LEFT:
    			myHero.setMoveLeft(false);
    			myHero.setVx(0);
    			break;
    		case KeyEvent.VK_RIGHT:
    			myHero.setMoveRight(false);
    			myHero.setVx(0);
    			break;
    	}
    	}
     
    	@Override
    	public void keyTyped(KeyEvent e) {
    		// TODO Auto-generated method stub
    		switch (e.getKeyCode()) {
    		case KeyEvent.VK_UP:
    			myHero.setMoveUp(true);
    			break;
    		case KeyEvent.VK_DOWN:
    			myHero.setMoveDown(true);
    			break;
    		case KeyEvent.VK_LEFT:
    			myHero.setMoveLeft(true);
    			break;
    		case KeyEvent.VK_RIGHT:
    			myHero.setMoveRight(true);
    			break;
    	}
    	}
     
     
    	int getMapWidth() {
    		return mapWidth;
    	}
     
     
    	int getMapHeight() {
    		return mapHeight;
    	}
     
     
    	void setMapWidth(int mapWidth) {
    		this.mapWidth = mapWidth;
    	}
     
     
    	void setMapHeight(int mapHeight) {
    		this.mapHeight = mapHeight;
    	}
     
     
    	String getMap() {
    		return map;
    	}
     
     
    	void setMap(String map) {
    		this.map = map;
    	}
     
     
     
     
    }



    Hero Class
    package Game;
     
    import java.io.IOException;
     
    public class Hero {
    	boolean isMoveUp() {
    		return moveUp;
    	}
     
    	boolean isMoveDown() {
    		return moveDown;
    	}
     
    	boolean isMoveLeft() {
    		return moveLeft;
    	}
     
    	boolean isMoveRight() {
    		return moveRight;
    	}
     
    	void setMoveUp(boolean moveUp) {
    		this.moveUp = moveUp;
    	}
     
    	void setMoveDown(boolean moveDown) {
    		this.moveDown = moveDown;
    	}
     
    	void setMoveLeft(boolean moveLeft) {
    		this.moveLeft = moveLeft;
    	}
     
    	void setMoveRight(boolean moveRight) {
    		this.moveRight = moveRight;
    	}
     
    	int getHp() {
    		return hp;
    	}
     
    	int getMp() {
    		return mp;
    	}
     
    	float getVx() {
    		return vx;
    	}
     
    	float getVy() {
    		return vy;
    	}
     
    	int getX() {
    		return x;
    	}
     
    	int getY() {
    		return y;
    	}
     
    	void setHp(int hp) {
    		this.hp = hp;
    	}
     
    	void setMp(int mp) {
    		this.mp = mp;
    	}
     
    	void setVx(float vx) {
    		this.vx = vx;
    	}
     
    	void setVy(float vy) {
    		this.vy = vy;
    	}
     
    	void setX(int x) {
    		this.x = x;
    	}
     
    	void setY(int y) {
    		this.y = y;
    	}
     
    	private int hp, mp;
    	private float vx, vy;
    	private int x, y;
    	private String map;
    	private boolean moveUp, moveDown, moveLeft, moveRight,colide;
    	private Collision collision = new Collision(this.x,this.y,this.vx,this.vy,32,32,this.map);
     
    	public Hero(int x, int y, int hp, int mp,String map) {
    		this.x = x;
    		this.y = y;
    		this.hp = hp;
    		this.mp = mp;
    		this.map = map;
    		vx = 0;
    		vy = 0;
    		moveUp = moveLeft = moveRight = moveDown = false;
    		System.out.println("Hero class map = " + map);
    	}
     
    	public void Update() {
    		HandleCollision();
    		HandleMovement();
    	}
    	public void HandleCollision(){
    		collision.update(x, y, vx, vy);
    		colide = collision.isCollision();
    		while(colide == true){
    			if(vx > 0){
    				vx-=1;
    			}else if(vx < 0){
    				vx+=1;
    			}
    		}
    	}
    	public void HandleMovement() {
    		if (moveUp == true && colide == false) {
    			if (y*32 >= MainGame.HEIGHT - 32 || y*32 <= 32) {
    				vy = (float) -1;
    			} else {
    				vy = 0;
    			}
    		} else if (moveDown == true) {
    			if (y*32 >= MainGame.HEIGHT + 32 || y*32 <= 32) {
    				vy = (float) 1;
    			} else {
    				vy = 0;
    			}
    		} else if (moveLeft == true) {
    			if (x*32 >= MainGame.HEIGHT || x*32 <= 32) {
    				vx = (float) -1;
    			} else {
    				vy = 0;
    			}
    		} else if (moveRight == true) {
    			if (x*32 >= MainGame.HEIGHT || x*32 <= 32) {
    				vx = (float) 1;
    			} else {
    				vy = 0;
    			}
    		}
     
    		AdjustPosition();
    	}
     
    	public void AdjustPosition() {
    		if(x <= 32) x =34;
    		if(y <= 32) y =34;
    		if(y >= 480) y=478;
    		if(x >= 800) x=798;
    		x += vx;
    		y += vy;
    	}
     
    }


    Collissions class
    package Game;
     
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import Game.MainGame;
    import Game.Hero;
    public class Collision {
     
     
    	private int x,y,width,height;
    	private float vx,vy;
    	private int mapWidth,mapHeight;
    	private boolean collision;
    	private MainGame main = new MainGame();
    	private String map;
    	private ArrayList gatarray = new ArrayList<Gat>();
     
    	public Collision(int x, int y, float vx, float vy,int width, int height, String map)  {
    		// TODO Auto-generated constructor stub
    		this.x = x;
    		this.y = y;
    		this.vx = vx;
    		this.vy = vy;
    		this.width = width;
    		this.height = height;
    		this.map = map;
    		System.out.println("Collission map = " + map);
    		try {
    			loadmap(map);
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
     
    	private void loadmap(String map) throws IOException {
    		// TODO Auto-generated method stub
     
    		BufferedReader fr = new BufferedReader(new FileReader("data/" + "map" + ".gat"));
    		ArrayList lines = new ArrayList();
    		while(true){
    			String line = fr.readLine();
    			if(line == null){
    				fr.close();
    				break;
    			}
     
    			lines.add(line);
    		}
     
    		for(int i = 0; i < height/32; i++){
    			String line = (String) lines.get(i);
    			for(int j=0; j < width/32; j++){
    				char ch = line.charAt(j);
    				Gat g = new Gat(j,i,Character.getNumericValue(ch));
    				System.out.println("COLLISION DETECTION GAT ARRAY ADDED Gat at " + j + " " + i + " " + ch);
    				gatarray.add(g);
    			}
    		}
    	}
     
     
    	public void update(int x, int y, float vx, float vy){
    		this.x = x;
    		this.y = y;
    		this.vx = vx;
    		this.vy = vy;
    		System.out.println("This width = " + main.getMapWidth() +  " This height = " + main.getMapHeight());
    		collision = collisiondetect(this.x,this.y,this.vx,this.vy);
    	}
    	private boolean collisiondetect(int x,int y, float vx, float vy){
    		int location = Locate(x,y);
    		if(vx > 0 || vx < 0){
    			int tileleft = ((Gat) gatarray.get(location-1)).getType();
    			if(tileleft == 1) return true;
     
    			int tileright = ((Gat) gatarray.get(location+1)).getType();
    			if(tileright == 1){System.out.println("Collision Detected at tile num: " + location + " VX = " + vx + " VY = " + vy); return true; }
    			else {return false;}
    		}
    		return false;
    	}
     
     
    	private int Locate(int x,int y){
    		int width,height;
    		int xtile,ytile;
    		xtile=ytile=0;
    		width = main.getWidth();
    		height = main.getHeight();
     
    		while(x > main.getMapWidth()/32){
    			xtile++;
    			x-=32;
    		}
    		while(y > main.getMapHeight()/32){
    			ytile+=width/32;
    			y-=32;
    		}
     
    		int tilenum = xtile+ytile;
    		return tilenum;
    	}
     
    	//Getters and Setters
    	int getX() {
    		return x;
    	}
     
    	int getY() {
    		return y;
    	}
     
    	float getVx() {
    		return vx;
    	}
     
    	float getVy() {
    		return vy;
    	}
     
    	int getWidth() {
    		return width;
    	}
     
    	int getHeight() {
    		return height;
    	}
     
    	void setX(int x) {
    		this.x = x;
    	}
     
    	void setY(int y) {
    		this.y = y;
    	}
     
    	void setVx(int vx) {
    		this.vx = vx;
    	}
     
    	void setVy(int vy) {
    		this.vy = vy;
    	}
     
    	void setWidth(int width) {
    		this.width = width;
    	}
     
    	void setHeight(int height) {
    		this.height = height;
    	}
    	boolean isCollision() {
    		return collision;
    	}
    	void setCollision(boolean collision) {
    		this.collision = collision;
    	}
     
     
     
    }

    And finally the gat class
    package Game;
    import java.awt.Image;
     
     
    public class Gat {
    	Image getImg() {
    		return img;
    	}
     
    	void setImg(Image img) {
    		this.img = img;
    	}
     
    	int getX() {
    		return x;
    	}
     
    	int getY() {
    		return y;
    	}
     
    	int getType() {
    		return type;
    	}
     
    	void setX(int x) {
    		this.x = x;
    	}
     
    	void setY(int y) {
    		this.y = y;
     
     
     
    	}
     
    	void setType(int type) {
    		this.type = type;
    	}
     
    	private int x, y, type;
    	private Image img;
    	public Gat(int x,int y, int type) {
    		// TODO Auto-generated constructor stub
    		this.x = x;
    		this.y = y;
    		this.type = type;
     
    	}
     
     
     
    }


    thats about it.

    i'm pretty sure everything else is correctly coded.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: A quick Question about classes

    The posted code shows 2 different constructors for the Hero class. The first is the short bit of code in the second posted code and the second constructor is the full class. Which one should be used?

    The first one has a local Collision class variable which will go away when the constructor exits.
    The second one uses the values of the variables before they get any values.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    boyton (May 5th, 2013)

  4. #3
    Junior Member
    Join Date
    May 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: A quick Question about classes

    thanks for taking the time to reply norm!

    The constructor that's supposed to be used is the full class.

    I actually realized that first statement of yours saying that the collision class variable will go away when the constructor exists. I made a few changes to the code and yet it still doesent work. Right now in my maingame class, i create collision class after my hero class. then i simply try to pass on the values from my hero class to my collision class.

    like so:


    @Override
    	public void start() {
    		// TODO Auto-generated method stub
    		myHero = new Hero(250, 250, 50, 50,this.map);
    		collision = new Collision(myHero.getX(),myHero.getY(),myHero.getVx(),myHero.getVy(),mapWidth,mapHeight,this.map);
    		System.out.println("Main game Start() map = " + this.map);
    		Thread thread = new Thread(this);
    		thread.start();
     
    		try{
    			loadmap("data/" + map);
    			}catch(IOException e){
    				e.printStackTrace();
    			}
    	}

    p.s. in my main game class i declard collision as: private static Collision collision;

    --- Update ---

    Hey guys, i actually fixed it now. i figured that it wasn't passing any variables onto my collision class from where i put it in the code. i created a new class of collision right after i execute my map loading (which is like the last function that my program uses before actually running) and it worked. it loads everything

    BUT THERE IS STILL A PROBLEM THAT PERSISTS

    i get this error:

    Exception in thread "Thread-3" java.lang.NullPointerException
    at Game.MainGame.run(MainGame.java:130)
    at java.lang.Thread.run(Unknown Source)


    this is the code that surrounds line 130 of my maingame.java:

    @Override
    	public void run() {
    		// TODO Auto-generated method stub
     
    		while (true) {
    			myHero.setColide(returnCollision());
    			myHero.Update();
    			collision.update(myHero.getX(), myHero.getY(), myHero.getVx(), myHero.getVy());
     
     
     
    			repaint();
    			try {
    				Thread.sleep(17);
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}
    	}

Similar Threads

  1. quick question
    By jco69 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 19th, 2013, 09:38 AM
  2. Quick question
    By l1nk3 in forum Java Theory & Questions
    Replies: 1
    Last Post: February 24th, 2012, 02:02 PM
  3. quick question, please HELP!
    By Nemus in forum What's Wrong With My Code?
    Replies: 14
    Last Post: September 29th, 2011, 01:23 PM
  4. Another Quick Question
    By Jacksontbh in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 1st, 2011, 07:18 AM
  5. Hi, quick question
    By curras in forum Member Introductions
    Replies: 1
    Last Post: March 21st, 2011, 03:21 PM

Tags for this Thread