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 › Image in corect position.
Page Index Toggle Pages: 1
Image in corect position. (Read 375 times)
Image in corect position.
Dec 5th, 2009, 10:09am
 
We're trying to create a word game with jumbled words in french and then the user has to click and drag word images into the place holder.

We have a text file with a string of jumbled words "wordsForButtons" and a string of words in the right order, "answerWords". The problem is that it displays correct when the user places them in the jumbled order.


Code:

void setupMainScreen(int currentQuestion, String[] wordsForButtons, String[] answerWords, String language)
{  
   
 for(int i = 0; i < wordsForButtons.length; i++)
 {
 
 
   //Checks to see how many buttons there are and
   //if more than 7 places the next button(s) on the next line
   if (numButtons == 7)
   {
     xCoord = 10;
     yCoord += 30;
     buttons.add(new Button(xCoord, yCoord, xCoord + 110, yCoord + 20, wordsForButtons[i], i));
   }
   else if (numButtons > 7)
   {
     xCoord += 120;
     buttons.add(new Button(xCoord, yCoord, xCoord + 110, yCoord + 20, wordsForButtons[i], i));
   }
   else
   {
     buttons.add(new Button(xCoord, yCoord, xCoord + 110, yCoord + 20, wordsForButtons[i], i));
     xCoord += 120;
   }
   numButtons++;
 }
 
 
 //Creates the boxes to match the correct amount of words
 boxes = new Box[answerWords.length];
 
 for(int i = 0; i < boxes.length; i++)
 {
   boxes[i] = new Box(boxXCoord, boxYCoord, 110, 25);
   boxXCoord += 130;
 }



So if the user has jumbled word #2 placed into box #2 it displays correct.
Code:

buttons.add(new Button(xCoord, yCoord, xCoord + 110, yCoord + 20, wordsForButtons[i], i));


The integer i in this case is the indicator of the jumbled word.

I thought about adding another for loop

Code:

for(int j = 0; j < answerWords.length; j++)
 {

and then
Code:
buttons.add(new Button(xCoord, yCoord, xCoord + 110, yCoord + 20, wordsForButtons[i], j)); 



But then this only drew the buttons three times each.



The code of the Button class:
Code:

//Class for the movable buttons

class Button
{
 int x, y;
 int wx, hy;
 int boxID;
 int correctX;
 int numCorrect = 0;
 boolean dragged = false;
 String textButton;
 boolean correctPosition = false;
 
 //Constructor for the button class
 public Button(int x, int y, int wx, int hy, String textButton, int boxID)
 {
   this.x = x;
   this.y = y;
   this.wx = wx;
   this.hy = hy;
   this.textButton = textButton;
   this.boxID = boxID;
 }

 void Draw()
 {
   textFont(buttonFont);
   //Store the coordinates
   pushMatrix();
     //Sets how the buttons will appear when dragged or not
     if(dragged)
     {
       fill(255, 255, 0, 100);
       stroke(255, 0, 0, 200);
     }
     else
     {
       fill(200, 0, 255, 100);
       stroke(0, 0, 255, 200);
     }
     strokeWeight(3);
     
     //Draws the points for each corner of the button
     beginShape();
       vertex(x, y);
       vertex(wx, y);
       vertex(wx, hy);
       vertex(x, hy);
     endShape(CLOSE);    
     fill(0, 0, 0);
     text(textButton, x + 20, y, wx, hy);
   popMatrix();
   
   if(correctPosition)
   {
     image(rightImage, correctX, 350);
   }
 }
 
 //Finds out if the button has been moved
 boolean Detect(int mx, int my)
 {
   if(mx >= x && mx <= wx && my >= y && my <= hy)      
     return true;
   
   return false;
 }

 //Moves the button to the new postion i.e. where the user last points their mouse
 void Move(int xc, int yc)
 {
   x += xc;
   wx += xc;
   y += yc;
   hy += yc;
   
   for(int i =0; i< boxes.length; i++)
   {
     if(dragged && x > boxes[i].x && x < (boxes[i].x + boxes[i].boxWidth) && y < (boxes[i].y + boxes[i].boxHeight) && y > boxes[i].y)
     {
       dragged = false;
       
       if((!dragged) && (i == boxID))
       {                  
 CorrectPosition(i);
         correctPosition = true;
         if(correctPosition == true)
         {
           numCorrect++;
         }
}
     }
   }
   println(numCorrect);
 }
 
 void CorrectPosition(int j)
 {  
    correctX = (50 + (j * 130));
    println(correctX);
    image(rightImage, correctX, 350);          
 }
}



Any suggestions?
Page Index Toggle Pages: 1