Write Code ... in a few lines

You would want to write code and be part of the riches available on the App Stores, specially now that there are more than just the Apple Store and the Google Play stores. The main issue remains where should I start. If you are a Mac user, then should you use xCode that comes free with the mac? Something that 99% of the mac users have tried at some point in time and have had mixed results of success.

Then come the second layer, frameworks, libraries that sit in between the native code on the mobile devices and the code that you write, offering the developer rich libraries and functions that do most of the heavy lifting. These can come at a price, after all these are written by other developers that write code like this for their living. However there are some developers that also do it from the goodness of their hearts, because they believe in giving back to the community (despite a couple of bad apples).

Today we shall focus on one of the frameworks that is surprisingly FREE of cost to use and is one that a developer can use as a beginner and then graduate to sheer raw power. We are talking about Moai and Rapanui. This is like any other frameworks that is based on a Lua shell which means it offers all of the functionality that you get with Lua, memory management, powerful and easy english like scripting, to name a few. The only catch is that you need to acknowledge that the app was made with Moai, either by incorporating a splash screen or by including it in the about box. Even if you have a windows box and Visual Studio or Visual Studio Express, you can use Moai as it works with both xCode and VS.

There was mention of Rapanui, so what is that exactly? Moai is a very powerful but low level API based development language. If you were searching an alternative to Objective-C or C++/C# then even though Moai is a Lua based framework, it can be daunting. Zipline games are aware of this and do mention that Moai is meant for seasoned developers. However the power can also be harnessed and that is exactly what Ymobe Ltd, a UK based development house has done. Rapanui is their offering to the community that provides a library wrapper over the low level API's of Moai offering functionality in a few lines of code like some other framework.

There are three games that have been bundled with the Rapanui library (I call this a library as it sits atop the Moai framework).
Here's a sampler of the Brick2D, an Arkanoid, ball breaker, etc type clone

