So what im trying to do is when the image bounces of the wall it will show which direction its going to. (my images are arrows btw)
eg if it bounces of the right wall the arrow will point left and vice versa
and if it bounces from the top the arrow will point down and vice versa
what I’m trying to do is implement an index for this. eg
index =0; for left side, and
index =1; for right side
etc.
if(index==0)image(right arrow....
else if (index ==1)image (left arrow....
else if (....
i know I need to implement it into my collision with the wall but I don’t know when or how to do that. if I could get some guidance that would be appreciated…
//allowing images to move around x and y axis and to bounce off walls
void move()
{
x += xSpeed;
if (x > width - x2 )
{
xSpeed *= -1;
}
if (x < 0)
{
xSpeed *= -1;
}
y += ySpeed;
if (y > height - y2)
{
ySpeed *= -1;
}
if (y < 0)
{
ySpeed *= -1;
}
unless i make a separate variable eg Index1 and make that equal to image1 and then call it once it collides with right wall. Or is there an easier way for this ?
class arrow
{
//declaring variables that will be used
PImage img1, img2, img3, img4;
int x, y;
int speed;
int count = 0;
int xSpeed;
int ySpeed;
int index; /// this will chnage to change the direction of the arrow i guess...
int x1 = 0;
int y1 = 0;
int x2 = 150;
int y2 = 75;
//construc the arrow
arrow(int x, int y, int dx, int dy)
{
this.x = x;
this.y = y;
this.xSpeed = dx;
this.ySpeed = dy;
//pre loading images in arrow constructor
img1 = loadImage("Up.png");
img2 = loadImage("Down.png");
img3 = loadImage("Left.png");
img4 = loadImage("Right.png");
}
//image changing method
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 )
{
xSpeed *= -1;
}
if (x < 0)
{
xSpeed *= -1;
}
y += ySpeed;
if (y > height - y2)
{
ySpeed *= -1;
}
if (y < 0)
{
ySpeed *= -1;
}
}
void update() {
render();
move();
}
} //end of class
setup code:
arrow white;
arrow green;
void setup()
{
size(400, 400);
white = new arrow(width/2, height/2, 1, 1);
green = new arrow(width/2, height/2, 3, 2);
}
/*Setting background to white so does not leave trail of people
objects getting their functions from arrow class */
void draw()
{
background(255);
white.update();
green.update();
}
truly for a beginner at this, i could have a try but i just fixed it after i figured out when it hits with top it doesnt face any of the directions i want, lol