Pretty much that, the screen stays on the player, the player stays the same size :/
I am new to Java so don't kill me :p
I attached my files, please help!
-Wolf
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.
Pretty much that, the screen stays on the player, the player stays the same size :/
I am new to Java so don't kill me :p
I attached my files, please help!
-Wolf
In the future, post code like using the [code=java][/code] tags. Next, the program won't run for me on Eclipse. It can't find the main method. Please, explain you program more fully.
There is one, in Component.class. Here's some code then :
Component.class
package net.wolfgamingdev.minecraftplatformer; import java.applet.*; import java.awt.*; import javax.swing.*; public class Component extends Applet implements Runnable { private static final long serialVersionUID = 1L; private static int pixelSize = 2; public static double sX = 0, sY = 0; public static Dimension size = new Dimension(700,560); public static Dimension pixel = new Dimension(size.width / pixelSize, size.height / pixelSize); public static String name = "Minecraft 2D!"; public static boolean isRunning = false; private Image screen; public static Level level; public static Character character; //BLERRRGH public Component() { setPreferredSize(size); } public void start() { //Defining Objects new Tile(); //Loading images level = new Level(); character = new Character(Tile.tileSize, Tile.tileSize * 2); //Starting Game Loop isRunning = true; new Thread(this).start(); } public void stop() { isRunning = false; } public static void main(String args[]) { Component component = new Component(); JFrame frame = new JFrame(); frame.add(component); frame.pack(); frame.setTitle(name); frame.setResizable(false); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); component.start(); } public void tick() { level.tick(); character.tick(); } public void render() { Graphics g = screen.getGraphics(); //Drawing Things g.setColor(new Color(100, 100, 255)); g.fillRect(0, 0, pixel.width, pixel.height); level.render(g); character.render(g); 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); while(isRunning) { tick(); render(); try { Thread.sleep(5); } catch(Exception e) { } } } }
Level.class
package net.wolfgamingdev.minecraftplatformer; import java.awt.*; public class Level { public Block[][] block = new Block[50][50]; public Level() { for (int x=0; x<block.length;x++) { for (int y=0;y<block[0].length;y++) { block[x][y] = new Block(new Rectangle(x * Tile.tileSize, y * Tile.tileSize, Tile.tileSize, Tile.tileSize), Tile.air); } } generateLevel(); } public void generateLevel() { for (int x=0; x<block.length;x++) { for (int y=0;y<block[0].length;y++) { if(x == 0 || y == 0 || x == block.length-1 || y == block[0].length-1) { block[x][y].id = Tile.earth; } } } } public void tick() { } public void render(Graphics g) { for (int x=0; x<block.length;x++) { for (int y=0;y<block[0].length;y++) { block[x][y].render(g); } } } }
Block.class (This is where the blocks values are defined)
package net.wolfgamingdev.minecraftplatformer; import java.awt.*; public class Block extends Rectangle { private static final long serialVersionUID = 1L; public int[] id = {-1, -1}; public Block(Rectangle size, int[] id) { setBounds(size); this.id = id; } public void render(Graphics g) { if(id != Tile.air) { g.drawImage(Tile.tileset_terrain, (int)x - (int)Component.sX, (int)y - (int)Component.sY, (int)x + width - (int)Component.sX, (int)y + height - (int)Component.sY, id[0] * Tile.tileSize - (int)Component.sX, id[1] * Tile.tileSize - (int)Component.sY, id[0] * Tile.tileSize + Tile.tileSize, id[1] * Tile.tileSize + Tile.tileSize, null); } } }
Character.class (There is a tile.class as well but it just defines the tiles)
package net.wolfgamingdev.minecraftplatformer; import java.awt.*; public class Character extends DoubleRectangle { public double fallingSpeed = 1.3; public Character(int width, int height) { setBounds((Component.pixel.width/2) - (width / 2), (Component.pixel.height/2) - (height / 2), width, height); } public void tick() { y += fallingSpeed; Component.sY += fallingSpeed; } public void render(Graphics g) { g.drawImage(Tile.tileset_terrain, (int)x - (int)Component.sX, (int)y - (int)Component.sY, (int)(x + width) - (int)Component.sX, (int)(y + height) - (int)Component.sY, Tile.character[0] * Tile.tileSize, Tile.character[1] * Tile.tileSize, Tile.character[0] * Tile.tileSize + (int)width, Tile.character[1] * Tile.tileSize + (int)height, null); } }
There is another class called doublerectangle but I think that is just defining the characters values.
Sorry for all the trouble I'm just a newb following a tutorial :/
Honestly I don't understand half of what's in there yet, but I have a theory that the bug has something to do with Component.sY getting bigger....
--- Update ---
It's definitely because of Component.Sy getting added in character.class, commented it out and it fixed it, (without the camera moving)
Several problems:
Why does the Applet class have a main() method? Is this an Applet or an application?
Component and Character are Java SE classes. You should NOT use the same name for your classes.
Too many variables are static.
If you don't understand my answer, don't ignore it, ask a question.
It's both? That's what this tutorial said. None of my classes have the same name...
Which variables shouldn't be static?
My hasn't even been touched on yet :/
There shouldn't be any static variables.
Rename the classes so their names are NOT the same as Java SE classes.
Change the extends Applet to extends Panel
If you don't understand my answer, don't ignore it, ask a question.
Doesn't static mean that they can be changed? (the point of variables? )
Which class is the same as a Java SE class?
Thanks for trying to help, I don't mean to be a bother :p
static means you don't have to create an object of that type to use it. for example in the Math class, not only do you not need to create a Math object, you can't.
Making a specific variable a 'static variable' allows static methods to use it and for that variable to be accessed in a static way.
When I take off the static variables the program breaks.
--- Update ---
No errors but Nothing shows on screen
How did you rewrite the program so it uses instances of a class instead of using static variables?the program breaks.
Post the full text of the compiler error messages and the new source code.
See post#4Which class is the same as a Java SE class?
If you don't understand my answer, don't ignore it, ask a question.
Norm - I just...deleted the part that says static (god I'm such a newb) And I can change Component to Engine and Character to Player
Post the new code and your questions if you are still having a problem.
If you don't understand my answer, don't ignore it, ask a question.
Norm - I just...deleted the part that says static (god I'm such a newb) And I can change Component to Engine and Character to Player
Just changed the classes didn't help
--- Update ---
Why do the Tile height decrease as my character falls
--- Update ---
The code didn't change with the exception of the class names. Nothing has changed
You need to get rid of the static variables and use instances of the class.
Some classes are missing. The posted code can not be compiled for testing.
If you don't understand my answer, don't ignore it, ask a question.
I'll re-post all the code's, and can you tell e how I could do that? (instances of the class)
Engine
package net.wolfgamingdev.minecraftplatformer; import java.applet.*; import java.awt.*; import javax.swing.*; public class Engine extends Applet implements Runnable { private static final long serialVersionUID = 1L; private static int pixelSize = 2; public static double sX = 0, sY = 0; public static Dimension size = new Dimension(700,560); public static Dimension pixel = new Dimension(size.width / pixelSize, size.height / pixelSize); public static String name = "Minecraft 2D!"; public static boolean isRunning = false; private Image screen; public static Level level; public static Player player; //BLERRRGH public Engine() { setPreferredSize(size); } public void start() { //Defining Objects new Tile(); //Loading images level = new Level(); player = new Player(Tile.tileSize, Tile.tileSize * 2); //Starting Game Loop isRunning = true; new Thread(this).start(); } public void stop() { isRunning = false; } public static void main(String args[]) { Engine engine = new Engine(); JFrame frame = new JFrame(); frame.add(engine); frame.pack(); frame.setTitle(name); frame.setResizable(false); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); engine.start(); } public void tick() { level.tick(); player.tick(); } public void render() { Graphics g = screen.getGraphics(); //Drawing Things g.setColor(new Color(100, 100, 255)); g.fillRect(0, 0, pixel.width, pixel.height); level.render(g); player.render(g); 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); while(isRunning) { tick(); render(); try { Thread.sleep(5); } catch(Exception e) { } } } }
Level
package net.wolfgamingdev.minecraftplatformer; import java.awt.*; public class Level { public Block[][] block = new Block[50][50]; public Level() { for (int x=0; x<block.length;x++) { for (int y=0;y<block[0].length;y++) { block[x][y] = new Block(new Rectangle(x * Tile.tileSize, y * Tile.tileSize, Tile.tileSize, Tile.tileSize), Tile.air); } } generateLevel(); } public void generateLevel() { for (int x=0; x<block.length;x++) { for (int y=0;y<block[0].length;y++) { if(x == 0 || y == 0 || x == block.length-1 || y == block[0].length-1) { block[x][y].id = Tile.earth; } } } } public void tick() { } public void render(Graphics g) { for (int x=0; x<block.length;x++) { for (int y=0;y<block[0].length;y++) { block[x][y].render(g); } } } }
Block
package net.wolfgamingdev.minecraftplatformer; import java.awt.*; public class Block extends Rectangle { private static final long serialVersionUID = 1L; public int[] id = {-1, -1}; public Block(Rectangle size, int[] id) { setBounds(size); this.id = id; } public void render(Graphics g) { if(id != Tile.air) { g.drawImage(Tile.tileset_terrain, (int)x - (int)Engine.sX, (int)y - (int)Engine.sY, (int)x + width - (int)Engine.sX, (int)y + height - (int)Engine.sY, id[0] * Tile.tileSize - (int)Engine.sX, id[1] * Tile.tileSize - (int)Engine.sY, id[0] * Tile.tileSize + Tile.tileSize, id[1] * Tile.tileSize + Tile.tileSize, null); } } }
Tile
package net.wolfgamingdev.minecraftplatformer; import java.awt.image.*; import javax.imageio.ImageIO; import java.io.*; public class Tile { public static int tileSize = 20; public static int[] air = {-1, -1}; public static int[] earth = {0, 0}; public static int[] character = {0, 18}; public static BufferedImage tileset_terrain; public Tile() { try { Tile.tileset_terrain = ImageIO.read(new File("res/tileset_terrain.png")); } catch(Exception e) { } } }
Player
package net.wolfgamingdev.minecraftplatformer; import java.awt.*; public class Player extends DoubleRectangle { public double fallingSpeed = 1.3; public Player(int width, int height) { setBounds((Engine.pixel.width/2) - (width / 2), (Engine.pixel.height/2) - (height / 2), width, height); } public void tick() { y += fallingSpeed; Engine.sY += fallingSpeed; } public void render(Graphics g) { g.drawImage(Tile.tileset_terrain, (int)x - (int)Engine.sX, (int)y - (int)Engine.sY, (int)(x + width) - (int)Engine.sX, (int)(y + height) - (int)Engine.sY, Tile.character[0] * Tile.tileSize, Tile.character[1] * Tile.tileSize, Tile.character[0] * Tile.tileSize + (int)width, Tile.character[1] * Tile.tileSize + (int)height, null); } }
DoubleRectangle
package net.wolfgamingdev.minecraftplatformer; public class DoubleRectangle { public double x, y, width, height; public DoubleRectangle() { setBounds(0, 0, 0, 0); } public DoubleRectangle(double x, double y, double width, double height) { setBounds(x, y, width, height); } public void setBounds(double x, double y, double width, double height) { this.x = x; this.y = y; this.width = width; this.height = height; } }
I see lots of static variables. These need to changed.
If you don't understand my answer, don't ignore it, ask a question.
Yes but I don't know how, just removing the static part, it doesn't show anything when I try to run it.
How did you change the code when you removed the static modifier?
Just deleting "static" would cause compiler errors so the program would not execute.
When I execute the code I do not see what you mean by a "tile".
There are some very small shapes on the left that move up. They get smaller as they continue to move up.
If you don't understand my answer, don't ignore it, ask a question.
That is exactly my problem. And that's all I did was remove static .
The code is ugly in the way it uses static. I'll leave debugging to you.
To debug the code, start by finding where the "tile" is drawn. Add some println statements there that print out the variables used to determine the size that the tile is drawn.
If you don't understand my answer, don't ignore it, ask a question.
Alright, how can I make it not use static?
That will require that there be instances of the classes and that those are used instead of the name of the class when referencing variables in the class.
Have you tried adding some println statements to find how or where the size of the "tile" is being changed?
If you don't understand my answer, don't ignore it, ask a question.
Yes and I'm having problems there too -__-
Here's where I put the println (Because when I tried putting it in the original tick function, it wouldn't allow me to access the block variables because they weren't static... )
Block.class
package net.wolfgamingdev.minecraftplatformer; import java.awt.*; public class Block extends Rectangle { private static final long serialVersionUID = 1L; public int[] id = {-1, -1}; public Block(Rectangle size, int[] id) { setBounds(size); this.id = id; } public void tick() { System.out.println(y + width - Engine.sY); } public void render(Graphics g) { if(id != Tile.air) { g.drawImage(Tile.tileset_terrain, (int)x - (int)Engine.sX, (int)y - (int)Engine.sY, (int)x + width - (int)Engine.sX, (int)y + height - (int)Engine.sY, id[0] * Tile.tileSize - (int)Engine.sX, id[1] * Tile.tileSize - (int)Engine.sY, id[0] * Tile.tileSize + Tile.tileSize, id[1] * Tile.tileSize + Tile.tileSize, null); } } }
And Engine.class
package net.wolfgamingdev.minecraftplatformer; import java.applet.*; import java.awt.*; import javax.swing.*; public class Engine extends Applet implements Runnable { private static final long serialVersionUID = 1L; private static int pixelSize = 2; public static double sX = 0, sY = 0; public static Dimension size = new Dimension(700,560); public static Dimension pixel = new Dimension(size.width / pixelSize, size.height / pixelSize); public static String name = "Minecraft 2D!"; public static boolean isRunning = false; private Image screen; public static Level level; public static Player player; public static Block block; //BLERRRGH public Engine() { setPreferredSize(size); } public void start() { //Defining Objects new Tile(); //Loading images level = new Level(); player = new Player(Tile.tileSize, Tile.tileSize * 2); //Starting Game Loop isRunning = true; new Thread(this).start(); } public void stop() { isRunning = false; } public static void main(String args[]) { Engine engine = new Engine(); JFrame frame = new JFrame(); frame.add(engine); frame.pack(); frame.setTitle(name); frame.setResizable(false); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); engine.start(); } public void tick() { level.tick(); player.tick(); block.tick(); } public void render() { Graphics g = screen.getGraphics(); //Drawing Things g.setColor(new Color(100, 100, 255)); g.fillRect(0, 0, pixel.width, pixel.height); level.render(g); player.render(g); 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); while(isRunning) { tick(); render(); try { Thread.sleep(5); } catch(Exception e) { } } } }
My error :p
Exception in thread "Thread-2" java.lang.NullPointerException
at net.wolfgamingdev.minecraftplatformer.Engine.tick( Engine.java:66)
at net.wolfgamingdev.minecraftplatformer.Engine.run(E ngine.java:89)
at java.lang.Thread.run(Thread.java:722)
I really do hate being such a bother :/
There is a variable at line 66 that has a null value when that line is executed. Find that variable and then backtrack to find out why is doesn't have a valid value.xception in thread "Thread-2" java.lang.NullPointerException
at net.wolfgamingdev.minecraftplatformer.Engine.tick( Engine.java:66)
If you don't understand my answer, don't ignore it, ask a question.
public void tick() {
System.out.println(y + height - Engine.sY);
}
this is the function line 66 is referring too..