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 › How to make images move within functions
Page Index Toggle Pages: 1
How to make images move within functions? (Read 425 times)
How to make images move within functions?
Jul 30th, 2008, 11:39am
 
i've been struggling on the script for long,  

how do you actually make the image move a certain distance you want or cause its width to increase to a certain length.

Right now in my script, the part where the image is called is actually inside :

void draw(){
 if(){
   if(){
     image();  //   <<<<<
   }
 }

}


How do I make it move a certain distance or change in width if the "if" condition is met?

also without using any other library like "megamu.shapetween", just the normal script:


width += 5; //til width reaches a certain value.


Your suggestions would be greatly appreciated
Re: How to make images move within functions?
Reply #1 - Jul 30th, 2008, 1:15pm
 
as far as I understand your problem the solution is simple,
you can add some global variables:

int counter = 0, maxcounter = 100;

and now:

void draw(){
//yours if structure
if(counter < maxcounter)
{
 image();
 counter++;
}
//
}

Re: How to make images move within functions?
Reply #2 - Jul 31st, 2008, 7:16am
 
thanks alot for ur effort to reply (:
but there's still some problem..


Code:

//assuming width is 101px

int counter=0, maxcounter=100;

void draw(){
 if(){
   image.width = width - 100;
   if(){
     image.width += 1;
     image();  
     counter++;
   }
 }
}

//the image moves 100px rightwards



when i track the image.width it remains as the value of
 
 image.width = width - 100;

it prints 100 times and ends there with the image gone too.


but when i tried with the x position rather than the width of the image, tracking the x  position, the values did increase by 100 at the end but the image doesn't follow the x value.

by any chance do you know the problem of this?


Re: How to make images move within functions?
Reply #3 - Jul 31st, 2008, 9:41am
 
Quote:
How do I make it move a certain distance or change in width if the "if" condition is met?


Store the destination you want to reach in a variable (here : destX).

Quote:
int posX, destX;

void draw() {
 
 // clear the background
 background(255);
 
 // moves the rect/image if necessary
 if (posX < destX) posX++;
 if (posX > destX) posX--;
 
 // if condition is satisfied, set the new destination
 if (mousePressed) {
   destX = mouseX;
 }
 
 // draw the rect/image
 rect(posX, 10, 10, 10);
 
}
Page Index Toggle Pages: 1