EventDispatcher docs

Documentation has been added for EventDispatcher in LDoc format, generated from the source file.

Here’s the basic usage:

All listeners receive the following fields in the parameter event table:

  • event.name (name of the event)
  • event.target (the listener itself)
  • event.source (the dispatcher)

Grab the latest code at Github.

Share this: Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInEmail this to someoneShare on RedditPin on PinterestShare on Tumblr

EventDispatcher update

Just a quick post that EventDispatcher has been updated with new features including new methods on(), emit(), once() that are similar to Node.js, a new method removeAllListeners(), and a debug function printListeners(). The on() and emit() methods are actually aliases to the addEventListener() and dispatchEvent() methods.

Chris Byerley (aka develephant) who developed Coronium.io and Coronium.gs is using EventDispatcher under the hood for the game server client code. Check out his projects if you’re a Lua / Corona developer.

Here’s the original post for EventDispatcher if you wish to read about it.

Grab the latest code at Github.

Share this: Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInEmail this to someoneShare on RedditPin on PinterestShare on Tumblr

ZeroBrane Studio

For Lua development, whether it is for plain Lua or platforms such as Corona SDK, ZeroBrane Studio is an excellent open-source IDE (written in Lua) for Windows, Mac OS and Linux. I still use Sublime Text with the Corona Editor plugin but ZeroBrane Studio is my first choice for Lua development.

This IDE is updated frequently and has great support from Paul Kulchenko. To grab the latest update before a new version is released, download from the github page, and follow these steps on Mac OS:

  1. Locate ZeroBraneStudio.app in the Applications folder
  2. Right-click on the app and choose Show Package Contents
  3. Open the Contents folder, and then the ZeroBraneStudio folder
  4. Drag and drop everything(*) inside the (github) ZeroBraneStudio folder into the above folder inside the app

* Actually not all files are needed, see an example of files and folders to drag over in the following image:

ZeroBraneStudio

If you have made changes to a theme or configuration (such as tomorrow.lua in the cfg folder), make sure you have a backup copy before replacing everything as described above.

Download the IDE here: http://studio.zerobrane.com

To show Corona SDK reference from right-clicking on a keyword in the editor, add the following code to the end of your user settings (user.lua) found under the Edit | Preferences menu:

Add showreference.lua from ZeroBranePackage to the ~/.zbstudio folder if it’s not there already. Quit and relaunch the IDE to see the update.

Share this: Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInEmail this to someoneShare on RedditPin on PinterestShare on Tumblr

Send custom events with EventDispatcher for Corona SDK / Lua

In Corona SDK, event listeners can only be added to the global Runtime object or to display objects. In order to broadcast messages to event listeners, the event dispatcher is limited to either one or the other. This limitation places messaging in the two scopes instead of between objects that are supposed to be talking to each other. I’ve seen many examples with display objects that are created solely for the purpose of dispatching events. This just doesn’t feel right to me; so I’m releasing my EventDispatcher, perhaps other developers may find it useful too.

Those who came from the good old Flash 5 days may remember FLEM (FLash Event Model). It was the first listener event model for Flash and ActionScript, and was created by Branden Hall and further developed and maintained by me. I’ve adapted the basic event model mechanism found in ActionScript 2/3 to Lua. This EventDispatcher has a similar interface as the listener model in Corona SDK, with some extra features thrown in (such as optional extra parameters when dispatching events, and returning status).

EventDispatcher provides custom event broadcaster/listener mechanism to regular Lua objects, it works as regular Lua 5.1 / 5.2 code, in Corona SDK, and likely other Lua-based frameworks.

[ Update: See http://swfoo.com/?p=718 ]

Basic usage:

Sample code below demonstrates how it can be used:

Here is the output from the code:

iPad is turned off by Artist2 (table)
Artist2 is resting
Rested 1
Cowboy1 is drawing a gun
Cowboy2 is drawing a gun
Artist1 is drawing a picture
iPad is turned on by Artist2 (function)
Artist2 is drawing on the iPad
Removed
iPad is turned off by Artist2 (table)
Artist2 is resting
Artist1 is resting
iPad is turned off by Artist1 (table)
Rested 2
Cowboy2 is drawing a gun and shooting a bandit
iPad is turned on by Artist2 (function)
Artist2 is drawing a bandit on the iPad
Mayor collected 42 pieces of gold from Dave

And here is the EventDispatcher module:

Update (Sept 12, 2014): Added aliases ‘on’ and ‘emit’ for ‘addEventListener’ and ‘dispatchEvent’ for Coronium GS.

EventDispatcher provides a broadcaster/listener event mechanism to regular Lua objects. Corona developers can write cleaner object-oriented messaging code that doesn’t rely on display objects or send messages from the global Runtime.

For the latest code, check out EventDispatcher at github.

Share this: Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInEmail this to someoneShare on RedditPin on PinterestShare on Tumblr

Lua table remove by key

Lua has a table.remove() function that deletes an element from a table at the specified position, or the last element by default. However, it doesn’t work when the table is an associative array (e.g. {name=”swfoo”, domain=”swfoo.com”} ).

Below is a simple function that deletes a key-value pair from a table. One important feature to note is that instead of modifying the original table, it returns a new table.

Share this: Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInEmail this to someoneShare on RedditPin on PinterestShare on Tumblr

A mighty PNG

This innocent-looking PNG image makes a huge difference to iOS apps if you need to target retina display devices. At 640×1136 and named “Default-568h@2x.png”, this default launch image tells the app to run in full retina resolution on devices such as iPhone 5. What’s different with this image is the tiny file size of only 184 bytes. You’re welcome to grab it and use it.

Default-568h@2x.png

Share this: Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInEmail this to someoneShare on RedditPin on PinterestShare on Tumblr