# Tint() in for loop doesn't work

**URL:** <https://discourse.processing.org/t/tint-in-for-loop-doesnt-work/37976>\
**Category:** Coding Questions\
**Created:** [July 15, 2022, 5:17pm UTC](https://discourse.processing.org/t/tint-in-for-loop-doesnt-work/37976 "2022-07-15T17:17:41Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andry](https://avatars.discourse-cdn.com/v4/letter/a/b487fb/32.png) [@Andry](https://discourse.processing.org/u/Andry)\
**Post date:** [July 15, 2022, 5:17pm UTC](https://discourse.processing.org/t/tint-in-for-loop-doesnt-work/37976/1 "2022-07-15T17:17:41Z")

</div>

Hi, I have this code that doesn’t work, why?

```auto
PImage img;

void setup() {
  size(400, 300);
  img= loadImage("myimage.jpg");
  image(img,0,0,width, height); 
}
 
void draw() {
 
}

float transparency = 255;
void keyPressed() {
      for (transparency=255;transparency>0;transparency = transparency-1){
      delay(10); 
      println(transparency);// entered to check: this works 
      image(img,0,0,width, height);      
      tint(255, transparency);// this does not work, why?
      }
}`Preformatted text`

```

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [July 15, 2022, 7:20pm UTC](https://discourse.processing.org/t/tint-in-for-loop-doesnt-work/37976/2 "2022-07-15T19:20:20Z")

</div>

Hi @Andry,

First, Please [format your code](https://discourse.processing.org/faq#format-your-code). This makes use easier to help you. 🙂

What is your expectation of what this code should do?

Cheers  
— mnse

---

<div class="post-metadata">

**Author:** ![Andry](https://avatars.discourse-cdn.com/v4/letter/a/b487fb/32.png) [@Andry](https://discourse.processing.org/u/Andry)\
**Post date:** [July 15, 2022, 7:51pm UTC](https://discourse.processing.org/t/tint-in-for-loop-doesnt-work/37976/3 "2022-07-15T19:51:14Z")

</div>

sorry for the incorrect formatting, hope i have solved

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [July 15, 2022, 7:58pm UTC](https://discourse.processing.org/t/tint-in-for-loop-doesnt-work/37976/4 "2022-07-15T19:58:54Z")

</div>

Unfortunately not

> Reformat existing code:
> 
> 1. Click the edit (pencil) icon under an old post
> 2. Highlight code
> 3. On the editor header bar, press the `</>` code button (context name: Preformatted text) …or press Ctrl+Shift+c.

> [@mnse](#):
>
> What is your expectation of what this code should do?

---

<div class="post-metadata">

**Author:** ![Andry](https://avatars.discourse-cdn.com/v4/letter/a/b487fb/32.png) [@Andry](https://discourse.processing.org/u/Andry)\
**Post date:** [July 16, 2022, 7:16am UTC](https://discourse.processing.org/t/tint-in-for-loop-doesnt-work/37976/5 "2022-07-16T07:16:38Z")

</div>

Done, thanks, it should be fine now.

---

<div class="post-metadata">

**Author:** ![Andry](https://avatars.discourse-cdn.com/v4/letter/a/b487fb/32.png) [@Andry](https://discourse.processing.org/u/Andry)\
**Post date:** [July 16, 2022, 7:20am UTC](https://discourse.processing.org/t/tint-in-for-loop-doesnt-work/37976/6 "2022-07-16T07:20:27Z")

</div>

The code should produce an animated fade on the image, but this is not the case.

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [July 16, 2022, 7:53am UTC](https://discourse.processing.org/t/tint-in-for-loop-doesnt-work/37976/7 "2022-07-16T07:53:20Z")

</div>

Hi,

This won’t work that way…

The keypressed event is handled at once, so your image is currently painted from opaque (255) to transparent (0) all at the same time.  
Also use the tint before drawing the image.

Check here and see what happend…

Cheers  
— mnse

```auto
PImage img;
int transparency=0;

void setup() {
  size(400, 300);
  img= loadImage("myimage.jpg");
  // image(img,0,0,width, height); // useless
}
 
void draw() {
  background(0);
  tint(255, transparency);
  image(img,0,0,width, height);      
  transparency=(transparency+1) % 255;
}

```

---

<div class="post-metadata">

**Author:** ![Andry](https://avatars.discourse-cdn.com/v4/letter/a/b487fb/32.png) [@Andry](https://discourse.processing.org/u/Andry)\
**Post date:** [July 16, 2022, 9:51am UTC](https://discourse.processing.org/t/tint-in-for-loop-doesnt-work/37976/8 "2022-07-16T09:51:50Z")

</div>

Thanks, you were kind enough to answer me. Your code works, but I wanted it to work in a for loop, but if that’s not possible I’ll find an adaptation. Good day

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [July 16, 2022, 10:12am UTC](https://discourse.processing.org/t/tint-in-for-loop-doesnt-work/37976/9 "2022-07-16T10:12:41Z")

</div>

I would recommend to reconsider your approach … a for loop imho just makes no sense in this concrete constellation… 🙂

you can imagine the sketch as

```auto
for (frameCount = 0; as_Long_Sketch_Is_Running_And_No_Noloop_used;frameCount++) {
   // do things to prepare next draw call and call it
   draw();
}

```

or

```auto
while (as_Long_Sketch_Is_Running_And_No_Noloop_used) {
   // do things to prepare next draw call and call it
   draw();
}

```

But sure, you can do whatever you want in your coding … 😉

Cheers  
— mnse

---

<div class="post-metadata">

**Author:** ![Andry](https://avatars.discourse-cdn.com/v4/letter/a/b487fb/32.png) [@Andry](https://discourse.processing.org/u/Andry)\
**Post date:** [July 16, 2022, 10:26am UTC](https://discourse.processing.org/t/tint-in-for-loop-doesnt-work/37976/10 "2022-07-16T10:26:04Z")

</div>

Thanks this is a good solution. Good boy

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [July 16, 2022, 1:47pm UTC](https://discourse.processing.org/t/tint-in-for-loop-doesnt-work/37976/11 "2022-07-16T13:47:58Z")

</div>

Hello,

This tutorial discusses _Events_ and _Event flow_:

> **[Interactivity](https://processing.org/tutorials/interactivity/)**
>
> Introduction to interactivity with the mouse and keyboard.

`:)`
