Okay, I’ve been struggling with what I’m going to bet is a relatively easy/common problem – I’ve searched the forums and docs but can’t seem to find the answer that fixes my issue.
I’m building a business app that will eventually be (hopefully) opening newWebPopups for each of the links to externally hosted pdf documents I’m linking to. There are many of these, broken down by categories of products. Each product has at least 4 pdf documents I’ll be linking to, and there are 70+ products.
At this point, what I think I would like to achieve is to have an external module with a function I can call in each product scene so that I only have to set a local variable (linkA, linkB, linkC, etc.) with the correct parameters (title, name, url), and add it to the sceneGroup correctly, so that they will be removed upon transitioning back to the parent scene, or any other scene. If this is perhaps not the most awesome way to go about doing this, any advice would be welcome, as well.
Here’s what I’ve got:
links.lua
[lua]
local link = {}
local link_mt = { __index = link } – metatable
– PRIVATE FUNCTIONS
– put them here if you can think of them
– PUBLIC FUNCTIONS
function link.new( title, name, url ) – constructor (these are the variables to use when creating a new link)
local newLink = {
title = title,
name = name,
target = url
}
local mainFont = “Gotham-Black.ttf”
local otherFont = “Gotham-Book.ttf”
function link:createNew()
local text = display.newText( self.title, display.contentCenterX, display.contentCenterY, otherFont, 16 )
end
function link:makeTouchable()
newLink:addEventListener(“touch”, linkTouch) --<— to do: make this work (to call newWebPopup)
end
return setmetatable( newLink, link_mt )
end
function link:printInfo()
print( self.name … " links to " … self.target )
print( self.name … " will be displayed in text as " … self.title )
end
return link
[/lua]
And here’s the only way I’ve been able to make it appear in the product module thus far:
fss45dc.lua
[lua]
local utils = require( “utils” )
local ff = require( “func” )
local link = require( “links” )
local widget = require( “widget” )
local composer = require( “composer” )
local scene = composer.newScene()
composer.setVariable( “parentScene”, “ic”)
function scene:create( event )
local sceneGroup = self.view
– create background group
local bg = utils.newBgGroup( 50, 50 )
local vfLogo = utils.newLogo( 50, 50 )
local goArrow = utils.newGoArrow( 50, 50 )
– create stuff here
local docGroup = display.newGroup()
– create some text
local mainFont = “Gotham-Black.ttf”
local otherFont = “Gotham-Book.ttf”
local title = display.newText( “FSS 45DC™ Documents”, display.contentCenterX, 110, mainFont, 24 )
local link1 = link.new( “FSS 45DC™ Datasheet”, “tdsUrl”, “http://versaflex.com/f/ds-VFfss45dc.pdf”)
link1:createNew() --<— this isn’t a part of the sceneGroup, so can’t be destroyed :\
link1:printInfo()
– create groups & other stuff here
local docGroup = display.newGroup()
docGroup.x = contentCenterX
docGroup.y = contentCenterY
docGroup:insert( title )
– Add stuff to scene here
sceneGroup:insert( bg )
sceneGroup:insert( vfLogo )
sceneGroup:insert( goArrow )
sceneGroup:insert( docGroup )
–sceneGroup:insert( link1 ) <----- this throws an error
– Add Event Listeners here
vfLogo:addEventListener(“touch”, logoTouch)
goArrow:addEventListener(“touch”, goBack)
end
– rest of scene:functions / addEventListeners removed for brevity
[/lua]
So, obviously I’m missing something critical here ( *cough* knowledge ), and so I’m wondering how can I achieve the goal of getting this working smoothly and efficiently?
Thanks for any advice.
I don’t think any of the metatable stuff is needed in your case.