- Joined
- Jan 2, 2020
- Messages
- 1,414
Here is a simple example on how to create a 2D sprites in runtime, using what I hope is a very familiar example.
Code:
' UP, UP, AND AWAY
Import mojo2
Function Main()
New MyApp
End
Class MyApp Extends App
Field canvas : Canvas
Field spritecanvas:Canvas
Field sprite:Image
Field x:Int, y:Int
Field def:String
Method OnCreate()
canvas = New Canvas ; SetUpdateRate 0
sprite = New Image(24,21,0,0,0) ' Enable sprite
spritecanvas = New Canvas(sprite)
createSprite(spritecanvas)
End
Method OnRender()
canvas.Clear 0,136/255.0,1
canvas.DrawImage sprite,x,y
x = x + 1 ; y = y + 1
If x >= 200 Then x = 0
If y >= 200 Then y = 0
canvas.Flush
End
Method createSprite(cp:Canvas)
cp.Clear 0,0,0,0
cp.SetColor 1,1,1
def = " ******* "
def = def + " *********** "
def = def + " ************* "
def = def + " ***** ****** "
def = def + " ***** ** ***** "
def = def + " ***** ********* "
def = def + " ***** ** ***** "
def = def + " ***** ****** "
def = def + " ************* "
def = def + " ************* "
def = def + " * ********* * "
def = def + " * ******* * "
def = def + " * ***** * "
def = def + " * *** * "
def = def + " * *** * "
def = def + " * * * "
def = def + " * * * "
def = def + " ***** "
def = def + " ***** "
def = def + " ***** "
def = def + " *** "
Local tempx:Int = 0, tempy:Int = 0
For Local temp:Int = 1 To Len(def)
If Mid(def,temp,1) = "*" Then cp.DrawRect tempx,tempy,1,1
tempx = tempx + 1 ; If tempx >= 24 Then tempx=tempx-24 ; tempy = tempy + 1
Next
cp.Flush
End
Function Len:Int(s:String)
Return s.Length
End
Function Mid:String(s:String,p:Int,n:Int)
p=p-1 ; Return s[(p)..(p+n)]
End
End