- Home /
fading with sprites?
I'm currently working with a 2d game with sprites and i'm using ex2d for the 2D sprites. I wanted to make a sprite i created to fade from visible to transparent,and i was wondering how to do it. the first thing that came to mind was using the iTween.FadeTo function which gave me an error, i did some searching and noticed i needed Transparent/diffuse type of shader or something close to it. Unfortunately ex2d sprites use the customed ex2d/alpha blended shader which resulted in the error, and changing the shader to transparent/diffuse obviously doesn't help. is there a way to do it? or am i just going about it wrong?
Answer by delstrega · Jun 24, 2012 at 05:47 AM
Just use the standard ex2d/alpha blended shader and change the exSprites color's alpha value.
Some example code (slap that onto the sprite you want to fade):
FadeMe.js:
#pragma strict
var mySprite : exSprite;
var fadeValue : float = 1;
var currentTime: float = 0;
var timeItTakesToFade = 1;
var isFading : boolean = false;
function Start () {
mySprite = GetComponent(exSprite);
StartFade(5);
}
function Update () {
if(isFading){
currentTime += Time.deltaTime;
if(currentTime <= timeItTakesToFade){
fadeValue = 1 - (currentTime / timeItTakesToFade);
mySprite.color = Color(1,1,1, fadeValue);
}else{
isFading = false;
}
}
}
function StartFade (howLong : float) {
currentTime = 0;
timeItTakesToFade = howLong;
isFading = true;
}
This is just an example to get you started :)
Hope that helps,
delstrega
@delstrega - I figured the shader must have an alpha - glad you knew it did!
Answer by MrHasuu · Jun 25, 2012 at 05:40 AM
This was exactly what i needed, thank you so much!
anyway so this was what i ended up with in the end: my goal was to have an object pulsing on the screen, but theres several versions of it and i didnt want to import pulsing animation with sprites which means a bunch of png being added in. So for anyone else who may need this in the future here's what I ended up with in C#
private float time = 10.0f;
private exSprite temp;
private float fadeValue = 1f;
private float currentTime = 0f;
private const float timeItTakesToFade = 0.5f;
private bool isFading = false;
private bool isAppearing = false;
void Start() {
temp = gameObject.GetComponent<exSprite>();
StartFade();
}
void Awake() {
Destroy(gameObject, time);
}
void Update() {
if(isFading){
currentTime += Time.deltaTime;
if(currentTime <= timeItTakesToFade){
fadeValue = 1f - (currentTime / timeItTakesToFade);
temp.color = new Color(1f,1f,1f, fadeValue);
}else{
isFading = false;
StartAppear();
}
}
if(isAppearing){
currentTime += Time.deltaTime;
if(currentTime <= timeItTakesToFade) {
fadeValue = 1f + (currentTime / timeItTakesToFade);
temp.color = new Color(1f, 1f, 1f, fadeValue);
}else{
isAppearing = false;
StartFade();
}
}
}
private void StartFade() {
currentTime = 0;
isFading = true;
}
private void StartAppear() {
currentTime = 0;
isAppearing = true;
}
Hello $$anonymous$$rHasuu!
I think there is an error in your code.
Replace this (line 34):
fadeValue = 1f + (currentTime / timeItTakesToFade);
with:
fadeValue = currentTime / timeItTakesToFade;