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 & HelpSyntax Questions › Image transformations
Page Index Toggle Pages: 1
Image transformations (Read 811 times)
Image transformations
Feb 10th, 2010, 12:10pm
 
Hello

Can any kind person please take a look at my code below and give me suggestions on how to improve this? I am new to processing and basically have minimal experience in programming Sad

I placed several icons on the screen and the idea is to move them around when the mouse hovers near each one of them. I got it to work for the first icon (img2), but when I added img3 and img4 they only move when the mouse hovers near the upper left corner of img2. What did i do wrong?

// this one is the background
PImage img;

// icons
PImage img2;
PImage img3;
PImage img4;

// icons starting x and y position on the screen.
int img2x = 30;
int img2y = 50;
int img3x = 30;
int img3y = 150;
int img4x = 30;
int img4y = 200;



// this is the size for each icon
int boxSize = 100;




void setup() {
 size(800,600);
 img = loadImage("Default_800x600.jpg");
 img2 = loadImage("p_icon_01.png");
 img3 = loadImage("itunes.png");
 img4 = loadImage("My-computer-icon.png");
}


void draw() {
 background(0);

   image(img,0,0);
   image(img2, img2x, img2y);
   image(img3, img3x, img3y);
   image(img4, img4x, img4y);
   checkMouse();

}

void checkMouse(){

   // If the mouse is inside the box..
   if(mouseX > img2x &&
     mouseX < img2x+boxSize &&
     mouseY > img2y &&
     mouseY < img2y+boxSize
 )
   
   {
     //if the mouse is in the area, rough syntax
     //suggestions for improvement?

     img2x = img2x + int(random(-30, 30));
     img2y = img2y + int(random(-30, 30));
     
     img3x = img3x + int(random(-30, 30));
     img3y = img3y + int(random(-30, 30));
     
     img4x = img4x + int(random(-30, 30));
     img4x = img4y + int(random(-30, 30));
     
     
   }
 }
Re: Image transformations
Reply #1 - Feb 10th, 2010, 12:19pm
 
Quote:
  // If the mouse is inside the box..
  if(mouseX > img2x &&
    mouseX < img2x+boxSize &&
    mouseY > img2y &&
    mouseY < img2y+boxSize
)


Q1) Which box is this if statement checking the mouse is near?
 
Quote:
  {
    //if the mouse is in the area, rough syntax
    //suggestions for improvement?

    img2x = img2x + int(random(-30, 30));
    img2y = img2y + int(random(-30, 30));
   
    img3x = img3x + int(random(-30, 30));
    img3y = img3y + int(random(-30, 30));
   
    img4x = img4x + int(random(-30, 30));
    img4x = img4y + int(random(-30, 30));
  }


Q2) And which icons is it moving if it's near that box?

Q3) Does that seem right?

Q4) What does the code "int x=2; x+=6;" do differently from the code "int x=2; x = x + 6;"?
Re: Image transformations
Reply #2 - Feb 10th, 2010, 12:37pm
 
Hello,

A1) I believe the statement is checking the intboxSize = 100

A2) All of the icons are moving at the same time, but only when the mouse is near img2

A3) no...what would you suggest doing?

A4) I'm not sure what += means but i think x= x+6 mean moving the icon 6 coordinates along the x-axis
Re: Image transformations
Reply #3 - Feb 10th, 2010, 2:00pm
 
A1 - Your condition only checks if the mouse is over img2:

Code:
if(mouseX > img2x &&
    mouseX < img2x+boxSize &&
    mouseY > img2y &&
    mouseY < img2y+boxSize)


For what you're trying to achieve you obviously need a separate condition for each image.

A2 - As you should expect: you also need to move the movement code into separate conditions for each image.

A3 - So as suggested above you should have something along this structure (in pseudo-code):

Code:
if (mouse is over img2){
 move(img2)
}
if (mouse is over img3){
 move(img3)
}
if (mouse is over img4){
 move(img4)
}


There are more efficient ways of doing this, but you should get to grips with this method first Wink

A4 - That was a trick question: (x += 6;) is just a shortcut to writing (x = x+6;).  I.e. it adds 6 to the value of x, so does indeed move the image 6 pixels to the right.  (Once you start writing larger sketches these seemingly trivial shortcuts save time!)
Re: Image transformations
Reply #4 - Feb 11th, 2010, 1:08am
 
Thank youuuu

i got it working now Smiley
Page Index Toggle Pages: 1