Hello Guest

Author Topic: Fade In effect  (Read 4461 times)

DarkSlash

  • Newbie
  • *
  • Posts: 19
    • View Profile
Fade In effect
« on: April 29, 2015, 10:26:59 pm »
I want to create a fade in/out class (don't know why the ones in iTween doesn't work for me).  I wrote this code but this doesn't affect the alpha color of the sprite. What I'm doing wrong?

Code: [Select]
    public int      speed = 4;

    bool            isFadeIn = false;
    bool            isFadeOut = false;
    float           alphaColor = 0;
    Color           spriteColor;

    void Update()
    {
    if(isFadeIn == true && alphaColor < 255)
        {
            alphaColor += Time.deltaTime * speed;
            spriteColor.a = alphaColor;
            GetComponent<tk2dSprite>().color= spriteColor;
        }
    }

    public void FadeIn()
    {
        isFadeIn = true;
    }

    public void FadeOut()
    {
        isFadeOut = true;
    }

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Fade In effect
« Reply #1 on: April 29, 2015, 10:40:04 pm »
The range for the Color type is 0..1 not 0..255

DarkSlash

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Fade In effect
« Reply #2 on: April 29, 2015, 10:46:05 pm »
Don't understand what you're saying. Is not this?


unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Fade In effect
« Reply #3 on: April 29, 2015, 11:21:10 pm »

DarkSlash

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Fade In effect
« Reply #4 on: April 30, 2015, 12:22:01 am »
I understand that. The question is how to access to the alpha color property in the script. I tried GetComponent<tk2dSprite>().color.a and doesn't work. Also doesn't respond when I apply a new color with GetComponent<tk2dSprite>().color = colorVariable. So, how is the correct way to access to a sprite color?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Fade In effect
« Reply #5 on: April 30, 2015, 07:38:08 pm »
Quote
       if(isFadeIn == true && alphaColor < 255)
        {
            alphaColor += Time.deltaTime * speed;
            spriteColor.a = alphaColor;
            GetComponent<tk2dSprite>().color= spriteColor;
        }

You're moving alphaColor from 0..255. The range for alpha is 0..1.