help with importing

Started by
1 comment, last by Moen 8 years ago

so i am currently working on a easy project, as i am not really THAT good with java and programming in general. i am just... making something atleast. well i decided to instead of controlling a rectangle, i want to controll a guy, or something, so i put something together quick in MS paint ( ?° ?? ?°) ( i know, i know). in this code i have a method called render, in wich i put what is going to be rendered, and then i have a method called tick, wich is generally the attributes of the thing i rendered. well on the code here, in the render, i put a code that i thought would be for for importing and showing the picture. Long story short, it dident.

here is the code for the player, wich has the player/guy i want to print, btw it has the same width and height as the variables in the code.

http://pastebin.com/xzf5UqaP

here is the main code, where i make the canvas and put everything together

http://pastebin.com/jQi7NUKM

here is the the main menu thing, it cointains the main string and that stuff, thats where it lanches from

http://pastebin.com/dx8tcE1N

here is the input handler:

http://pastebin.com/32Rx7hGb

i also want to make kind of gravity when i jump, in the jump if statement, can i like do it so like the y-=5; last for like, 2 sec before maby this code starts to work: y +=3;, so:

if (Jump && y - 5>= 0) {
y-= 5; (wait 2 secounds)
y+=3;
}
but that jumping would look very cheap right, go straight up, stop, and go down, maby the speed got slower and slower, i dont know, plz help me out here ^^ thx in advance ^^
Advertisement

In the Player.java class you are loading the player image inside the render method which is called every loop. Resource loading should not be done in such methods, They should be loaded only once.


    public void render(Graphics g) {

        player = new ImageIcon("C:/Users/Bruker/Pictures/myGuy.PNG").getImage();


        g.setColor(Color.blue);
        g.fillRect(x, y, Width, Height);
    }

Move the player image loading logic to the top of the class where you initialize your other things:


...

    boolean goingLeft = false;
    boolean goingDown = false;

    public Image player = new ImageIcon("C:/Users/Bruker/Pictures/myGuy.PNG").getImage();

    Rectangle boundingBox;

...

Moving is easy to do:

if (keyLeftDown) speedX = -5

if (keyRightDown) speedX = 5

if (rightOrLeftKeyReleased) speedX = 0

and each render cycle you do this:

posX = speedX * timeTakenToRenderInSeconds

Gravity:

declare constant gravity = -1.0

each render cycle:

speedY = speedY - gravity * timeTakenToRenderInSeconds

posY = speedY * timeTakenToRenderInSeconds

And you need to check if a limit has been reached for posY because you don't want the player to fall off the screen.

For example something like:

if (posY < -500) {

speedY = 0; // reset the speed because the player hit the floor

posY = -500;

}

And to jump you simply check that the player is not falling (speedY is ~0) and set the speedY something positive, for example speedY = speedY + 50

The gravity part isn't very correct but it will do the job I think.

Use floats for positions, speeds, gravity and so on.

In the Player.java class you are loading the player image inside the render method which is called every loop. Resource loading should not be done in such methods, They should be loaded only once.

public void render(Graphics g) {        player = new ImageIcon("C:/Users/Bruker/Pictures/myGuy.PNG").getImage();        g.setColor(Color.blue);        g.fillRect(x, y, Width, Height);    }
Move the player image loading logic to the top of the class where you initialize your other things:
...    boolean goingLeft = false;    boolean goingDown = false;    public Image player = new ImageIcon("C:/Users/Bruker/Pictures/myGuy.PNG").getImage();    Rectangle boundingBox;...
Moving is easy to do:

if (keyLeftDown) speedX = -5
if (keyRightDown) speedX = 5
if (rightOrLeftKeyReleased) speedX = 0
and each render cycle you do this:
posX = speedX * timeTakenToRenderInSeconds

Gravity:
declare constant gravity = -1.0
each render cycle:
speedY = speedY - gravity * timeTakenToRenderInSeconds
posY = speedY * timeTakenToRenderInSeconds

And you need to check if a limit has been reached for posY because you don't want the player to fall off the screen.
For example something like:
if (posY < -500) {
speedY = 0; // reset the speed because the player hit the floor
posY = -500;
}

And to jump you simply check that the player is not falling (speedY is ~0) and set the speedY something positive, for example speedY = speedY + 50

The gravity part isn't very correct but it will do the job I think.
Use floats for positions, speeds, gravity and so on.
well thx for reply m8, well if i move the image thing to the top, its just going to stand there doing noting, how can i "call" it in the render method? correct and exlpain if i am wrong. i am not by a computer now so i cant check ^^

if (keyLeftDown) speedX = -5
if (keyRightDown) speedX = 5
if (rightOrLeftKeyReleased) speedX = 0
and each render cycle you do this:
posX = speedX * timeTakenToRenderInSeconds

Gravity:
declare constant gravity = -1.0
each render cycle:
speedY = speedY - gravity * timeTakenToRenderInSeconds
posY = speedY * timeTakenToRenderInSeconds

i dont really understand this, i understand the algorythm but i dont understand where u want me to put it
when u wrote the "if keyleftdown", i have that already in the inputhandler

and each render cycle you do this:
posX = speedX * timeTakenToRenderInSecond

i dident really understand the first and third line

by float u mean a variable right?

i have already made collision detection ^^ thx for help and hoping for further reply ^^

This topic is closed to new replies.

Advertisement