Hi there, im quite new to processing and programming in general, im trying to get a different image when it hits a wall but also that it changes its image to the direction it is going after it has hit a wall. I have gotten the images to collide with walls and that it loops the images using a counter but I cant for the life of me get it to only change an image when it hits a wall. Ive looked on processing for guidance but i cant find anything. Im not looking for the answer just some pointers of what i should do. Ive jus thit a brick wall and have no idea where to go from here
Many thanks
class walker
{
//declaring variables that will be used
PImage img1, img2, img3, img4;
int x, y;
int speed;
int count = 0;
int xSpeed;
int ySpeed;
int x1 = 0;
int y1 = 0;
int x2 = 150;
int y2 = 75;
//construc the walker
walker(int x, int y, int dx, int dy)
{
this.x = x;
this.y = y;
this.xSpeed = dx;
this.ySpeed = dy;
//pre loading images in walker constructor
img1 = loadImage("upright.jpg");
img2 = loadImage("downright.jpg");
img3 = loadImage("downleft.jpg");
img4 = loadImage("upleft.jpg");
}
//image chaging method for the human
void render()
{
if (count < 10)
{
image(img1, x, y);
img1.resize(150, 75);
} else if (count < 20)
{
image(img2, x, y);
img2.resize(150, 75);
} else if (count < 30)
{
image(img3, x, y);
img3.resize(150, 75);
} else if (count < 40)
{
image(img4, x, y);
img4.resize(150, 75);
} else count = -1;
count++;
}
//allowing images to move around x and y axis and to bounce off walls
void move()
{
x += xSpeed;
if (x > width - x2 || x < 0)
{
xSpeed *= -1;
}
y += ySpeed;
if (y > height - y2 || y < 0)
{
ySpeed *= -1;
}
}
void setup()
{
size(400, 400);
human1 = new walker(width/2, height/2, 1, 1);
human2 = new walker(width/2, height/2, 3, 2);
}
/*Setting background to white so does not leave trail of people
objects getting their functions from walker class */
void draw()
{
background(255);
human1.update();
human2.update();
}
so would i need to possibly make a new method or function (im not sure which is which) that would basically check what the index number is while it collides with a wall and if it is, to add one to index to get the next number ?
With doing this, would I be able to get it to select what image it is when it hits a specific wall so say if the image is an arrow and it hits the left wall, then the arrow points right to the right wall and if it hits the top, the arrow would point down?
Would I be able to like specifically make an image appear only when it hits a specific wall with what I have already as in with the if and else if statements I have, like just say “if this image is Equal to width, change to this picture” and so on with other images? Or is it not really possible
just one more thing (sorry for all the questions, is there a way for it to alternates when it hits each side as in, it hits left side image1 then right side image2 and then back to image 1 and so on ? and the same for up and down iof possible ?
In my way (when you split the if clause and get rid of || ) you have different images for left and right side automatically.
OR :
Do you mean you want another image the first time you hit the left side and then the second time you hit the same side (left)?
That would also be possible.
Eg when you say want to alternate between index 0 or index 4.
Then you additionally check if lastIndexLeft was 0 or 4 and choose the other. Then set lastIndexLeft to the same value.