Java 2D Platformer Tutorial #7 - Adding Blocks

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

what is the shortcut key to import liberaries?

epromes
Автор

Whenever I add the collision it won't let me use "Or" statements.

rhyse
Автор

When I press the a or d, it stars to move then doesnt stop, HELP

package stockman.entities;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;

import

import stockman.gamestate.GameState;
import stockman.main.GamePanel;
import stockman.phyics.Collision;
import stocman.objects.Block;

public class Player extends Rectangle{

private static final long serialVersionUID = 1L;

//movement booleans
private boolean right = false, left = false, jumping = false, falling = false;

private double moveSpeed = 2.5;

//bounds
private double x, y;
private int width, height;

//jumpspeed
private double jumpSpeed = 5;
private double currentJumpSpeed = jumpSpeed;

//fallspeed
private double fallSpeed = 5;
private double currentFallSpeed = fallSpeed;



public Player(int width, int height){
x = GamePanel.WIDTH /2;
y = GamePanel.HEIGHT / 2;
this.width = width;
this.height = height;
}

public void tick(Block[] b){

int iX = (int)x;
int iY = (int)y;

for(int i = 0; i < b.length; i++){

//right 
Point(iX + width, iY), b[i]) ||
Point(iX + width, iY + height), b[i]))

{
= false;
}
}

if(right) {GameState.xOffset += moveSpeed;}

if(left) {GameState.yOffset += moveSpeed;}


if(jumping) {
y -= currentJumpSpeed;

currentJumpSpeed -= 1;

if(currentJumpSpeed <= 0) {

currentJumpSpeed = jumpSpeed;
jumping = false;
falling = true;

}
}

if(falling) {
y += currentFallSpeed;

if(currentFallSpeed < fallSpeed) {

currentFallSpeed += 0.1;

}
}

if(!falling) currentFallSpeed = 0.1;
}


public void draw(Graphics g){

g.setColor(Color.BLACK);
g.fillRect((int)x, (int)y, width, height);

}

public void keyPressed(int e){

if(e == KeyEvent.VK_D) {right = true;}

if(e == KeyEvent.VK_A) {left = true;}

if(e== KeyEvent.VK_SPACE){ jumping = true;}
}

public void keyReleased(int e){

if(e == KeyEvent.VK_D) {right = false;}
if(e == KeyEvent.VK_A) {left = false;}

}
}

iPodTouchhelper
Автор

im trying to make a mario clone in ruby using gosu. when i want to make the blocks hitable, i also loop thru the array just like in the video but it will always take the hitbox for the last block in the array.

uplife
Автор

at about 15 mins i start to get this error
Exception in thread "Thread-1" 3
at
at
at
at
at
at java.lang.Thread.run(Unknown Source)

pressing enter at the main screen will not bring me into the game, and i also cannot navigate the home menu.. i thaught i copies yours exactly but i guess not.

stringlights
Автор

Is it just me but it's super laggy :( how do I fix this? Also thanks so much!!!

quexatt
Автор

whenever i move one way, it won't stop moving, and i have the controls down correctly

firstpersonstupidity
Автор

why now left arrow is up
and right arrow is still right? what i did wrong? :\
package me.Bamba.game.entities;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyEvent;

import
import me.Bamba.game.objects.Block;
import
import me.alon.game.main.GamePanel;

public class Player {

// movement booleans
private boolean right = false, left = false, jumping = false, falling = false;

// bounds
private double x, y;
private int width, height;

// move speed
private double moveSpeed = 2.5;

// jump speed
private double jumpSpeed = 5;
private double currentJumpSpeed = jumpSpeed;

// fall speed
public double maxFallSpeed = 5;
public double currentFallSpeed = 0.1;

public Player (int width, int height) {
x = GamePanel.WIDTH / 2;
y = GamePanel.HEIGHT / 2;
this.width = width;
this.height = height;
}

public void tick(Block[] b) {

int iX = (int)x;
int iY = (int)y;

for (int i = 0; i < b.length; i++) {

// right
Point(iX + width, iY), b[i]) 
Collision.playerBlock(new Point(iX + width, iY + height), b[i])) {
   
}

}


if (right) {
GameState.xOffset += moveSpeed;
}

if (left) {
GameState.yOffset -= moveSpeed;
}

if (jumping) {
GameState.yOffset -= currentJumpSpeed;

currentJumpSpeed -= .1;

if(currentJumpSpeed <= 0) {
currentJumpSpeed = jumpSpeed;
jumping = false;
falling = true;
}
}

if (falling) {
GameState.yOffset += currentFallSpeed;

if(currentFallSpeed <= maxFallSpeed) {
currentFallSpeed += .1;
}
}

if(!falling) {
currentFallSpeed = 1;
}

}

public void draw(Graphics g) {
g.setColor(Color.BLACK);
g.fillRect((int)x, (int)y, width, height);
}

public void keyPressed(int k) {
if (k == KeyEvent.VK_RIGHT) right = true;
if (k == KeyEvent.VK_LEFT) left = true;
if (k == KeyEvent.VK_SPACE) jumping = true;
}

public void keyReleased(int k) {
if (k == KeyEvent.VK_RIGHT) right = false;
if (k == KeyEvent.VK_LEFT) left = false;
}

}

Levisss
Автор

at like 15 minutes in the video I get this error
Exception in thread "Thread-1" 3
at
at
at
at
at java.lang.Thread.run(Unknown Source)
when trying to run the game. any help or advice?

bigpool
Автор

at 17 minutes, when i press a or d I go always left :[

Monkexxx
Автор

In this tutorial you barely explain anything. Why would you say "Tutorial", and not teach us anything? copying the code doesn't do anything for us. Rename these to "Showing you how to make a platformer #".... Instead of lying to us.

AndrewisGaming