We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Displaying 'Correct' image in drag and drop array
Pages: 1 2 
Displaying 'Correct' image in drag and drop array (Read 3239 times)
Displaying 'Correct' image in drag and drop array
Nov 6th, 2009, 8:49am
 
Hello again. We made more features to out drag and drop word game. At the moment we're trying to create a method which displays a 'correct' message if the word images are placed into the right 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;

void setup()
{
 size(798, 760);
 
 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));
   
 }
 
 for(int i =0; i< boxes.length; i++)
 {
   boxY+=80;
   boxes[i] = new Box(boxX, boxY, 130, 25);
 }
}


void draw()
{
   
   b = loadImage("EiffelTower.jpg");
   CorrectImg = loadImage("Correct.jpg");
   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);
// Ah, this one is indeed dragged!
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();
               
               for(int j =0; j < boxes.length; j++)
               {
                if(sprites[i].x>boxes[j].x)
                 {
                        image(CorrectImg, 78, 490);
                  }
               }
   }
    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;
 

 Sprite(PImage img, int SpriteSize,float x,float y)
 {
   this.x = x;
   this.y = y;
   this.img = img;
   
   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;
                 
         }
             }
                 
  }
 
 

boolean IsDragged()
{
return m_bDragged;
}


   void Move()
   {
if (m_bDragged)
{
x = mouseX - m_clickDX;
y = mouseY - m_clickDY;
}
   }
   
   void Draw()
   {
     image(img, x, y);
     
   }
   
 
 
}


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;
 }
 
  void Draw()
   {
     rect(x,y, h, w);
     
   }
}


We added a new method which displays the 'correct' image, but at the moment it displays it when any word image is placed into any place box.

Code:

for (int i = 0; i < sprites.length; i++)
   {
               
// Check if the user tries to drag it
sprites[i].Update(spritesDragging);
// Ah, this one is indeed dragged!
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();
               
               for(int j =0; j < boxes.length; j++)
               {
                if(sprites[i].x>boxes[j].x)
                 {
                        image(CorrectImg, 78, 490);
                  }
               }
   }


What we want is something like word image number one is equal to box number one, return correct. Which works fine when hard coded like this:
Code:

for(int j =0; j < boxes.length; j++)
               {
                if(sprites[1].x>boxes[1].x)
                 {
                        image(CorrectImg, 78, 490);
                  }
               }


Any suggestions? Thanks again in advance.
Re: Displaying 'Correct' image in drag and drop array
Reply #1 - Nov 6th, 2009, 9:05am
 
I'm just after rewriting that last method a bit better.

Code:

for(int j =0; j < boxes.length; j++)
               {
                if((sprites[0].x>boxes[0].x) && (sprites[0].x< (boxes[0].x+boxes[0].w-22))&& (sprites[0].y <(boxes[0].y+boxes[0].h-20) && sprites[0].y >boxes[0].y))
                 {
                        image(CorrectImg, 78, 490);
                  }
               }
Re: Displaying 'Correct' image in drag and drop array
Reply #2 - Nov 6th, 2009, 9:05am
 
You could add properties to your sprite class to store the 'correct target' area - either by passing a reference to a target object, or simply storing appropriate values (e.g. x,y, width, height).  You could then check against this on mouseRelease to determine if the Sprite has been placed correctly.

It was actually your previous post that got me to dig out my old ActionScript drag and drop code and start converting it to Processing.  I posted a demo just the other day.  Not finished yet and really just an exercise to improve my OOP coding, but you might find some of it useful...
Re: Displaying 'Correct' image in drag and drop array
Reply #3 - Nov 6th, 2009, 2:15pm
 
Well earlier we tried placing this method into the Sprite class

Code:

for(int j =0; j < boxes.length; j++)
               {
                if((sprites[i].x>boxes[j].x) && (sprites[i].x< (boxes[j].x+boxes[j].w-22))&& (sprites[i].y <(boxes[j].y+boxes[j].h-20) && sprites[i].y >boxes[j].y))
                 {
                        image(CorrectImg, 78, 590);
                  }
               }


But it ended up having the same result. The question is how can check if a certain sprite aligns with a certain box. For example if sprite 1 aligns with box 1 and sprite 2 aligns with box 2. etc?
Re: Displaying 'Correct' image in drag and drop array
Reply #4 - Nov 7th, 2009, 2:27am
 
The answer is exactly what I said before...

It looks to me like you're checking against every target.  As I said: for each draggable object you need to store the correct target as a property.  You'll see in my BaseDragger class that I have a 'target' property, which is an array of type BaseTarget.  When I instantiate a dragger object a target, or an array of targets, must be passed as parameters to the class constructor:

