Thanks for the reply. I’ve attempted to add something similar to what you wrote into the code (I’ve pasted the part I’m talking about below) but I can’t seem to figure out the variables due to how I’ve written my code. I probably should have posted all of the code to give anyone a better understanding of how it works, Anyways…
Do the playerY & playerX variables represent the current area in the map[][] array where the player is?
Do the halfHeight & haldWidth variables represent the amount of tiles from where the player is standing on to the edge of the screen?
What do tileX and tile Y represent?
[quote]int startX = (playerX - halfWidth) / tileX - 1;
int startY = (playerY - halfHeight) / tileY - 1;
int endX = (playerX + halfWidth) / tileX + 1;
int endY = (playerY + halfHeight) / tileY + 1;
for(int i = startX; i < endX; i++) {
for(int j = startY; j < endY; j++) {
// Render code!
}
}
[/quote]
I’ll post the movement code along with the rest of the rendering code so that you can see how I’m doing everything.
This is the render/logic code.
package Screens;
import Core.GameCanvas;
import Core.ResourceHandler;
import Entity.CollisionDetection;
import Entity.Player;
import java.awt.Graphics2D;
public class ScreenGame extends Screen
{
private int[][] currentMap;
private GameCanvas canvas;
private ResourceHandler resource;
private Player player;
private CollisionDetection collision;
private int playerXLocation, playerYLocation, playerDirection;
private boolean playerIsWalking;
private int mapWidth, mapHeight;
private int spriteCounter = 0, spriteCounterTwo = 0;
public ScreenGame(GameCanvas canvas)
{
this.canvas = canvas;
resource = new ResourceHandler();
player = new Player();
resource.loadImages();
collision = new CollisionDetection();
}
public void logicUpdate()
{
setCurrentMap();
collision.logicUpdate();
player.logicUpdate();
playerXLocation = player.getXLocation();
playerYLocation = player.getYLocation();
playerIsWalking = player.getIsWalking();
playerDirection = player.getDirection();
mapWidth = currentMap.length;
mapHeight = currentMap[0].length;
spriteCounterTwo++;
//This while statement checks 4 different points around the player's sprite, if any of them return false then the player's position is changed until they all return true.
while(collision.checkPointCollision(playerXLocation - 12, playerYLocation + 22) == false /*LowerLeftPoint*/ || collision.checkPointCollision(playerXLocation + 12, playerYLocation + 22) == false /*Lower Right Point*/ || collision.checkPointCollision(playerXLocation - 12, playerYLocation) == false /*Middle Left Point*/ || collision.checkPointCollision(playerXLocation + 12, playerYLocation) == false /*Middle Right Point*/)
{
switch(playerDirection)
{
case 0:
{
player.setXLocation(1);
playerXLocation++;
break;
}
case 1:
{
player.setYLocation(1);
playerYLocation++;
break;
}
case 2:
{
player.setXLocation(-1);
playerXLocation--;
break;
}
case 3:
{
player.setYLocation(-1);
playerYLocation--;
break;
}
}
}
}
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
@Override
public void render(Graphics2D g)
{
//Updates Logic in other Methods
logicUpdate();
//Render Map
for(int row = 0; row < mapHeight; row++)
{
for(int column = 0; column < mapWidth; column++)
{
switch(currentMap[column][row]) //Note: To increase the player's walking speed just times both the playerXPosition and playerYPosition by a speed multiplyer
{
case -1237980: //Grass
{
g.drawImage(resource.getSprite(2, 0, 0), column*40 - playerXLocation, row * 40 - playerYLocation, null);
break;
}
case -3584: //Grass Flower 1
{
g.drawImage(resource.getSprite(2, 0, 1), column*40 - playerXLocation, row * 40 - playerYLocation, null);
break;
}
case -16735512: //Grass Flower 2
{
g.drawImage(resource.getSprite(2, 0, 2), column*40 - playerXLocation, row * 40 - playerYLocation, null);
break;
}
case -14503604: //Stone Path
{
g.drawImage(resource.getSprite(2, 0, 3), column*40 - playerXLocation, row * 40 - playerYLocation, null);
break;
}
case -14503860: //Stone Path
{
g.drawImage(resource.getSprite(2, 0, 4), column*40 - playerXLocation, row * 40 - playerYLocation, null);
break;
}
case -14504116: //Stone Path
{
g.drawImage(resource.getSprite(2, 0, 5), column*40 - playerXLocation, row * 40 - playerYLocation, null);
break;
}
case -14504372: //Stone Path
{
g.drawImage(resource.getSprite(2, 0, 6), column*40 - playerXLocation, row * 40 - playerYLocation, null);
break;
}
case -14504628: //Stone Path
{
g.drawImage(resource.getSprite(2, 0, 7), column*40 - playerXLocation, row * 40 - playerYLocation, null);
break;
}
case -14504884: //Stone Path
{
g.drawImage(resource.getSprite(2, 0, 8), column*40 - playerXLocation, row * 40 - playerYLocation, null);
break;
}
case -14505140: //Stone Path
{
g.drawImage(resource.getSprite(2, 0, 9), column*40 - playerXLocation, row * 40 - playerYLocation, null);
break;
}
case -14505396: //Stone Path
{
g.drawImage(resource.getSprite(2, 0, 10), column*40 - playerXLocation, row * 40 - playerYLocation, null);
break;
}
case -14505652: //Stone Path
{
g.drawImage(resource.getSprite(2, 0, 11), column*40 - playerXLocation, row * 40 - playerYLocation, null);
break;
}
}
}
}
//Render Player
//Whenever you get diagonal movement animations on the player sprite create more direction numbers for the diagonal movement and then add the stuff to render the diagonal animations below
if (!playerIsWalking)
{
spriteCounter = 0;
spriteCounterTwo = 0;
}
else if(playerIsWalking && spriteCounter == 0)
{
spriteCounter = 1;
}
if (spriteCounter == 0 && !playerIsWalking)
{
g.drawImage(resource.getSprite(0, playerDirection, spriteCounter), 628, 499, null);
}
else if (spriteCounter == 1 && playerIsWalking && spriteCounterTwo >= 15)
{
g.drawImage(resource.getSprite(0, playerDirection, spriteCounter), 628, 499, null);
spriteCounter++;
spriteCounterTwo = 0;
}
else if (spriteCounter == 1 && playerIsWalking && spriteCounterTwo < 15)
{
g.drawImage(resource.getSprite(0, playerDirection, spriteCounter), 628, 499, null);
}
else if (spriteCounter == 2 && playerIsWalking && spriteCounterTwo >= 15)
{
g.drawImage(resource.getSprite(0, playerDirection, spriteCounter), 628, 499, null);
spriteCounter--;
spriteCounterTwo = 0;
}
else if (spriteCounter == 2 && playerIsWalking && spriteCounterTwo < 15)
{
g.drawImage(resource.getSprite(0, playerDirection, spriteCounter), 628, 499, null);
}
}
//Set methods
public void setCurrentMap()
{
currentMap = resource.getMap();
}
}
Movement
package Entity;
import Core.KeyboardListener;
import java.awt.event.KeyEvent;
public class Player extends Entity
{
private int xLocation = 0, yLocation = 0, direction = 0;
private boolean isWalking = false;
public Player()
{
}
public void logicUpdate()
{
if(KeyboardListener.isKeyPressed(KeyEvent.VK_W) || KeyboardListener.isKeyPressed(KeyEvent.VK_UP))
{
//Up
yLocation--;
isWalking = true;
direction = 1;
}
else if(KeyboardListener.isKeyPressed(KeyEvent.VK_S) || KeyboardListener.isKeyPressed(KeyEvent.VK_DOWN))
{
//Down
yLocation++;
isWalking = true;
direction = 3;
}
else if(KeyboardListener.isKeyPressed(KeyEvent.VK_A) || KeyboardListener.isKeyPressed(KeyEvent.VK_LEFT))
{
//Left
xLocation--;
isWalking = true;
direction = 0;
}
else if(KeyboardListener.isKeyPressed(KeyEvent.VK_D) || KeyboardListener.isKeyPressed(KeyEvent.VK_RIGHT))
{
//Right
xLocation++;
isWalking = true;
direction = 2;
}
else
{
isWalking = false;
}
}
//Set Methods
public void setXLocation(int x)
{
xLocation += x;
}
public void setYLocation(int x)
{
yLocation += x;
}
//Get Methods
public int getXLocation()
{
return xLocation;
}
public int getYLocation()
{
return yLocation;
}
public boolean getIsWalking()
{
return isWalking;
}
public int getDirection()
{
return direction;
}
}
To get where the player’s position on the map[][] array is, I use this:
int tempInt = 15;
int tempIntTwo = 12;
if(playerXLocation> 0)
{
tempInt = 16;
}
if(playerYLocation> 0)
{
tempIntTwo = 13;
}
int xPositionOnMap = ((playerXLocation/ 40)) + tempInt;
int yPositionOnMap = ((playerYLocation/ 40)) + tempIntTwo;
As for the improvements to the switch statement, that’ll help a lot! I’m first going to look into hashmaps first before I attempt to use them.