--[[
--
-- RapaNui
--
-- by Ymobe ltd  (http://ymobe.co.uk)
--
-- LICENSE:
--
-- RapaNui uses the Common Public Attribution License Version 1.0 (CPAL) http://www.opensource.org/licenses/cpal_1.0.
-- CPAL is an Open Source Initiative approved
-- license based on the Mozilla Public License, with the added requirement that you attribute
-- Moai (http://getmoai.com/) and RapaNui in the credits of your program.
]]

BALL_START_X = 50
BALL_START_Y = 200

paddle = RNFactory.createImage("rapanui-samples/games/brick2d/paddle.png")
paddle.y = 400

ball = RNFactory.createImage("rapanui-samples/games/brick2d/ball.png")
ball.x = BALL_START_X
ball.y = BALL_START_Y

back = RNFactory.createImage("images/background-purple.png")
back:sendToBottom()

bricks = {}

bricksInGame = 0

bricksBroken = 0

label = RNFactory.createText("Bricks broken: ", { size = 10, top = 440, left = 5, width = 200, height = 50 })
score = RNFactory.createText("", { size = 10, top = 440, left = 210, width = 30, height = 50 })

function init()
    dir = round(math.random() * 1);
    ball.speed = 2;

    if (dir == 1) then
        angle = 45;
    else
        angle = 135;
    end

    ball.xspeed = ball.speed * math.cos((angle) * math.pi / 180);
    ball.yspeed = ball.speed * math.sin((angle) * math.pi / 180);
end

function initBricks()
    local padding = 10
    local color = 1

    for col = 0, 5, 1 do
        for row = 0, 4, 1 do
            local aBrick = RNFactory.createImage("rapanui-samples/games/brick2d/brick" .. color .. ".png")
            aBrick.y = 50 + padding + (row * 10 + padding * row)
            aBrick.x = 25 + padding + (col * 40 + padding * col)
            bricks[string.format("%s%s", row, col)] = aBrick
            bricksInGame = bricksInGame + 1
        end
        color = color + 1
        if (color > 3) then
            color = 1
        end
    end
end

trn = RNTransition:new()

function collideBricks()
    local padding = 10
    for col = 0, 5, 1 do
        for row = 0, 4, 1 do
            local brick = bricks[string.format("%s%s", row, col)]
            if (brick ~= nil) then
                if (ball.y + 8 >= brick.y - 5 and ball.y - 8 <= brick.y + 5) and
                        (ball.x + 8 >= brick.x - 20 and ball.x - 8 <= brick.x + 20)
                then
                    bricks[string.format("%s%s", row, col)] = nil
                    trn:run(brick, { type = "scale", xScale = -1, yScale = -1, time = 500, onComplete = function() brick:remove() end })
                    trn:run(brick, { type = "alpha", alpha = 0, time = 400 })
                    bricksInGame = bricksInGame - 1
                    bricksBroken = bricksBroken + 1
                    score:setText("" .. bricksBroken)
                    return true
                end
            end
        end
    end

    if (bricksInGame == 0) then
        initBricks()
    end
end


function collidePad()

    if (ball.y >= paddle.y - 10 and ball.y <= paddle.y) and
            (ball.x >= paddle.x - 20 and ball.x <= paddle.x + 20)
    then
        return true
    end

    return false
end

function update(enterFrame)

    ball.x = ball.x + ball.xspeed;
    ball.y = ball.y + ball.yspeed;

    if ((ball.x <= 5) or (ball.x >= 320)) then
        ball.xspeed = -ball.xspeed;
    end

    if (collideBricks()) then
        ball.yspeed = -ball.yspeed;
    end

    if ((ball.y <= 5) or (collidePad())) then
        ball.yspeed = -ball.yspeed;
    end

    if (ball.y > paddle.y + 20) then
        trn:run(ball, { type = "alpha", alpha = 0, time = 500 })
    end

    if (ball.y > 480) then
        ball:setAlpha(1)
        ball.x = BALL_START_X;
        ball.y = BALL_START_Y;


        dir = round(math.random() * 1);
        speed = 2;
        if (dir == 1) then
            angle = 45;
        else
            angle = 135;
        end

        ball.xspeed = speed * math.cos((angle) * math.pi / 180);
        ball.yspeed = speed * math.sin((angle) * math.pi / 180);
    end
end


function round(num)
    if num >= 0 then
        return math.floor(num + .5)
    else
        return math.ceil(num - .5)
    end
end

function onTouchEvent(event)

    if event.phase == "began" then
        paddle.x = event.x
    end

    if event.phase == "moved" then
        paddle.x = event.x
    end
end

init()
initBricks()

RNListeners:addEventListener("enterFrame", update)
RNListeners:addEventListener("touch", onTouchEvent)


Now that is all the code that one would have to write. Note that most of the heavy lifting is already managed by the library itself. So when you create a new Image with the RNFactory.createImage, it is this library that you are calling.

Papaya Mobile created their own python based SDK and offered it to their community and were immediately slandered for stealing another company's API. So to not get into that, there is the RNFactory namespace that hosts all the functions, which are also named differently. Though this was not done to avoid litigation, it is a good idea. Now the only problem with this approach is that if you have been a developer on a different framework and need to move to another, you are a bit stuck. This is where lua comes in pretty handy.

The best thing about lua is that you all functions are pointers and thereby you can have a particular function point to another function, if this is confusing, then try this lua code

 oldprint = print
 print = function (a)
  a = a * 3
  oldprint(a)
 end

 print(3)

you can simply use aliasing to make things easier or similar to other languages/frameworks as
 createNewImage = RNFactory.createImage

and then use the createNewImage() instead of the RNFactory.createImage, this will provide you with a bit of speed optimization and also with familiarity to other frameworks that you might have been used to working with. If it is paramount that you even have similar namespaces, then you can use something like

moai = {}
moai.createNewImage = RNFactory.createImage

local myImage = moai.createImage("imagename.png")
or
myLib = {}
myLib.createNewImage = RNFactory.createImage
local myImage = myLib.createImage("imagename.png")

you can have any namespace (that is allowed) and alias functions as required for maximum compatibility with other frameworks that you might want to work with. You can try some displayable namespaces for objects that are visible on the screen and so on. It is very easy.

The advantages that you can in the form of using Moai is Speed, it is compiled natively and provides a much faster app than some beer apps. The other advantage that you get is the sheer power, you have low level access to the Moai API and you also to the xCode/source code, so adding functionality that does not exist can be added easily. Want to add iAds or any new framework for Ads, social media, etc it is as easy as writing the code or copy pasting the code from a site/example. You do not have to wait for the framework company to make that available amongst a list of other features that you might be after. You find an error or have a better way to do things, the entire source code is available, make the relevant changes, and if you so want, share it back with the community by uploading the changes to the GitHub repository of the source code.

Rapanui offers the high level access to the same raw power offered by Moai. Lua is a non OO type scripting language but there have been many attempts to make it Object Oriented. With frameworks like Moai, Gideros these offer an OO experience and thereby, not only make things easier, modular and reusable but also help the developer build better code, where as with some other commonly used Lua based framework, the developer ends up with a spaghetti blob of code.

So if you are after writing code with fewer lines of code, want your app to run as fast as possible on the mobile devices, not be at the mercy of the framework developers who would make you beg for features and not comply, be able to easily fix bugs, extend the capabilities of the framework whilst using Lua as the language of choice then one of the options that you can choose from is Moai in conjunction with Rapanui. All of this for Free and compile for multiple platforms, desktops and mobile platforms, Androids and iOS, even Chrome Browser. Compile using xcode or visual studio and most importantly you do not have to be on-line. You can also leverage the power of the cloud (for a price/subscription).

It costs you nothing, it is better than "the beer" and it's free, so download a copy and give it a try, you might find something to use for your projects that you had to hold back due to many features not being available or high costs.

Comments

Popular Posts