2D Toolkit Forum

2D Toolkit => Support => Topic started by: DarkSlash on April 29, 2015, 10:26:59 pm

Title: Fade In effect
Post by: DarkSlash 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;
    }
Title: Re: Fade In effect
Post by: unikronsoftware on April 29, 2015, 10:40:04 pm
The range for the Color type is 0..1 not 0..255
Title: Re: Fade In effect
Post by: DarkSlash on April 29, 2015, 10:46:05 pm
Don't understand what you're saying. Is not this?

Title: Re: Fade In effect
Post by: unikronsoftware on April 29, 2015, 11:21:10 pm
Look in the unity docs.
http://docs.unity3d.com/ScriptReference/Color.html
Title: Re: Fade In effect
Post by: DarkSlash 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?
Title: Re: Fade In effect
Post by: unikronsoftware 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.