Components
Components
For instance components/pageX/page/swip.lua enables a page transition, and components/pageX/audios folder can have multiple audio lua files for instance short/audioOne.lua, long/audioTwo.lua. short audio file is loaded in to memory with audio.loadSound(), and long audio file is streaming with audio.loadStream().
local sceneName = ...
--
local scene = require('controller.scene').new(sceneName, {
name = "pageX",
components = {
layers = {
{ bg={} },
},
audios = { short = {"audioOne"}, long= {"audioTwo" },
groups = { },
timers = { },
variables = { },
page = { "controllers.swipe" }
},
commands = { },
onInit = function(scene) print("onInit") end
})
--
return scene
page
- common
- controllers
- swipe
- keyboard
audio
group
timer
Common components
You can add a common component to components/common directory

And let it pass in arguments of bootstrap function in main.lua
local common = {commands = {"myEvent"}, components = {"myComponent"}}
require("controller.index").bootstrap({
name="book", sceneIndex = 1, position = {x=0, y=0},
common =common
})Common components are executed after all the layer components of a scene are rendered. So you can access a layer component by UI.layers table.
for instance, the following myComponent.lua attaches a tap listener
local M = {}
local function listener(event)
print("bg is tapped")
end
function M:init(UI)
end
--
function M:create(UI)
end
function _M:didShow(UI)
local sceneGroup = UI.scene.view
UI.layers.bg:addEventListener("tap", listener)
end
function M:didHide(UI)
local sceneGroup = UI.scene.view
UI.layers.bg:removeEventListener("tap", listener)
end
--
function M:destroy()
end
--
return Mcomponent/layers lua
you can copy a lua file of layer - App/{book}/components/{page}/layers/{layer}.lua to the custom/components.
For instance, App/snowMan/components/page1/layers/ground.lua to be copied to custom/components folder
y9u can specify it in main.lua
...
...
local common = {commands = {"myEvent"}, components = {"ground"}}
...
...then set isSharedAsset property true in in custom/components/hat.lua. then hat will be called envery page in snowMan book. custom components are called after the calls to each layer module, so the hat will be front.
...
...
local layerProps = {
...
...
-- Set isSharedAsset = true, and then set a common module. See kwikTheCatCommon.lua
--
M.isSharedAsset = true
M.imagePath = "page1/hat.png"
function M:init(UI)
--local sceneGroup = UI.scene.view
if not self.isSharedAsset then
self.imagePath = UI.page ..self.imageName
end
end
--
function M:create(UI)
if not self.isSharedAsset then
self.imagePath = UI.page ..self.imageName
end
local obj = self:createImage(UI)
UI.layers[#UI.layers] = obj
self.obj = obj
end
...
...