Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Functional reactive programming and LuaGravity — Gideros Forum

Functional reactive programming and LuaGravity

MagnusviriMagnusviri Member
edited March 2012 in General questions
@ techdojo mentioned component game design in this thread

http://www.giderosmobile.com/forum/discussion/comment/3846#Comment_3846

And it has pretty well sucked me in and I've eventually landed on LauGravity.

http://www.lua.inf.puc-rio.br/~francisco/luagravity/

Has anyone used it?

Comments

  • Ok, so LuaGravity seems to be more of a concept than something that is actually used.

    Anyway, I liked some of the ideas and they can sort of be implemented with coroutines anyway, so I rewrote the HelloBall example using coroutines. It's about 5 lines shorter and much more interesting to read in my opinion. The if's are gone and replaced with whiles. I could have left in the local x and y variables but decided it was safer to do it this way (if there were other ways of setting the ball position).

    The original HelloBall is here:

    http://www.giderosmobile.com/documentation/getting_started.html
     
    local background = Bitmap.new(Texture.new("field.png"))
    stage:addChild(background)
     
    local fruit = Bitmap.new(Texture.new("ball.png"))
     
    fruit.xspeed = 1.5
    fruit.yspeed = 2.3
     
    stage:addChild(fruit)
     
    function moveBallX()
    	while true do
    		while fruit:getX() < 320 -fruit:getWidth() do
    			fruit:setX( fruit:getX() + fruit.xspeed )
    			coroutine.yield()
    		end
    		while fruit:getX() > 0 do
    			fruit:setX( fruit:getX() - fruit.xspeed )
    			coroutine.yield()
    		end
    	end
    end
     
    function moveBallY()
    	while true do
    		while fruit:getY() < 480 -fruit:getHeight() do
    			fruit:setY( fruit:getY() + fruit.yspeed )
    			coroutine.yield()
    		end
    		while fruit:getY() > 0 do
    			fruit:setY( fruit:getY() - fruit.yspeed )
    			coroutine.yield()
    		end
    	end
    end
     
    co_moveBallX = coroutine.create(moveBallX)
    co_moveBallY = coroutine.create(moveBallY)
     
    function onEnterFrame(event)
    	coroutine.resume(co_moveBallX)
    	coroutine.resume(co_moveBallY)
    end
     
    stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)

    Likes: ndoss

    +1 -1 (+1 / -0 )Share on Facebook
  • Ooh, neat! I read about lua gravity after reading your first post which then led me to coroutines which then led me nowhere .... I couldn't exactly see how to use them in Gideros.

    This example helps greatly! Thanks!
  • Oh I'm wrong, it's 6 lines longer. And I think how I'm controlling one variable is kind of inefficient. But I can see this working to control an AI or game timeline really well.
Sign In or Register to comment.