Alright the 'correct' is now displaying fine whenever one image is in the correct box.
Code:
Sprite[] sprites;
Box[] boxes;
int bx=500;
int by=600;
int s_size=150;
boolean spritesDragging;
int boxX=630;
int boxY=100;
PImage b;
PImage CorrectImg;
boolean bShowCorrect;
void setup()
{
size(798, 760);
frameRate(15);
b = loadImage("EiffelTower.jpg");
CorrectImg = loadImage("Correct.jpg");
boxes= new Box[7];
sprites = new Sprite[7];
for(int i = 0; i < sprites.length; i++)
{
sprites[i] = new Sprite(loadImage(i + ".png"),s_size,random(bx), random(by), i);
}
for(int i =0; i< boxes.length; i++)
{
boxY+=80;
boxes[i] = new Box(boxX, boxY, 25, 130);
}
}
void draw()
{
background(b);
boolean bDragging = false;
//rectangle x, y, w , h
//rect(boxX, boxY, 140, 30);
for (int i=0; i <boxes.length; i++)
{
boxes[i].Draw();
}
for (int i = 0; i < sprites.length; i++)
{
// Check if the user tries to drag it
sprites[i].Update(spritesDragging);
if (sprites[i].IsDragged())
{
// We will remember a dragging is being done
bDragging = true;
// And move it to mouse position
sprites[i].Move();
}
// In all case, we redraw the sprite
sprites[i].Draw();
}
spritesDragging = bDragging;
}
class Sprite
{
float x;
float y;
PImage img;
float SpriteSize;
int i;
private boolean m_bIsHovered, m_bDragged;
private float m_clickDX, m_clickDY;
int boxID;
int boxClick=0;
Sprite(PImage img, int SpriteSize,float x,float y, int boxID)
{
this.x = x;
this.y = y;
this.img = img;
this.boxID = boxID;
this.SpriteSize=SpriteSize;
}
void Update(boolean bAlreadyDragging)
{
// Check if mouse is over the sprite
m_bIsHovered = mouseX > x && mouseX < x + SpriteSize && mouseY > y && mouseY < y + SpriteSize/10;
if (!bAlreadyDragging && mousePressed && mouseButton == LEFT && m_bIsHovered)
{
// We record the state
m_bDragged = true;
m_clickDX = mouseX - x;
m_clickDY = mouseY - y;
}
// If mouse isn't pressed
if (!mousePressed)
{
// Any possible dragging is stopped
m_bDragged = false;
}
//method for locking image in place
for(int i =0; i< boxes.length; i++)
{
//method for locking image in place
if(x> boxes[i].x && x< (boxes[i].x+boxes[i].w-22) &&y <(boxes[i].y+boxes[i].h-20) && y >boxes[i].y)
{
m_bDragged = false;
if(i == boxID)
{
boxClick++;
//println(boxClick);
if(boxClick == boxes.length)
{
//image(CorrectImg, 78, 590);
bShowCorrect=true;
}
}
}
}
}
boolean IsDragged()
{
return m_bDragged;
}
void Move()
{
if (m_bDragged)
{
x = mouseX - m_clickDX;
y = mouseY - m_clickDY;
}
}
void Draw()
{
image(img, x, y);
if(bShowCorrect)
{
image(CorrectImg, 78, 590);
}
}
}
class Box
{
float x;
float y;
float h;
float w;
Box(float x,float y, float h, float w)
{
this.x = x;
this.y = y;
this.h= h;
this.w= w;
}
boolean checkBox(float spriteX, float spriteY, Sprite theSprite)
{
return false;
}
void Draw()
{
rect(x,y, w, h);
}
}
The main aim was to have it displaying when all boxes are correct which still isn't occurring.
Code:
//method for locking image in place
for(int i =0; i< boxes.length; i++)
{
//method for locking image in place
if(x> boxes[i].x && x< (boxes[i].x+boxes[i].w-22) &&y <(boxes[i].y+boxes[i].h-20) && y >boxes[i].y)
{
m_bDragged = false;
if(i == boxID)
{
boxClick++;
//println(boxClick);
if(boxClick == boxes.length)
{
//image(CorrectImg, 78, 590);
bShowCorrect=true;
}
}
}
}
The line "if(boxClick == boxes.length)" is useful and does the same without this if statement.
Any more suggestions?