Function to read value from table or text box

Hey there, 

So I’m trying to get a basic version of autocomplete working, but I’m falling down trying to get a function to read the table…

I’ve got a couple of letters that when tapped display into a text box (via a laborious function), and I want to make it so that when the text box reads a certain value then an image will appear… 

For example, “AABB” will show image “nicoll”…

My code is below… I’ve tried making it a clickable button, but it’s still nothing appearing so far… even a ‘print’ value isn’t showing that the value is there… similarly, I’ve tried asking it to check the newText without success… 

local function stationShow( event ) if (theWordText== "AABB") then nicoll.isVisible = true print( "Nicoll show" )

Any help would be greatly appreciated… 

local composer = require( "composer" ) local scene = composer.newScene() local widget = require ( "widget" ) local chosenLetters = {} theWord = '' theWordText = "" function scene:create( event ) local sceneGroup = self.view letterGroup = display.newGroup() sceneGroup:insert( letterGroup ) stationGroup = display.newGroup() sceneGroup:insert( stationGroup ) local letterA = display.newImageRect ( letterGroup, "images/anton\_54.png", 200, 200) letterA.x = 200 letterA.y = 200 letterA.id = 1 local letterB = display.newImageRect ( letterGroup, "images/anton\_61.png", 200, 200) letterB.x = 400 letterB.y = 200 letterB.id = 2 local letterC = display.newImageRect ( letterGroup, "images/anton\_28.png", 200, 200) letterC.x = 600 letterC.y = 200 local theWordText = display.newText("",500,437,native.systemFontBold,50) theWordText:setTextColor( 1, 1, 1 ) local function formStringA( event ) if ( event.target.id == 1 ) then local theLetter = "A" print( "letterID: " .. event.target.id ) table.insert(chosenLetters,theLetter) theWord = theWord .. theLetter theWordText.text = theWord theWordText.x = display.contentWidth/2 elseif ( event.target.id == 2 ) then local theLetter = "B" print( "letterID: " .. event.target.id ) table.insert(chosenLetters,theLetter) theWord = theWord .. theLetter theWordText.text = theWord theWordText.x = display.contentWidth/2 end return true end local function reset( event ) if ( event.phase == "began" ) then theWord = '' theWordText.text = "" elseif ( event.phase == "ended" ) then print( "Letters reset" ) end return true end local nicoll = display.newImageRect ( stationGroup, "images/nicoll.png", 200, 200) nicoll.x = 200 nicoll.y = 600 nicoll.isVisible = false local function stationShow( event ) if (chosenLetters == "AABB") then nicoll.isVisible = true print( "Nicoll show" ) return true end end stationShow() letterA:addEventListener( "tap",formStringA ) letterB:addEventListener( "tap",formStringA ) letterC:addEventListener( "touch", reset) end

I have fixed up your demo. First, using composer is trickier than I would like. You want to separate from your main, imo.

Second, you are comparing a table of letters (chosenLetters) to a string. This does not work. You need to use table.concat(chosenLetters) == “AABB”. I didn’t have your graphics, so I made some of my own. GL

Absolute legend, cheers for that…

sorry, I should’ve clarified it wasn’t in the main.lua but a sub-page… I haven’t got into the habit of using “scenes.item” yet… 

I’ve got a couple of questions though if I may…

- using composer is trickier than I would like

What issues did you run up against in composer?.. I’m admit that I’m pretty new to this, and I’m a bit confused as to this…

from main.lua

_G.s = require( “composer” )

s.stage:toFront()

s.gotoScene( “scenes.home” )

from home.lua

local scene = s.newScene()

I’m confused as to the use of “.s” … Is this creating a stage instead of a scene?.. what’s the difference here?.. I can see from the main.lua you’ve provided that _G.s still requires composer… 

I’m curious as to how best to integrate this into the rest of the application…

thanks again… 

> What issues did you run up against in composer?

 

Using Corona’s Composer requires going to a composer scene.

Then there’s subtle things that can go wrong with using overlays and scene transitions and masking.

For your case, it’s very straightforward.

 

 

When Lua looks for a variable, it defaults to looking in the Global namespace (which is _G for CoronaSDK)

_G.s = require( “composer” )

This puts the composer include in the global namespace, since you basically would be including it in every page anyway.

After setting it…

s.newScene() – this is the same as composer.newScene()

s.gotoScene( “scenes.home” ) – this is the same as composer.gotoScene(…)

 

s.stage:toFront() – this isn’t necessary for your use case

 

https://docs.coronalabs.com/api/library/composer/index.html

cheers man, pretty sure I’ve got my head around it now…

thanks again for your help… 

I have fixed up your demo. First, using composer is trickier than I would like. You want to separate from your main, imo.

Second, you are comparing a table of letters (chosenLetters) to a string. This does not work. You need to use table.concat(chosenLetters) == “AABB”. I didn’t have your graphics, so I made some of my own. GL

Absolute legend, cheers for that…

sorry, I should’ve clarified it wasn’t in the main.lua but a sub-page… I haven’t got into the habit of using “scenes.item” yet… 

I’ve got a couple of questions though if I may…

- using composer is trickier than I would like

What issues did you run up against in composer?.. I’m admit that I’m pretty new to this, and I’m a bit confused as to this…

from main.lua

_G.s = require( “composer” )

s.stage:toFront()

s.gotoScene( “scenes.home” )

from home.lua

local scene = s.newScene()

I’m confused as to the use of “.s” … Is this creating a stage instead of a scene?.. what’s the difference here?.. I can see from the main.lua you’ve provided that _G.s still requires composer… 

I’m curious as to how best to integrate this into the rest of the application…

thanks again… 

> What issues did you run up against in composer?

 

Using Corona’s Composer requires going to a composer scene.

Then there’s subtle things that can go wrong with using overlays and scene transitions and masking.

For your case, it’s very straightforward.

 

 

When Lua looks for a variable, it defaults to looking in the Global namespace (which is _G for CoronaSDK)

_G.s = require( “composer” )

This puts the composer include in the global namespace, since you basically would be including it in every page anyway.

After setting it…

s.newScene() – this is the same as composer.newScene()

s.gotoScene( “scenes.home” ) – this is the same as composer.gotoScene(…)

 

s.stage:toFront() – this isn’t necessary for your use case

 

https://docs.coronalabs.com/api/library/composer/index.html

cheers man, pretty sure I’ve got my head around it now…

thanks again for your help…