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 could this work
Page Index Toggle Pages: 1
How could this work? (Read 571 times)
How could this work?
May 10th, 2009, 12:16am
 
How could i get this image- the rabbit to move up, down and to the sides when a key is pressed? i know the key command in the reference but i don't know how to combine it to movements and the image

Thanks
louisa

PImage c;
void setup(){ size(900,800);
c=loadImage("Space-Rabbit.png");
smooth();
}

//ideas: have the rabbit jumping around to key presses.

void draw(){
 background(0);
 
 fill(255);//moon
noStroke();
ellipse(450,900,1000,900);
noFill();

image(c,300,195,180,380);//Rabbit
println(c.width);// prints the image width


line(mouseX-5,mouseY,mouseX+5,mouseY);
line(mouseX,mouseY-5,mouseX,mouseY+5);
println("x "+mouseX+" :y "+mouseY);
}
Re: How could this work?
Reply #1 - May 10th, 2009, 1:40am
 
You have to store the position of the rabbit.
On keyPressed(), you update this position, + or - a given offset.
Re: How could this work?
Reply #2 - May 10th, 2009, 2:16am
 
Could anyone elaborate please? im still a little confused
Re: How could this work?
Reply #3 - May 10th, 2009, 3:56am
 
- Define some variables to store the X and Y position of the rabbit (e.g. int rabbitX, rabbitY).
- Do this just below where you declare "PImage c;" and before setup() so they're global.
- In setup() assign the starting values for X and Y - this will be where the rabbit is displayed when the code first runs.
- In draw() adjust the rabbitX and rabbitY appropriately based on keypresses - i.e. something along the lines of rabbitX +=3 when the right arrow is pressed...
- Now instead of hard-coding the x and y into your call to image in the draw() loop:

image(c,300,195,180,380);//Rabbit

pass the values of rabbitX and rabbitY:

image(c,rabbitX,rabbitY,180,380);//Rabbit

Edit: see the sprite example if this still isn't clear.  The only difference in your case is you're using the keyboard to change the 'xpos' and 'ypos'...
Re: How could this work?
Reply #4 - May 10th, 2009, 3:57am
 
Quote:
How could i get this image- the rabbit to move up, down and to the sides when a key is pressed?

...
image(c,300,195,180,380);//Rabbit
...


the first argument to image is the img to use, the second and third are x and y positions. make these variables and change them on keypresses

image(c, x, y, 180, 380); // rabbit at x,y

then do something like this in your keypress routine
(this is pseudocode, you'll have to look up the correct syntax)
and you'll need to define x and y as ints globally (ie before setup())

if (key == up) then
 y--;
fi

if (key == down) then
 y++;
fi

if (key == right) then
 x++;
fi

if (key == left) then
 x--;
fi
Page Index Toggle Pages: 1