draggers[i] = new CircleDragger(150 + random(0,width-200), 50 + random(0,height-100), diameter, targets);

My dragger object then contains an array of the target objects it can be placed on.  So when I want to check if it is placed correctly I iterate through this array - not the global array of targets (or boxes in your case) that you're using:

Code:
void mouseIsReleased() {
   for(int i=0; i<target.length; i++) {
     if(target[i].checkTarget(x,y,this)) {
       targetID = i;
       onTarget = true;
     }
   }
 }


You'll notice that I'm actually calling a method on the target - that way my dragger doesn't have to know what kind of target it's dealing with (circle,rect etc.), but you could just as easily reference your box properties.  So assuming your Sprite contains an array of targets:

Code:
  void mouseIsReleased() {
   for(int i=0; i<target.length; i++) {
       if(x > target[i].x && x < target[i].x+target[i].w){
           if(y > target[i].y && y < target[i].y+target[i].h){
               onTarget = true;  
           }
       }

   }
 }


You should be calling that as a method of your sprite class so you can reference x and y directly to get the current sprite's location...  Which looking at your code is possibly the other problem you're having.
Re: Displaying 'Correct' image in drag and drop array
Reply #5 - Nov 7th, 2009, 9:35am
 
In our case, would the 'target' be the equivalent of the 'boxes'. I looked at our code and was thinking that we were going to have to create a new class.

But maybe we just really need to edit the boxes class. Would this be correct?
Re: Displaying 'Correct' image in drag and drop array
Reply #6 - Nov 7th, 2009, 5:27pm
 
blindfish wrote on Nov 7th, 2009, 2:27am:
...targets (or boxes in your case)...

...but you could just as easily reference your box properties.


That more or less answers your question; though I think you will need to make changes in your Sprite class, not your Box class.
Re: Displaying 'Correct' image in drag and drop array
Reply #7 - Nov 8th, 2009, 11:47am
 
We made a few changes based on your suggestions but we're after running into more problems. Can you tell us we're at least on the right track?

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;

void setup()
{
 size(798, 760);
 
 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));
   
 }
 
 for(int i =0; i< boxes.length; i++)
 {
   boxY+=80;
   boxes[i] = new Box(boxX, boxY, 130, 25);
 }
}


void draw()
{
   
   b = loadImage("EiffelTower.jpg");
   CorrectImg = loadImage("Correct.jpg");
   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);
// Ah, this one is indeed dragged!
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();
               
               //correct image method
               /*
               for(int j =0; j < boxes.length; j++)
               {
                if((sprites[i].x>boxes[j].x) && (sprites[i].x< (boxes[j].x+boxes[j].w-22))&& (sprites[i].y <(boxes[j].y+boxes[j].h-20) && sprites[i].y >boxes[j].y))
                 {
                        image(CorrectImg, 78, 590);
                  }
               }*/
   }
    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;
 

 Sprite(PImage img, int SpriteSize,float x,float y)
 {
   this.x = x;
   this.y = y;
   this.img = img;
   
   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(boxes[i].checkBox(x,y,this))
                   {
                     boxID = i;
                     
                 
         }
                 }
                 
               }
 }
 

boolean IsDragged()
{
return m_bDragged;
}


   void Move()
   {
if (m_bDragged)
{
x = mouseX - m_clickDX;
y = mouseY - m_clickDY;
}
   }
   
   void Draw()
   {
     image(img, x, y);
     
   }
   
 
 
}


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, h, w);
     
   }
}


Re: Displaying 'Correct' image in drag and drop array
Reply #8 - Nov 8th, 2009, 3:27pm
 
OK - some progress...

One piece of advice - tidy up your code as you go along (e.g. make indentation consistent) - it makes it a lot easier to work with and for others to read.

You've added code to the Update method that will stop a Sprite from being dragged if it is placed over any box and from there you call a checkBox method:

Code:
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(boxes[i].checkBox(x,y,this)) {
     boxID = i;
   }
 }
}


The checkBox method currently only ever returns false.  You now need to figure out how to return true if it's in the right box...

Though TBH I wouldn't do it that way round:  You've already got a reference to the boxes: boxes[i].  If your Sprite class stored a reference to the 'correct' box you should just be able to check for equality between the two without calling a method on the box to determine if it's the right one.

Note that in my example the box/target method is used to determine if the sprite/dragger is within the bounds of the box; which isn't quite the same thing as determining if it is in the right box...
Re: Displaying 'Correct' image in drag and drop array
Reply #9 - Nov 8th, 2009, 11:48pm
 
I just woke up and realised that my last answer might have confused the issue slightly.  Forget about using boxes[i] as a reference to the box:  You've set up a int boxID property in your Sprite so you can probably just check this against the value of i:

