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 › key pressed
Page Index Toggle Pages: 1
key pressed (Read 587 times)
key pressed
Oct 20th, 2009, 3:38pm
 
These length of codes are supported to do when every time a key is pressed, a letter and associated image are shown on the screen.  Plus, when another key is pressed, the previous letter and image are hidden.

Thanks for the comments, but I still cannot get each key pressed letter to correspondent to each image.


PFont font;
char letter = 'a';  
int maxImages = 26; // Total # of images
int imageIndex = 1; // Initial image to be displayed is the first
PImage img;

void setup()  
{
size(500, 500);
smooth();
font = loadFont("Candara-Bold-80.vlw");  
textFont(font);
textSize(100);  
 
img = loadImage("image1.gif");
img = loadImage("image2.gif");
img = loadImage("image3.gif");
img = loadImage("image4.gif");
img = loadImage("image5.gif");
img = loadImage("image6.gif");
img = loadImage("image7.gif");
img = loadImage("image8.gif");
img = loadImage("image9.gif");
img = loadImage("image10.gif");
img = loadImage("image11.gif");
img = loadImage("image12.gif");
img = loadImage("image13.gif");
img = loadImage("image14.gif");
img = loadImage("image15.gif");
img = loadImage("image16.gif");
img = loadImage("image17.gif");
img = loadImage("image18.gif");
img = loadImage("image19.gif");
img = loadImage("image20.gif");
img = loadImage("image21.gif");
img = loadImage("image22.gif");
img = loadImage("image23.gif");
img = loadImage("image24.gif");
img = loadImage("image25.gif");
img = loadImage("image26.gif");
}

void draw()
{
  background(173, 180, 216);  
  text(letter, 30, 90);  
   
  for(int i = imageIndex; i < maxImages; i +=1)
  {
    imageMode(CENTER);
    image(img, 264, 298);
   }    
}

void keyPressed()  
{
  if(key >= 'A' && key <= 'z')
    {
     letter = char(key);
     println(key);
    }
}
Re: key pressed
Reply #1 - Oct 21st, 2009, 12:04am
 
Well, each img = loadImage() line overwrites the previous value...
Beside, you should use a loop to do this job!
You can get an example of loading the images in an array in the Question about PImage syntax thread.
Page Index Toggle Pages: 1