Functions and multiple outputs

Jimmy

Well-known member
3rd Party Module Dev
Tutorial Author
I wonder why Eachin did that though. Maybe there's internal optimization and it assumes what the target is.
Be careful with Eachin, I'm not fond of it so I might have missed some detail about it but I've looked through the code this evening and can't find what could possibly be doing that.

I just do a normal FOR NEXT loop and the problem is gone.

Jimmy

Well-known member
3rd Party Module Dev
Tutorial Author
Seems to work It might seem unusual to have data and code separate instead of methods but it will be helpful
when things grow.  Code:
Strict
Import mojo2
Const n:Int = 100
Global test:xy = New xy

Function Main:Int()
New Game()
Return 0
End

Class Game Extends App

Field canvas:Canvas
Field Obj:xy[n]

Method OnCreate:Int()
canvas = New Canvas() ; SetSwapInterval 1 ; SetUpdateRate 0
For Local i := 0 Until n ; Obj[i] = New xy ; initxy(Obj[i]) ; Next
Return 0
End

Method OnRender:Int()
canvas.Clear
For Local i := 0 Until n
canvas.DrawRect Obj[i].x,Obj[i].y,Obj[i].size,Obj[i].size    ' Draw

Obj[i] = movexy(Obj[i])                                ' Update

Next
canvas.Flush
Return 0
End

End

Class xy
Field x:Int
Field y:Int
Field vx:Int
Field vy:Int
Field size:Int
End

Function initxy:xy(p:xy)
p.size = 1 + Rnd(40) ; p.x = Rnd(640-p.size) ; p.y = Rnd(480-p.size)
p.vx = Rnd(1,4) ; If Rnd(1) >= 0.5 Then p.vx = -p.vx
p.vy = Rnd(1,4) ; If Rnd(1) >= 0.5 Then p.vy = -p.vy
Return p
End

Function movexy:xy(p:xy)
p.x = p.x + p.vx
If p.x < 0 Or p.x > (640-p.size) Then p.vx = -p.vx
p.y = p.y + p.vy
If p.y < 0 Or p.y > (480-p.size) Then p.vy = -p.vy
Return p
End

Last edited: