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 › How to prevent an int increasing in each frame
Pages: 1 2 
How to prevent an int increasing in each frame (Read 2092 times)
How to prevent an int increasing in each frame
Nov 13th, 2009, 8:55am
 
We have the code design for a word game which locks an image into place when the user drags it over a box. We're trying to create an integer which increases each time the user drags the correct image onto the correct box and then display the image 'correct'.

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);
                     
                     }
                 }
            }
         }


The problem is that the boxClick integer increases on each frame. How can we increase it only when the correct image is placed and not on each single frame?
Re: How to prevent an int increasing in each frame
Reply #1 - Nov 13th, 2009, 9:00am
 
Didn't I already answer this?
Re: How to prevent an int increasing in each frame
Reply #2 - Nov 13th, 2009, 9:11am
 
Code:

if(i == boxID && m_bDragged)
{
boxClick++;
println(boxClick);
if(boxClick == boxes.length)
{

image(CorrectImg, 78, 590);

}
m_bDragged = false;
}
Re: How to prevent an int increasing in each frame
Reply #3 - Nov 13th, 2009, 9:51am
 
Code:

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)
               {
                 
                     
                   if(i == boxID && m_bDragged)
           {
 boxClick++;
println(boxClick);
                       
   if(boxClick == boxes.length)
   {
     
image(CorrectImg, 78, 590);
   
   }
   m_bDragged = false;  
}
            }


This is still creating a problem. The correct image only flashes once when the correct image is on the correct box.

The aim is create it so that when all boxes are correct. For example seven boxes with seven images and each placed correctly. Then boxClick=7, i.e the same length of the boxes, boxes.length.

Seven correct boxes, then return correct
Re: How to prevent an int increasing in each frame
Reply #4 - Nov 13th, 2009, 11:54am
 
blindfish wrote on Nov 13th, 2009, 9:00am:
Didn't I already answer this


lol.

darklord, post the entire thing including a link to the pictures so we can run it and debug it ourselves. it's very hard to extrapolate the entire program from snippets.

and please don't start any more threads about it. that's 3 now.
Re: How to prevent an int increasing in each frame
Reply #5 - Nov 13th, 2009, 1:20pm
 
darklord84 wrote on Nov 13th, 2009, 9:51am:
This is still creating a problem. The correct image only flashes once when the correct image is on the correct box.

Well, I don't know the remainder of the context, so the code was correct for the given snippet... Smiley
Instead of doing image() call immediately, just set a global boolean ("bShowCorrect" for example) and in the draw() function, call the image() if bShowCorrect is true.
Re: How to prevent an int increasing in each frame
Reply #6 - Nov 13th, 2009, 1:54pm
 
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?
Re: How to prevent an int increasing in each frame
Reply #7 - Nov 13th, 2009, 2:02pm
 
Typo. *The line "if(boxClick == boxes.length)" is 'useless' and does the same without this if statement.

It works the same as just
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);
                       

     
//image(CorrectImg, 78, 590);
                               bShowCorrect=true;
   
   
}
              }
           
           
         }

Any more suggestions?
Re: How to prevent an int increasing in each frame
Reply #8 - Nov 14th, 2009, 1:27am
 
The problem now is that you've not followed the advice given.  You've moved the boxClick variable to be a property of Sprite.  That means each Sprite stores a separate value for boxClick which won't be affected when the other Sprites are placed correctly...
Re: How to prevent an int increasing in each frame
Reply #9 - Nov 14th, 2009, 1:50am
 
Also when any of the Sprites are placed correctly bShowCorrect is set to 'true' meaning that correctImg is shown regardless of the value of boxClick.

The problem here isn't that our suggestions don't work - or that they are "useless" - but that you haven't properly understood how to implement them.  It's rather rude to simply start a new thread when you don't get the answer you want to hear; or are simply hoping for someone to post the solution direct in your code.  If you want to learn and understand it's much better for you to implement suggested solutions yourself.
Re: How to prevent an int increasing in each frame
Reply #10 - Nov 17th, 2009, 7:25am
 
