Hi, I've made a sketch with several images appearing and fading out by pressing keys.
It have worked perfectly, but after adding a background image, everything got messed up.
I gave the pressing action to only object images, but background image also react to pressing key.
Here's the code;
- /////////////setting for image///////////////
- PImage[] pat = new PImage[13];
- PImage bg;
- ////////////extra settings//////////////
- float[] time = new float[13];
- float going = 5;
- float maxAlpha = 255;
- void setup()
- {
- size(1024, 768);
- //load images
- bg = loadImage("bg_trans.png");
- for(int i=0; i<pat.length; i++)
- {
- pat[i] = loadImage("pattern" + nf(i+1, 2) + ".png");
- }
- imageMode(CENTER);
- for(int j=0; j<13; j++)
- {
- time[j]=255;
- }
- }
- void draw()
- {
- background(0,0,75);
- image(bg, width/2, height/2); // this is the background image I called
- tint(255, 40); // I want to fix the alpha of background image at 40
- for(int k=0; k<13; k++)
- {
- if(time[k] < maxAlpha)
- {
- image(pat[k], width/2, height/2);
- tint(255, maxAlpha-time[k]); //I want to give this tint() to only object images, not to background image
- time[k] = time[k] + going;
- }
- }
- }
- void keyPressed()
- {
- if ( key == 'z' )
- {
- time[0] = 0;
- }
- if ( key == 'x' )
- {
- time[1] = 0;
- }
- if ( key == 'c' )
- {
- time[2] = 0;
- }
- if ( key == 'v' )
- {
- time[3] = 0;
- }
- if ( key == 'b' )
- {
- time[4] = 0;
- }
- if ( key == 'a' )
- {
- time[5] = 0;
- }
- if ( key == 's' )
- {
- time[6] = 0;
- }
- if ( key == 'd' )
- {
- time[7] = 0;
- }
- if ( key == 'f' )
- {
- time[8] = 0;
- }
- if ( key == 'g' )
- {
- time[9] = 0;
- }
- if ( key == 'q' )
- {
- time[10] = 0;
- }
- if ( key == 'w' )
- {
- time[11] = 0;
- }
- if ( key == 'e' )
- {
- time[12] = 0;
- }
- }
- }
I thought that tint() would only have effect on object images(in this case, pat[] I mean)....But it also affect bg.
The alpha value of bg should be fixed at 40.
Would someone help me, please? I don't have any idea...
(I cannot use background(bg) because bg is smaller than application size and I don't want to change the size of image 'bg'. 'bg' is PNG image with transparent background and I just want to put it on the background(0,0,75), at the center of the screen.)
1