Game Development Reference
In-Depth Information
We simply create a generic function called loadImage that takes the imageName and the x,y-
coordinates and loads the image for us. If you decide to switch your code to another framework, you
will only need to address this function, and the rest of your code will work fine. Using this function,
we display our helicopter in the middle of the screen:
local heli = loadImage("_chopper.png", _W/2, _H/2) -- middle of the screen
In order to position our items, as we'll do later, we create a generic function called position and
pass it the object to position followed by the x,y-coordinates.
Here's the code for Corona SDK:
function position(theObj, x, y)
if theObj == nil then return end
theObj:setReferencePoint(display.TopLeftReferencePoint)
theObj.x = x
theObj.y = y
end
And here's the code for Gideros Studio:
function position(theObj, x, y)
if theObj ~= nil then
theObj:setPosition(x,y)
end
end
Using the Accelerometer
We can plug into the accelerometer to move the helicopter on the screen as follows.
Here's the code for Corona SDK:
local isSimulator = system.getInfo("environment") == "simulator"
local _hasAccel = not isSimulator
And here's the code for Gideros Studio:
require ("accelerometer")
local _hasAccel = accelerometer:isAvailable()
if _hasAccel then
accelerometer:start()
end
There is no function equivalent to accelerometer:start() in Corona SDK. To deal with this, we have
to set an EventListener that listens to the events; the events are then fired based on the update
frequency set.
 
Search WWH ::




Custom Search