Scrolling tilemap
in
Programming Questions
•
1 year ago
Hi
I'm making an overhead shooter, and although I can control the character (he moves to wherever the mouse clicks) and draw the tile map I've loaded in. I can't figure out how to move the screen with the character.
I've used the variables offsetX and offsetY to show the amount the screen has 'moved', but I can't figure out how to make this work for the tiles and following the shooter.
//Set where the player is going to move to
- Player1.targetX = mouseX+offsetX;
Player1.targetY = mouseY+offsetY;
THE PLAYER CLASS (NOT THE MAIN GAME draw)
//Drawing and moving the player, I don't know how I should use the offsetX and offsetY variables here
- class player {
float x, y, speed, angle, targetX, targetY, dx, dy, oldAngle;
int health, gun;
player(float px, float py, float pspeed, float pangle) {
x = px;
y = py;
speed = pspeed;
angle = pangle;
health = 100;
} -
void draw() {
- pushMatrix();
imageMode(CENTER);
translate(x,y);
rotate(atan2(targetY-y, targetX-x));
image(imgPlayer,0,0); - popMatrix();
}
void move() {
if ((targetX > 0) && (targetY > 0)) {
angle = atan2(targetY-y,targetX-x);
if (dist(x,y,targetX,targetY) < 10) {
println("reached target"); -
} else {
- y+= (sin(atan2(targetY - (y+offsetY), targetX - (x+offsetX)))*1);
-
x+= (cos(atan2(targetY - (y+offsetY), targetX - (x+offsetX)))*1);
-
}
- }
-
and draw the tiles in view
- void updateTiles() {
for (int i = 0; i < tilesW; i++) {
for (int j = 0; j < tilesH; j++) {
if(tiles[i][j]>0) {
if ((i*tileSize)< (sWidth-offsetX+tileSize) && (i*tileSize) > (0-tileSize-offsetX)) {
if ((j*tileSize)< (sHeight-offsetY+tileSize) && (j*tileSize) > (0-tileSize-offsetY)) {
image(imgTile[tiles[i][j]],i*tileSize+offsetX,j*tileSize+offsetY);
}
}
}
}
}
}
a screenshot:
Thanks for any help.
1