Code:
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) {
// Assuming you're replacing the Sprite image
// i.e. you may want to adjust x and y properties depending on your need...
       image(CorrectImg, x, y);
   }
 }
}


Obviously you're also going to have to set that boxID property when you instantiate (create) a Sprite object, so you'll need to add a parameter to the constructor:

Code:
Sprite(PImage img, int SpriteSize,float x,float y, int boxID){
this.x = x;
   this.y = y;
   this.img = img;
   this.boxID = boxID


One other point that dawned on me was that whilst I said you should store the correct target for the Sprite as a property of the Sprite, you could just as well store the correct Sprite for the Box as a property of the Box: there are arguments for and against both options.  For instance if you know that a Sprite will only ever belong in one box, but a box could hold one of several Sprites it makes more sense to store and check this in the Sprite - that avoids the need for using an array of targets...

I'm going to have to think about which is the best option; though since I'm trying to produce a set of re-usable classes I'm having to take a more generic approach.  Of course the advantage of this is that my classes can easily be re-used in other projects - but then that's the whole point of OOP Wink
Re: Displaying 'Correct' image in drag and drop array
Reply #10 - Nov 9th, 2009, 9:38am
 
Thanks very much. That's worked out grand.
Re: Displaying 'Correct' image in drag and drop array
Reply #11 - Nov 9th, 2009, 10:34am
 
We're actually have a bit of a problem now. We two arrays starting at point 0 to point 6. If sprite 1 is moved into box 1 then it returns 'correct' image like it is suppose to.

However if sprite 1 is moved into 2, it still returns correct. This is true for each sprite with box + 1.

For example sprite 2 placed into box 3 returns true, so on so forth.

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;

void setup()
{
 size(798, 760);
 
 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, 130, 25);
 }
}


void draw()
{
   
   b = loadImage("EiffelTower.jpg");
   CorrectImg = loadImage("Correct.jpg");
   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);
// Ah, this one is indeed dragged!
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();
               
               //correct image method
               /*
               for(int j =0; j < boxes.length; j++)
               {
                if((sprites[i].x>boxes[j].x) && (sprites[i].x< (boxes[j].x+boxes[j].w-22))&& (sprites[i].y <(boxes[j].y+boxes[j].h-20) && sprites[i].y >boxes[j].y))
                 {
                        image(CorrectImg, 78, 590);
                  }
               }*/
   }
    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;
 

 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 )
                   {
 
                     image(CorrectImg, 78, 590);
                   }
                }
             }
                 
       }
 
 
 

boolean IsDragged()
{
return m_bDragged;
}


   void Move()
   {
if (m_bDragged)
{
x = mouseX - m_clickDX;
y = mouseY - m_clickDY;
}
   }
   
   void Draw()
   {
     image(img, x, y);
     
   }
   
 
 
}


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, h, w);
     
   }
}
Re: Displaying 'Correct' image in drag and drop array
Reply #12 - Nov 9th, 2009, 11:23am
 
OK...  First off: I politely suggested you clean up your code to make it more readable.  That didn't seem to get through, so let's put it this way: if you can't be bothered to make the effort to clean it up, then don't expect people to offer you help...  If nothing else it's good practice and will make it easier for you to work with.

Looking at your code is giving me headaches  Angry

See http://en.wikipedia.org/wiki/Indent_style if you're not sure what I'm talking about.

Second - look at the reference for rect() then tell me what's wrong with your Box's Draw method.  If you fix that you should be able to figure out what the problem is...
Re: Displaying 'Correct' image in drag and drop array
Reply #13 - Nov 9th, 2009, 11:25am
 
Oh - and if you want to improve performance don't load images in draw().  Do it in setup()...
Re: Displaying 'Correct' image in drag and drop array
Reply #14 - Nov 9th, 2009, 12:12pm
 
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;










void setup()
{
     size(798, 760);
 
    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, 130, 25);

  }
}







void draw()
{
   

     b = loadImage("EiffelTower.jpg");
     CorrectImg = loadImage("Correct.jpg");
    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;
 



  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 )
                    {
 
                     
                          image(CorrectImg, 78, 590);
                     

                    }
                 }
              }
 }    
       
 
 
 


boolean IsDragged()
{
return m_bDragged;
}




   void Move()
   {

if (m_bDragged)
{
 x = mouseX - m_clickDX;
 y = mouseY - m_clickDY;
}

   }
   
   
   void Draw()
   {
            image(img, x, y);
     
   }
   
 
 
}



   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, h, w);
     
     }
}



How does that look now?



The rect() reference is made in the Box.Draw method for the array of boxes. It is called in the draw method.

Code:

     for (int i=0; i <boxes.length; i++)
     {

       boxes[i].Draw();

     }
Pages: 1 2