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

Thread: 2D Survival Platformer Help

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question 2D Survival Platformer Help

    Hey guys, I'm programming a 2D pixel-based platformer, kind of like Terraria, but I ran into a problem with the crafting mechanism. My goal was to create a 3x3 crafting space when you hit "p" while holding a workbench. However, what ended up happening was a straight line of cubic crafting spaces. Here's what you need to know to help me troubleshoot -

    NOTE: I realize that this is a rediculous amount of code for such a small problem, but I feel you need all of it to sufficiently understand my goal. Any help would be much appreciated :D

    ANOTHER NOTE: This is in ------------> JRE 6 <-----------------

    Workbench class
    package net.figgle.minecraftplatformer;
     
    import java.awt.Graphics;
    import java.awt.Point;
    import java.awt.Rectangle;
    import java.awt.event.MouseEvent;
     
    public class WorkBench {
    	public static Cell[] workBench = new Cell[Tile.workBenchLength * Tile.workBenchHeight];
    	public static Cell[] product = new Cell[Tile.productLength * Tile.productHeight];
     
    	private static boolean isHolding = false;
    	public static boolean isWorkBenchOpen = false;
     
    	public static int[] holdingID = Tile.air;
    	private int yOffSet = 150;
     
    	public WorkBench() {
    		int x = 0, y = 0;
    		for(int i = 0; i< workBench.length; i++) {
    			workBench[i] = new Cell(new Rectangle((Component.pixel.width >> 1) - ((Tile.workBenchLength * (Tile.invCellSize + Tile.invCellSpace)) >> 1) + (x * (Tile.invCellSize + Tile.invCellSpace)), Component.pixel.height - (Tile.invCellSize + Tile.invBorderSpace) - (Tile.workBenchHeight * (Tile.invCellSize + Tile.invCellSpace)) + (y * (Tile.invCellSize + Tile.invCellSpace)) - (yOffSet), Tile.invCellSize, Tile.invCellSize), Tile.air);
     
    			x++;
    			if(x == Tile.invLength) {
    				x = 0;
    				y++;
    			}		
    		}
    		for(int i = 0; i< product.length; i++) {
    			product[i] = new Cell(new Rectangle((Component.pixel.width >> 1) - ((Tile.productLength * (Tile.invCellSize + Tile.invCellSpace)) >> 1) + (x * (Tile.invCellSize + Tile.invCellSpace)) + 58, Component.pixel.height - (Tile.invCellSize + Tile.invBorderSpace) - (Tile.productHeight * (Tile.invCellSize + Tile.invCellSpace)) + (y * (Tile.invCellSize + Tile.invCellSpace)) - (yOffSet + 118), Tile.invCellSize, Tile.invCellSize), Tile.air);
     
    			x++;
    			if(x == Tile.productLength) {
    				x = 0;
    				y++;
    			}		
    		}
     
    		workBench[0].id = Tile.earth;
    		workBench[2].id = Tile.lava;
    	}
     
    	public static void click(MouseEvent e) {
    		if(e.getButton() == MouseEvent.BUTTON1) {
    			if(isWorkBenchOpen) {
    				for(int i = 0; i < workBench.length; i++) {
    					if(workBench[i].contains(new Point(Component.mse.x / Component.pixelSize, Component.mse.y / Component.pixelSize))){ 
    						if(workBench[i].id != Tile.air && !isHolding) {
    						holdingID = workBench[i].id;
    						workBench[i].id = Tile.air;
     
    						isHolding = true;
    						} else if(isHolding && workBench[i].id == Tile.air) {
    							workBench[i].id = holdingID;
     
    							isHolding = false;
    						} else if (isHolding && workBench[i].id != Tile.air) {
    							int[] con = workBench[i].id;
     
    							workBench[i].id = holdingID;
    							holdingID = con;
    						}
    					}
    				}	for(int i = 0; i < product.length; i++) {
    					if(product[i].contains(new Point(Component.mse.x / Component.pixelSize, Component.mse.y / Component.pixelSize))){ 
    						if(product[i].id != Tile.air && !isHolding) {
    						holdingID = product[i].id;
    						product[i].id = Tile.air;
     
    						isHolding = true;
    						} else if(isHolding && product[i].id == Tile.air) {
    							product[i].id = holdingID;
     
    							isHolding = false;
    						} else if (isHolding && product[i].id != Tile.air) {
    							int[] con = product[i].id;
     
    							product[i].id = holdingID;
    							holdingID = con;
    						}
    					}
    				}
    				for(int i = 0; i < Inventory.invBar.length; i++) {
    					if(Inventory.invBar[i].contains(new Point(Component.mse.x / Component.pixelSize, Component.mse.y / Component.pixelSize))){ 
    						if(Inventory.invBar[i].id != Tile.air && !isHolding) {
    						holdingID = Inventory.invBar[i].id;
    						Inventory.invBar[i].id = Tile.air;
     
    						isHolding = true;
    						} else if(isHolding && Inventory.invBar[i].id == Tile.air) {
    							Inventory.invBar[i].id = holdingID;
     
    							isHolding = false;
    						} else if (isHolding && Inventory.invBar[i].id != Tile.air) {
    							int[] con = Inventory.invBar[i].id;
     
    							Inventory.invBar[i].id = holdingID;
    							holdingID = con;
    						}
    					}
    				}
     
    				for(int i = 0; i < Inventory.invBag.length; i++) {
    					if(Inventory.invBag[i].contains(new Point(Component.mse.x / Component.pixelSize, Component.mse.y / Component.pixelSize))){ 
    						if(Inventory.invBag[i].id != Tile.air && !isHolding) {
    						holdingID = Inventory.invBag[i].id;
    						Inventory.invBag[i].id = Tile.air;
     
    						isHolding = true;
    						} else if(isHolding && Inventory.invBag[i].id == Tile.air) {
    							Inventory.invBag[i].id = holdingID;
     
    							isHolding = false;
    						} else if (isHolding && Inventory.invBag[i].id != Tile.air) {
    							int[] con = Inventory.invBag[i].id;
     
    							Inventory.invBag[i].id = holdingID;
    							holdingID = con;
    						}
    					}
    				}
    			}
    		}
    	}
     
    	public void checkRecipes() {
    		if(product[0].id == Tile.air) {
    			if(workBench[0].id == Tile.earth && workBench[1].id == Tile.lava) {
    				if(holdingID == Tile.sand) {
    					workBench[0].id = Tile.air;
    					workBench[1].id = Tile.air;
    					product[0].id = Tile.air;
    				}
    				if(holdingID != Tile.sand) {
    					product[0].id = Tile.sand;
    				}
    			} else {
    				product[0].id = Tile.air;
    			}
    		}
    	}	
     
    	public void render(Graphics g) {
    		if(isWorkBenchOpen) {
    			for(int i=0;i<workBench.length;i++) { 
    				workBench[i].render(g, false);
    			}
    			for(int i=0;i<product.length;i++) { 
    				product[i].render(g, false);
    			}
    		}
    		if(isHolding) {
    			g.drawImage(Tile.tileset_terrain, (Component.mse.x / Component.pixelSize) - (Tile.invCellSize / 2) + Tile.invItemBorder, (Component.mse.y / Component.pixelSize) - (Tile.invCellSize / 2) + Tile.invItemBorder, (Component.mse.x / Component.pixelSize) - (Tile.invCellSize / 2) + Tile.invCellSize - Tile.invItemBorder, (Component.mse.y / Component.pixelSize) - (Tile.invCellSize / 2) + Tile.invCellSize - Tile.invItemBorder, holdingID[0] * Tile.tileSize, holdingID[1] * Tile.tileSize, holdingID[0] * Tile.tileSize + Tile.tileSize, holdingID[1] * Tile.tileSize+ Tile.tileSize, null);
    			}
    		}
    	}
     
     
    [B][U]Inventory class[U][B][/B][/U][/U][/B]
    package net.figgle.minecraftplatformer;
     
    import java.awt.*;
    import java.awt.event.MouseEvent;
     
    public class Inventory {
    	public static Cell[] invBar = new Cell[Tile.invLength];
    	public static Cell[] invBag = new Cell[Tile.invLength * Tile.invHeight];
     
    	public static boolean isOpen = false;
    	public static boolean isHolding = false;
     
    	public static int selected = 0;
    	public static int[] holdingID = Tile.air;
     
    	public Inventory() {
    		for(int i=0;i<invBar.length;i++) {
    			invBar[i] = new Cell(new Rectangle((Component.pixel.width >> 1) - ((Tile.invLength * (Tile.invCellSize + Tile.invCellSpace)) >> 1) + (i * (Tile.invCellSize + Tile.invCellSpace)), Component.pixel.height - (Tile.invCellSize+ Tile.invBorderSpace), Tile.invCellSize, Tile.invCellSize), Tile.air);
    		}
     
    		int x = 0, y = 0;
    		for(int i = 0; i< invBag.length; i++) {
    			invBag[i] = new Cell(new Rectangle((Component.pixel.width >> 1) - ((Tile.invLength * (Tile.invCellSize + Tile.invCellSpace)) >> 1) + (x * (Tile.invCellSize + Tile.invCellSpace)), Component.pixel.height - (Tile.invCellSize + Tile.invBorderSpace) - (Tile.invHeight * (Tile.invCellSize + Tile.invCellSpace)) + (y * (Tile.invCellSize + Tile.invCellSpace)), Tile.invCellSize, Tile.invCellSize), Tile.air);
     
    			x++;
    			if(x == Tile.invLength) {
    				x = 0;
    				y++;
    			}		
    		}
     
    		invBar[0].id = Tile.earth;
    		invBar[1].id = Tile.grass;
    		invBar[2].id = Tile.tnt;
    		invBar[3].id = Tile.workBench;
    		invBar[4].id = Tile.leaves;
    		invBar[5].id = Tile.wood;
    		invBar[6].id = Tile.sand;
    		invBar[7].id = Tile.lava;
    	}
     
    	public static void click(MouseEvent e) {
    		if(e.getButton() == 1) {
    			if(isOpen) {
    				for(int i = 0; i < invBar.length; i++) {
    					if(invBar[i].contains(new Point(Component.mse.x / Component.pixelSize, Component.mse.y / Component.pixelSize))){ 
    						if(invBar[i].id != Tile.air && !isHolding) {
    						holdingID = invBar[i].id;
    						invBar[i].id = Tile.air;
     
    						isHolding = true;
    						} else if(isHolding && invBar[i].id == Tile.air) {
    							invBar[i].id = holdingID;
     
    							isHolding = false;
    						} else if (isHolding && invBar[i].id != Tile.air) {
    							int[] con = invBar[i].id;
     
    							invBar[i].id = holdingID;
    							holdingID = con;
    						}
    					}
    				}
     
    				for(int i = 0; i < invBag.length; i++) {
    					if(invBag[i].contains(new Point(Component.mse.x / Component.pixelSize, Component.mse.y / Component.pixelSize))){ 
    						if(invBag[i].id != Tile.air && !isHolding) {
    						holdingID = invBag[i].id;
    						invBag[i].id = Tile.air;
     
    						isHolding = true;
    						} else if(isHolding && invBag[i].id == Tile.air) {
    							invBag[i].id = holdingID;
     
    							isHolding = false;
    						} else if (isHolding && invBag[i].id != Tile.air) {
    							int[] con = invBag[i].id;
     
    							invBag[i].id = holdingID;
    							holdingID = con;
    						}
    					}
    				}
    			}
    		}
    	}
     
    	public void render(Graphics g) {
    		for(int i=0;i<invBar.length;i++) {
    			boolean isSelected = false;
    			if(i == selected) {
    				isSelected = true;
    			} 
     
    			invBar[i].render(g, isSelected);
    		}
     
    		if(isOpen) {
    			for(int i=0;i<invBag.length;i++) { 
    				invBag[i].render(g, false);
    			}
    		}
     
    		if(isHolding) {
    			g.drawImage(Tile.tileset_terrain, (Component.mse.x / Component.pixelSize) - (Tile.invCellSize / 2) + Tile.invItemBorder, (Component.mse.y / Component.pixelSize) - (Tile.invCellSize / 2) + Tile.invItemBorder, (Component.mse.x / Component.pixelSize) - (Tile.invCellSize / 2) + Tile.invCellSize - Tile.invItemBorder, (Component.mse.y / Component.pixelSize) - (Tile.invCellSize / 2) + Tile.invCellSize - Tile.invItemBorder, holdingID[0] * Tile.tileSize, holdingID[1] * Tile.tileSize, holdingID[0] * Tile.tileSize + Tile.tileSize, holdingID[1] * Tile.tileSize+ Tile.tileSize, null);
    			}
    		}
    	}
     
                                    [U][B]Component class[U][B][/B][/U][/B][/U]
     
    package net.figgle.minecraftplatformer;
     
    import java.applet.*;
     
    import javax.swing.*;
     
    import java.awt.*;
    import java.util.*;
     
    public class Component extends Applet implements Runnable {
    	private static final long serialVersionUID = 1L;
     
    	public static byte pixelSize = 2;
     
    	public static int moveFromBorder = 0;
    	public static double sX = moveFromBorder, sY = moveFromBorder;
    	public static double dir = 0;
     
    	public static int width = 700;
    	public static int height = 560;
     
    	public int timer = 0;
    	public byte movingTimer = 0;
     
    	public static Dimension realSize = new Dimension(0, 0);
    	public static Dimension size = new Dimension(700, 560);
    	public static Dimension pixel = new Dimension(size.width / pixelSize, size.height / pixelSize);
     
    	public static Point mse = new Point(0, 0);
     
    	public static String name = "Minegeist v1.0.0 Alpha ";
    	public static String deathText = "You died!";
     
    	public static boolean isRunning = false;
    	public static boolean isMoving = false;
    	public static boolean isJumping = false;
    	public static boolean isMouseLeft = false;
    	public static boolean isMouseRight = false;
     
    	private Image screen;
     
    	public int fps;
    	public int TotalTime;
     
    	public static Level level;
    	public static Character character;
    	public static Inventory inventory;
    	public static WorkBench workBench;
    	public static Sky sky;
    	public static ArrayList<Mob> mob = new ArrayList<Mob>();
    	public static Spawner spawner;
    	public static Checker checker;
     
    	public Component() {
    		setPreferredSize(size);
     
    		addKeyListener(new Listening());
    		addMouseListener(new Listening());
    		addMouseMotionListener(new Listening());
    		addMouseWheelListener(new Listening());
    	}
     
    	public void start() {
    		frame = new JFrame();
     
    		Toolkit toolkit = Toolkit.getDefaultToolkit();
    		Image cursor = toolkit.getImage("res/Cursors/cursor.png");
    		Point cursorHotSpot = new Point(17, 17);
    		Cursor customCursor = toolkit.createCustomCursor(cursor, cursorHotSpot, "Cursor");
    		frame.setCursor(customCursor);
     
    		realSize = new Dimension(frame.getWidth(), frame.getHeight());
     
    		frame.setTitle(name);
    		frame.setSize(new Dimension(WIDTH, HEIGHT));
    		frame.add(this);
     
    		frame.pack();
    		frame.setLocationRelativeTo(null);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setResizable(false);
     
    		frame.setVisible(true);
     
    		frame.setIconImage(new ImageIcon("res/windowIcon.png").getImage());
     
     
    		//Defining objects etc.
    		new Tile(); //Loading images
    		level = new Level();
    		character = new Character(Tile.tileSize, Tile.tileSize*2);
    		inventory = new Inventory();
    		sky = new Sky();
    		workBench = new WorkBench();
    		spawner = new Spawner();
    		checker = new Checker();
     
    		//starting le game loop 
    		isRunning = true;
    		new Thread(this).start();
    	}
     
    	public void stop() {
    		isRunning = false;
    	}
     
    	private static JFrame frame;
    	public static void main(String args[]) {
    		Component component = new Component();
     
    		new Menu(component);
    	}
     
    	public void tick() {
    		if(frame.getWidth()!= realSize.width||frame.getHeight()!= realSize.height){
    			frame.pack();
    		}
     
    		character.tick();
    		level.tick((int) sX, (int) sY, (pixel.width / Tile.tileSize) + 2, (pixel.height / Tile.tileSize) + 2);
    		sky.tick();
    		checker.tick();
    		workBench.checkRecipes();
     
    		for(int i = 0; i < mob.toArray().length; i++) {
    			mob.get(i).tick();
    		}
    	}
     
    	public void render() {
    		Graphics g = screen.getGraphics();
     
    		//Drawing things
    		sky.render(g);
     
    		level.render(g, (int) sX, (int) sY,(pixel.width / Tile.tileSize) + 2, (pixel.height / Tile.tileSize) + 2);
    		character.render(g);
     
    		for(int i = 0; i < mob.toArray().length; i++) {
    			mob.get(i).render(g);
    		}
    		inventory.render(g);
    		workBench.render(g);
     
    		g.setColor(Color.GREEN);
    		g.drawString(fps + " fps", 1, 10);
    		g.drawString("X: " + Math.round(sX/18), 315, 10);
    		g.drawString("Y: " + Math.round(sY/18), 315, 20);
     
    		if(character.isDead){
    			g.setColor(Color.BLACK);
    			g.fillRect(0,  0,  width, height);
    			g.setColor(Color.RED);
    			g.setFont(new Font("Courier New", Font.BOLD, 24));
    			g.drawString(deathText, 80, 135);
    		}
     
    		if(timer < 250) {
    			timer++;
    			movingTimer++;
    			g.setColor(Color.BLACK);
    			g.fillRect(0, 0, width, height);
    			if(movingTimer > 10) {
    				g.drawImage(Tile.movingCharacter, timer + 35, 150, null);
    			} else {
    				g.drawImage(Tile.movingCharacter2, timer + 35, 150, null);
    			}
    			if(movingTimer > 20) {
    				movingTimer = 0;
    			}
    			g.setColor(Color.WHITE);
    			g.setFont(new Font("Courier New", Font.BOLD, 25));
    			g.drawString("Loading...", 125, 80);
    			g.setColor(Color.GREEN) ;
    			g.fillRect(50, 100, timer, 40);
    			g.setColor(Color.BLUE);
    			g.drawRect(50, 100, 250, 40);
    		}
     
    		g = getGraphics();
     
    		g.drawImage(screen, 0, 0, size.width, size.height, 0, 0, pixel.width, pixel.height, null);
    		g.dispose();
     
     
    	}
     
    	public void run() {
    		screen = createVolatileImage(pixel.width, pixel.height);
     
    		int frames = 0;
    		double nonProcessedSeconds = 0;
    		long previousTime = System.nanoTime();
    		double secondsPerTick = 1 / 60.0;
    		int TickCount = 0;
    		boolean hasTicked = false;
    		int TotalTime = 0;
     
    		while(isRunning) {
    			long currentTime = System.nanoTime();
    			long passedTime = currentTime - previousTime;
    			previousTime = currentTime;
    			nonProcessedSeconds += passedTime / 1000000000.0;
     
    			while(nonProcessedSeconds > secondsPerTick) {
    				tick();
    				nonProcessedSeconds -= secondsPerTick;
    				hasTicked = true;
    				TickCount++;
    					if(TickCount % 60 == 0) {
    						previousTime += 1000;
    						fps = frames;
    						frames = 0;
    					}
    			}
    			if(hasTicked){
    				frames++;
    			}
     
    			render();
    			frames++;
     
    			frames++;
    			requestFocus();
    			tick();
    			render();
     
    			try {
    				Thread.sleep(5);
    			} catch(Exception e) { }
    		}
    	}
    }

    NOW...
    Like I said, I realize this is a lot of code. But it would be highly appreciated if you would try and help me troubleshoot. Thanks :)
    Last edited by Figgle; April 18th, 2014 at 08:58 AM.


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

    Default Re: 2D Survival Platformer Help

    How are you debugging the code? I don't see any println() statements for showing what the code is doing and where it is executing.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2D Survival Platformer Help

    I'm only trying to convert the line of squares that you get from hitting "P" while holding a workbench. I know all of the code is executing and when it's supposed to, but the coding itself is incorrect. I just want it to be a 3x3 square with one block to the left for crafting.

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

    Default Re: 2D Survival Platformer Help

    the coding itself is incorrect
    Do you know what the code that is incorrect is supposed to do?
    Can you describe in detail what it is supposed to do so the logic you have can be compared to the code to see if the code follows the design?

    Have you tried using println()s to show what the code is doing so you can make changes to make it do what you want.

    The code has used the name of a Java se class: Component.
    It's better if you use your own names, not java se class names.

    --- Update ---

    Can you make a small, complete program that compiles, executes and shows the problem?
    The posted code has too many lines and is missing too many class definitions.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2D Survival Platformer Help

    1) The incorrect code is supposed to print a 3x3 cube as well as one stray cube, like in Minecraft crafting.
    2) This part here -

    public class WorkBench {
    public static Cell[] workBench = new Cell[Tile.workBenchLength * Tile.workBenchHeight];
    public static Cell[] product = new Cell[Tile.productLength * Tile.productHeight];

    AND:

    public WorkBench() {
    int x = 0, y = 0;
    for(int i = 0; i< workBench.length; i++) {
    workBench[i] = new Cell(new Rectangle((Component.pixel.width >> 1) - ((Tile.workBenchLength * (Tile.invCellSize + Tile.invCellSpace)) >> 1) + (x * (Tile.invCellSize + Tile.invCellSpace)), Component.pixel.height - (Tile.invCellSize + Tile.invBorderSpace) - (Tile.workBenchHeight * (Tile.invCellSize + Tile.invCellSpace)) + (y * (Tile.invCellSize + Tile.invCellSpace)) - (yOffSet), Tile.invCellSize, Tile.invCellSize), Tile.air);

    x++;
    if(x == Tile.invLength) {
    x = 0;
    y++;
    }
    }

    is supposed to create the grid. I took variables from the inventory, component, and workbench class to attempt to create properly SIZED and DESIGNED cubes, like the inventory of the game. Basically, I was trying to make the cubes the proper size, and then create a larger 3x3 cube using those previously established cubes.

    3) I'm awful at debugging with println()s, and I have no idea were to put them.

    I'm sorry, I'm quite bad at fixing errors. If I'm not helping you, then let me know.
    Last edited by Figgle; April 18th, 2014 at 10:07 AM. Reason: Forgot some code.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: 2D Survival Platformer Help

    Can you make a small, complete program that compiles, executes and shows the problem?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Making Slopes for a Platformer Game
    By Gravity Games in forum Java Theory & Questions
    Replies: 4
    Last Post: November 8th, 2012, 07:22 PM

Tags for this Thread