Okay, we apologise for the mis-interpretation. We stil just don't understand how to increase the integer only when the image is corrently placed rather than increasing in each fram rate.

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((!m_bDragged) &&(i == boxID))
                  {
                     boxClick++;
                     for(int j =0; j< boxes.length; j++)
                     {
 //boxClick++;
//println(boxClick);
                        if(j==boxes.length)
                        {

                               bShowCorrect=true;
                               println(j);
   
  }
                     }
   

              }

We tried a different approach here, but it is still causing the same problem.

I tried decreasing the framrate of the overall application, but this only slowed down the running of the game.

Is there a way of increasing an integer in a for loop only on one single frame?

We also tried increasing it in a boolean method but this only caused the same problem.
Re: How to prevent an int increasing in each frame
Reply #11 - Nov 17th, 2009, 7:43am
 
Something like:
Code:
for(int i =0; i< boxes.length; i++)
   {
 //method for locking image in place
   if(m_bDragged && 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++;
   if(boxClick == boxes.length)
   {
 bShowCorrect=true;
   }
}
  }
   }
perhaps.
Re: How to prevent an int increasing in each frame
Reply #12 - Nov 17th, 2009, 8:13am
 
Okay that's really good, it prevents the boxClick from increasing on every single frame. The slight now is that it increases only when user is clicking the correct image onto the correct box and increases for the amount of frames to do so.

For example if the user takes 13 milliseconds to place the image then boxclick increases and stops at 13. Which is better than gong all the way to several hundred.

Code:

for(int i =0; i< boxes.length; i++)
   {
 //method for locking image in place
   if(m_bDragged && 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((!m_bDragged) &&(i == boxID))
                         {
     boxClick++;
                             println(boxClick);
   if(boxClick == boxes.length)
   {
 bShowCorrect=true;
   }
                         }
}
  }


If there a way to prevent boxClick from ncreasing to much and just stop at one.
The idea is that when the correct imae is placed, it wold only increase by one and then if it reached the same number of boxes, ie all correct it would return correct.

Re: How to prevent an int increasing in each frame
Reply #13 - Nov 17th, 2009, 9:15am
 
Quote:
if the user takes 13 milliseconds to place the image then boxclick increases and stops at 13.

I haven't tested the whole stuff, but I am surprised, as m_bDragged is set to false, so we shouldn't go there anymore.

But well, you can execute this part of code only if m_bDragged (user is dragging) and if !mousePressed (user just released the mouse button).
So the Update() logic goes like:
Code:

void Update(boolean bAlreadyDragging)
{
// Check if mouse is over the sprite
m_bIsHovered = mouseX > x && mouseX < x + SpriteSize &&
mouseY > y && mouseY < y + SpriteSize/10;

// User pressed the mouse button on the sprite and no dragging is already occurring
if (!bAlreadyDragging && mousePressed && mouseButton == LEFT && m_bIsHovered)
{
// We record the state
m_bDragged = true;

m_clickDX = mouseX - x;
m_clickDY = mouseY - y;
return;
}

// If mouse isn't pressed and sprite was dragged, user just released the mouse button
if (!mousePressed && m_bDragged)
{
// Dragging is stopped
m_bDragged = false;

//method for locking image in place
for (int i = 0; i < boxes.length; i++)
{
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)
{
if (i == boxID)
{
boxClick++;
//println(boxClick);

if (boxClick == boxes.length)
{
bShowCorrect = true;
}
}
}
}
}
}

Still untested.
Note: you should take care of proper indentation, even on unfinished code (it is easier to maintain indentation than fix it all at the end anyway): it helps in reading code and understanding it.
Re: How to prevent an int increasing in each frame
Reply #14 - Nov 17th, 2009, 10:31am
 
Thanks for the reply. I just tested this code you gave and now it's not locking the images into any of the boxes.
Pages: 1 2