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 › Help on a school project
Page Index Toggle Pages: 1
Help on a school project (Read 402 times)
Help on a school project
Jan 16th, 2008, 12:26am
 
I have been searching for hours on a way to make a few .gif images move across, on and off a screen. I have searched several different example in the basics and library info. One i came across specifically was the movement of a line, and was trying to replace the line with an image. But I have no idea if you can create a value for an image.

Your help is appreciated, Thanks
Re: Help on a school project
Reply #1 - Jan 16th, 2008, 8:40am
 
Hello,

I'm a complete novice with Processing, but I will try to be helpful.

It seems that this example can be useful for you: http://processing.org/learning/basics/sprite.html

Here it is the code:

PImage teddy;

float xpos;
float ypos;
float drag = 30.0;

void setup()
{
 size(200,200);
 teddy = loadImage("teddy.gif");
 xpos = width/2;
 ypos = height/2;
 frameRate(60);
}

void draw()
{
 background(102);
 
 float difx = mouseX - xpos-teddy.width/2;
 if(abs(difx) > 1.0) {
   xpos = xpos + difx/drag;
   xpos = constrain(xpos, 0, width-teddy.width);
 }  
 
 float dify = mouseY - ypos-teddy.height/2;
 if(abs(dify) > 1.0) {
   ypos = ypos + dify/drag;
   ypos = constrain(ypos, 0, height-teddy.height);
 }  
 
 // Display the sprite at the position xpos, ypos
 image(teddy, xpos, ypos);
}


To do an animation is easy. First, you clean the screen using "background". Then, you put the image on the screen. Because this is in a draw(), the sequence of steps will be repeated time after time. You only need to change the coordinates of the image just a little in every step. And, that's all! You will have an animation.

Javier
Page Index Toggle Pages: 1