Hi everyone,
I’ve been creating a map with a 2d array, and now i’m trying to get it to move (since the map is bigger than the screen), but I’m having some trouble with getting the field to stay where it is. The idea behind the code is to compare the mouseX/Y to the point where you started dragging, and forcing that point towards the mouseX/Y to make them align, moving the map in the process.
the code is:
int Xmovement;
int Ymovement;
int pointX;
int pointY;
boolean firstpos;
void draw() {
background(0);
for (int x = 0; x < map.length; x++) {
for (int y = 0; y < map[x].length; y++) {
float posX = (x * scaleX) + Xmovement;
float posY = (y * scaleY) + Ymovement;
if (map[x][y] == 0) {
image(ED, posX, posY, scaleX, scaleY);
void mouseDragged() {
if (mouseButton == LEFT) {
if (firstpos == false) {
pointX = mouseX;
pointY = mouseY;
firstpos = true;
}
if (firstpos == true) {
if ((pointX != mouseX) || (pointY != mouseY) || ((pointX != mouseX) && (pointY != mouseY))) {
Xmovement = mouseX - pointX;
Ymovement = mouseY - pointY;
firstpos = false;
}
}
}
}
The problem is (at least from what i understand) the image resetting, since if you don’t move the mouse for one second while keeping it pressed, the pointX and pointY will reset, making the X and Ymovement 0 and thus the posX and Y return to their original state. Does anybody have tips/ideas to make the system better (my thought was either a different approach for the posX/Y or change the mouseDragged loop entirely)? Thanks in advance!
(ps. the complete array is 76x78 and the image is the same except for the comparison, so I thought I’d leave that out )