简体   繁体   中英

string passed as an argument turns into a table. lua

I have a function that whenever I pass a string into, it turns into a table and I don't know why. Any help is very appreciated. Here's my code:

function gotoSkip(para)
    print(para)
    print(type(para))
    --scene:removeEventListener( "create", scene )
    --composer.gotoScene(para)
end

function scene:create( event )
    local sceneGroup = self.view
    background = display.newImageRect(sceneGroup,"images/white.jpg",768,1500)
    background.x = display.contentCenterX
    background.y = display.contentCenterY
    
    local skipButton = display.newText(sceneGroup, "Skip", 600, 1300, nativeSystemFont, 60)
    skipButton:setFillColor(0,0,256)
    skipButton:addEventListener("tap",gotoSkip, "StringExample")
end

Here is the result of the two print statements respectively:

table: 0D27FB88 (or some other memory address)

table

addEventListener function only gets 2 arguments, so you should remove that third argument.

skipButton:addEventListener("tap", gotoSkip)

If you wanna pass parameter to a function from an Event Listener , there are 2 ways;

1-) You can set a property for event.target;

function gotoSkip(event)
    print( event.target.para)
    print( type(event.target.para) )
end

local skipButton = display.newText("Skip", 400, 400, nativeSystemFont, 60)
skipButton.para= "StringExample"
skipButton:addEventListener("tap", gotoSkip)

2-) You can use a sub function;

function gotoSkip(para)
    print( para )
    print( type(para) )
end

local skipButton = display.newText("Skip", 400, 400, nativeSystemFont, 60)

function subFunc()
    gotoSkip("StringExample")
end

skipButton:addEventListener("tap", subFunc)

You can remove its listener like this;

skipButton:removeEventListener("tap", subFunc)

Also nativeSystemFont isn't a valid constant unless you defined it. Correct one is native.systemFont .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM