Gideros live coding: a simple example

Some of the users of ZeroBrane Studio had troubles running the live coding demo that Andy Bower put together, so I decided to post a very simple example that can be used as a starting point. I also packaged these files as a ZeroBraneEduPack project, so you can browse and download all these files together (they will also be packaged with the next release of ZeroBrane Studio).

To run this example you only need three files:

<project>
 <properties scaleMode="0" logicalHeight="480" logicalWidth="320" orientation="0">
  <imageScales/>
 </properties>
 <file file="zbstudio.png"/>
 <file file="main.lua"/>
</project>
LiveCodeSimple = LiveCodeSimple or Core.class(Sprite)

function rgb(r, g, b) return ((r*256)+g)*256+b end

function LiveCodeSimple:onEnterFrame() end
function LiveCodeSimple:init(x, y)
  application:setBackgroundColor(rgb(220, 220, 255))
  local image=Bitmap.new(Texture.new("zbstudio.png"))
  image:setScale(1.0)
  image:setAnchorPoint(0.5, 0.5)
  image:setPosition(x, y)
  image:setRotation(45)
  self.image=image

  self:addChild(image)
  stage:addChild(self)
end

function Application:clearStage()
  while stage:getNumChildren()>0 do
    stage:removeChildAt(1)
  end
end

application:clearStage()
LiveCodeSimple.new(150, 400)
LiveCodeSimple.new(200, 300)

if initialized then return end
initialized = true
require("mobdebug").start()
stage:addEventListener(Event.ENTER_FRAME,
  function(...) pcall(LiveCodeSimple.onEnterFrame, ...) end)

When you open main.lua, set Gideros as the interpreter, and enable live coding for this script (using Project | Run as Scratchpad), you can change any values (for example, change 45 to -45 in setRotation(45)) or update the code and should see the immediate effect of your changes. You may check this post for details on how to setup Gideros with ZeroBrane Studio.

The script looks like a "normal" Gideros script, but includes a check for a global variable initialized to avoid adding Gideros events listeners multiple times: if initialized then return end.

You need to wrap onEnterFrame code into an anonymous functions to allow LiveCodeSimple:onEnterFrame method to be replaced if needed and use pcall to avoid aborting the Gideros Player on run-time errors inside onEnterFrame. You may read this and this discussion on Gideros forums for implementation details and background information.

You should get a copy of my slick ZeroBrane Studio IDE and follow me on twitter here.

1 Comment

thank you! the live coding successfully runs now. i can set it up in less than a minute

Leave a comment

what will you say?
(required)
(required)

About

I am Paul Kulchenko.
I live in Kirkland, WA with my wife and three kids.
I do consulting as a software developer.
I study robotics and artificial intelligence.
I write books and open-source software.
I teach introductory computer science.
I develop a slick Lua IDE and debugger.

Recommended

Close