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 keeps repeating when moving with keypressed
Page Index Toggle Pages: 1
Image keeps repeating when moving with keypressed (Read 444 times)
Image keeps repeating when moving with keypressed
Jul 3rd, 2009, 3:53am
 
Hey all,

I m busy with a school project and i m stuck with something.
I am busy making a maze and with "WASD" you can move around. Normally when I m clicking, the loaded image should be moving(moving person)
The only problem is that when i m pressing one of these buttons that the image repeats and leaves a big stripe behind it. But I dont want that because the image should be moving in place of repeating.

My code:
Code:

PImage img;
PImage persoon;
int numPixels; // definieer numPixels als int
color trackColor;
int value = 0;
int x = 10;
int y = 10;
int b;
int c;

void setup(){
 img = loadImage("doolhof.bmp");
 persoon = loadImage("persoon.gif");
 size(img.width,img.height);
 trackColor = color(255,0,0);
 img.updatePixels(); // vernieuw pixels
 image(img, 0, 0); // laat afbeelding zien
 image(persoon,x,y);
 frameRate(50);
}

void draw() {
 //omlaag
 if(keyPressed) {
   if (key == 's' || key == 'S') {
     persoon = loadImage("persoon.gif");
     b = y - 30;
     y = y + 1;
     if(img.pixels[x + y * img.width] == trackColor){
       y = y - 1;
     }
   }
 }

 //omhoog
 if(keyPressed) {
   if (key == 'w' || key == 'W') {
     persoon = loadImage("omhoog.gif");
     b = y + 30;
     y = y - 1;
     if(img.pixels[x + y * img.width] == trackColor){
       y = y + 1;
     }
   }
 }

 //links
 if(keyPressed) {
   if (key == 'a' || key == 'A') {
     persoon = loadImage("links.gif");
     c = x + 30;
     x = x - 1;
     if(img.pixels[x + y * img.width] == trackColor){
       x = x - 10;
     }
   }
 }

 //rechts
 if(keyPressed) {
   if (key == 'd' || key == 'D') {
     persoon = loadImage("rechts.gif");
     c = x - 30;
     x = x + 1;
     if(img.pixels[x + y * img.width] == trackColor){
       x = x + 10;
     }
   }
   image(persoon,x,y);
 }
}


Need to make 5 dummy posts to post an image :/



Re: Image keeps repeating when moving with keypressed
Reply #1 - Jul 3rd, 2009, 3:53am
 
dummy2
Re: Image keeps repeating when moving with keypressed
Reply #2 - Jul 3rd, 2009, 3:54am
 
dummy3
Re: Image keeps repeating when moving with keypressed
Reply #3 - Jul 3rd, 2009, 3:54am
 
dummy4
Re: Image keeps repeating when moving with keypressed
Reply #4 - Jul 3rd, 2009, 3:55am
 
dummy5
Re: Image keeps repeating when moving with keypressed
Reply #5 - Jul 3rd, 2009, 3:55am
 
Finnally i can post an image:o
This is the issue
...
Re: Image keeps repeating when moving with keypressed
Reply #6 - Jul 3rd, 2009, 4:53am
 
You made two mistakes made by most newcomers to Processing:
- Avoid to do loadImage() in draw(), just load the images you need in setup(). It is faster as it will do less disk accesses (even if system put the files in cache...).
- More related to your issue, you just forgot to call background() at the start of draw().
Or, rather in your case, you must call image() with the maze image instead, to restore its pristine state and draw a clean image of character over it.
Of course, all images must be declared before setup().

Additional note: ideally, size() call should have only constant values (and be first instruction of setup()).
Page Index Toggle Pages: 1