Image Fade In/Out
Monkey Forums/Monkey Programming/Image Fade In/Out| 
 | ||
| I would like to fade an image in and out, from 0 alpha to 1 and vice verse. Has anyone written anything they would share? Is there a better way than WritePixels? Thank you | 
| 
 | ||
| Just use SetAlpha before DrawImage: Strict
Import "image.png"
Import mojo
Class Program Extends App
    Field img:Image
    Field alpha:Float = 0.0
    Field switch:Bool = False
    Method OnCreate:Int()
        SetUpdateRate(60)
        img = LoadImage("image.png")
        Return 0
    End
    Method OnRender:Int()
        Cls 0,0,0
        SetBlend AlphaBlend
        SetAlpha alpha
        DrawImage(img,50,50)
        Return 0
    End
    
    Method OnUpdate:Int()
        If switch = False
            alpha += 0.01
            If alpha > 1.0
                alpha = 1.0
                switch = True
            Endif
        Else
            alpha -= 0.01
            If alpha < 0.0
                alpha = 0.0
                switch = False
            Endif
        Endif
        Return 0
    End
End
Function Main:Int()
    New Program
    Return 0
End | 
| 
 | ||
| Thank you! :) | 
| 
 | ||
| Another way Strict
Import mojo
Class Game Extends App
	Field alpha:Float
	Field image:Image
	Field dir:Float
	
	Method OnCreate:Int()
		SetUpdateRate(60)
		Self.image=LoadImage("test.png")
		
		Self.alpha=0.0
		Self.dir=0.01
		Return 0
	End Method
	
	Method OnUpdate:Int()
		Self.alpha += Self.dir
		If Self.alpha > 1.0 Or Self.alpha < 0.0 Then Self.dir = -Self.dir
		Return 0
	End Method
	
	Method OnRender:Int()
		Cls
		SetAlpha(Self.alpha)
		DrawImage(Self.image,0,0)
		Return 0
	End Method
End Class
Function Main:Int()
	New Game
	Return 0
End Function | 
| 
 | ||
| Cleaner, thank you! :) | 
| 
 | ||
| And here's a variation that will do pausing between fades. Note: Doesn't allow for different update rates. |