Text 在Lua中使用表格显示文本不起作用?
我正在使用Corona SDK、Sublime Text 3和Lua。我在这个特定程序中的目标是制作一个视觉新颖的应用程序,当玩家点击屏幕底部显示的文本时,该应用程序会发生变化,从而进入下一个文本条目,从而继续故事 我为徽标、标题屏幕和其他东西设置了一系列代码,这些代码工作得非常好。我目前试图在VisualNovel场景中使用表格绘制文本,方法是更改.text属性以从表格中选择某个值,从而选择文本并使其成为新文本。基本上,有点像。。。下面是一些伪代码Text 在Lua中使用表格显示文本不起作用?,text,lua,coronasdk,lua-table,Text,Lua,Coronasdk,Lua Table,我正在使用Corona SDK、Sublime Text 3和Lua。我在这个特定程序中的目标是制作一个视觉新颖的应用程序,当玩家点击屏幕底部显示的文本时,该应用程序会发生变化,从而进入下一个文本条目,从而继续故事 我为徽标、标题屏幕和其他东西设置了一系列代码,这些代码工作得非常好。我目前试图在VisualNovel场景中使用表格绘制文本,方法是更改.text属性以从表格中选择某个值,从而选择文本并使其成为新文本。基本上,有点像。。。下面是一些伪代码 novelText = display.ne
novelText = display.newText (insert the parameters for old text and the old text)
--the variable used to call the value in the table
page = 1
dummy table Novel_pages = {
[1] = nil,
[2] = "new text"
}
(insert runtime event here that calls this function)
page = page + 1
novelText.text = Novel_pages[page]
display.newText(novelText)
这只是伪代码,所以请不要介意格式我只想展示我如何尝试从表中调用这些值,并展示我在做什么,而不必让人们查看我的所有代码
所以,在Corona SDK模拟器中,一切都很正常,文本甚至只是瞬间变化——直到一秒钟后,我收到一条消息,内容如下
"mainl.lua:160: bad argument #1 to 'newText' (string expected, got table)
stack traceback:
[C]: in function 'NewText'
main.lua:160: in function <main.lua: 156>
?: in function <?169>"
现在是我给你所有代码的部分!我希望这不是太多,并且我已经详细说明了这个问题。我看不出在表中哪里出错,因为它应该只是替换.text而不是所有其他属性?然后用新的文本属性显示,之后根本不必引用表?在显示视觉小说文本之前,可能需要程序处理.text更改存在问题
不管怎样,请帮帮我!如果我知道这里出了什么问题,或者有人向我提出另一种选择,我将不胜感激!非常感谢你:
下面是代码-一切都从函数sceneVN开始!请原谅我的尴尬对话。c:这是一个练习项目
local store = require( "plugin.google.iap.v3" )
local composer = require("composer")
local scene = composer.newScene()
display.setStatusBar( display.HiddenStatusBar ) -- Removes status bar
coins = 5 -- variable that defines the number of coins player has in the game.It will be different
--in a stand alone game, but as a small app meant to demonstrate function, it's necessary to start off
--with a small, defined number in the beginning.
local logo = display.newImage("/Images/logo.png", 155, 275) --code for my personal logo, drawn by me.
--Not actually showcased in the video because of time limit.
logo.alpha = 0
local function makeTitleTrue()
logo:removeSelf()
print("menu should be TRUE")
titleScreen()
end
local function fadeOut()
transition.to(logo, {time = 1000, alpha = 0, onComplete = makeTitleTrue})
end
transition.to(logo, {time = 1000, alpha = 1, onComplete = fadeOut}) -- end of logo code
function titleScreen() -- beginning of title code, which is not managed as a separate scene
title = true
titleImg = display.newImage("/Images/vn_bg.png", 155, 275)
--titleWords = display.newImage("/Images/TitleWords.png", 155, 275)
--fix to flow towards the upper right corner.
local flare = display.newImage("/Images/flare2.png", 40, 30)
flare.xScale = .5
flare.yScale = .5
local flare2 = display.newImage("/Images/flare2.png", 400, 70)
flare2.xScale = .6
flare2.yScale = .6
local flare3 = display.newImage("/Images/flare2.png", -30, 100)
flare3.xScale = .4
flare3.yScale = .4
local flare4 = display.newImage("/Images/flare2.png", 100, 400)
flare4.xScale = .4
flare4.yScale = .4
local flare5 = display.newImage("/Images/flare2.png", 400, 400)
flare5.xScale = .3
flare5.yScale = .3
local flare6 = display.newImage("/Images/flare2.png", 250, 200)
flare6.xScale = .3
flare6.yScale = .3
local function moveFlare1()
transition.to(flare, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare1})
end
local function moveFlare2()
transition.to(flare2, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare2})
end
local function moveFlare3()
transition.to(flare3, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare3})
end
local function moveFlare4()
transition.to(flare4, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare4})
end
local function moveFlare5()
transition.to(flare5, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare5})
end
local function moveFlare6()
transition.to(flare6, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare6})
end
transition.to(flare, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare1})
transition.to(flare2, {time=2500, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare2})
transition.to(flare3, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare3})
transition.to(flare4, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare4})
transition.to(flare5, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare5})
transition.to(flare6, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare6})
--add options that can when the screen is tapped, tap on an option twice to select
-- start story
-- continue story
-- coin gambling
-- end game
if (title == true) then
Runtime:addEventListener("tap", sceneVN)
end
end
function forceQuit()
function quit()
os.exit()
end
timer.performWithDelay(1000,quit)
end
function sceneVNChapter2()
return
end
function sceneVN() -- the actual visual novel code itself
display.remove(titleImg)
--display.remove(titleWords)
display.remove(flare)
display.remove(flare2)
display.remove(flare3)
display.remove(flare4)
display.remove(flare5)
display.remove(flare6)
title = false
local coinSheetData =
{
width = 32,
height = 32,
numFrames = 8,
}
local coinimageSheet = graphics.newImageSheet( "/Images/spinning_coin.png", coinSheetData )
local sequenceData =
{
name= "spinning_coin",
start = 1,
count = 8,
time = 1000,
loopCount = 0
}
--the properties of the name plate that can be changed ingame by using ".text" property
local nameOptions =
{
text = "Frankenstein",
x = 165,
y = 450,
width = 310,
font = "Charlesworth.ttf",
fontSize = 22,
align = "left"
}
local bg = display.newImage("/Images/bg4.jpg", 155, 275)
textRect = display.newRect(155, 525, 325, 200)
textRect:setFillColor(.02, .02, .02)
textRect.alpha = .6
page = 1
local frames = display.newImage("/Images/windowframes_gold.png", 155, 275)
display.newText(nameOptions)
local VN_pages = {
[1] = nil,
[2] = "\"Then, seeing as this is a simulation of\n a visual novel dating sim, I have no\n choice but to ask you...\"",
[3] = "\"My lady, would you go on a date with me?\nFrankenstein... butler of the fineest noble,\nCadis Etrama di Raizel?\"",
[4] = "duck",
[5] = "duck",
[6] = "duck",
}
local displayNovelText = display.newText("\"I see. So I\'m supposed to pretend I am\na character in a multi-chapter phone\napp that you\'ve been reading...\"", 165, 500, "Goudy Old Style Regular.ttf", 17)
function changePage()
print("dang it")
page = page + 1
displayNovelText.text = VN_pages[page]
display.newText(displayNovelText)
end
textRect:addEventListener("tap", changePage)
if (coins < 10) then
coinsDigits = 2
else
if (coins > 9) and (coins < 100) then
coinsDigits = 3
else
if (coins > 99) and (coins < 1000) then
coinsDigits = 4
else
if (coins > 999) and (coins < 10000) then
coinsDigits = 5
else
if (coins > 9999) and (coins < 100000) then
coinsDigits = 6
end
end
end
end
end
cooin = display.newSprite(coinimageSheet, sequenceData)
cooin.x = 25
cooin.y = 30
cooin:play()
coinText = display.newText("1", 57 + 4 * coinsDigits, 32, "VCR_OSD_MONO_1.001.ttf", 25)
coinText.text = coins
coinTimer = timer.performWithDelay(2000, cooin, 1)
end
function choiceMade( event ) --the scenes where all the choices are made
if (event.action == "clicked") then
local i = event.index
if (i == 1) then
Runtime:removeEventListener()
titleScreen()
else
if (i == 2) then
system.openURL( "https://www.paypal.com/us/home" )
else
if (i == 3) then
return
end
end
end
end
end -- end of choice scenes
function Outofcoins()
--native alert lack of zero coins
local alertMessage = "Uh oh, looks like you've run out of coins! To continue reading the story, would you like to buy or gameble for coins?"
native.showAlert( "Out of coins!", alertMessage, {"Gamble for coins", "Purchase coins", "Exit to Menu"}, choiceMade)
end
if (coins == 0) then -- conditional branch that alerts Outofcoins if no coins left
Outofcoins()
end
function sceneGambleStart()
function earntCoins()
numberEarnt = 0
local coinsGot = display.newImage("/Images/coins_gold.png", 155, 275)
coinsGot.alpha = 0
local function fadeOutCoinsEart()
transition.to(logo, {time = 2000, alpha = 0})
display.remove(coinsGot)
end
local transitionFade = transition.to(logo, {time = 2000, alpha = 1, onComplete = fadeOutCoinsEarnt})
timer.performWithDelay(2000, transitionFade, 1)
coinText.text = coins + numberEarnt
end
local function gamblerIntro()
nameOptions.text = "Gambler"
local bg = display.newImage("/Images/bg4.jpg", 155, 275)
textRect = display.newRect(155, 525, 325, 200)
textRect:setFillColor(.02, .02, .02)
textRect.alpha = .6
local frames = display.newImage("/Images/windowframes_gold.png", 155, 275)
display.newText(nameOptions)
if (gambleVisit == false) then
display.newText("\"Welcome to the coin gambling shop!\nHere's your chance to earn free coins\nwithout having to use the app store!", 165, 500, "Goudy Old Style Regular.ttf", 17)
--display.newText("\"You can play here once a day if you\'ve\nNO coins in your inventory! You are\ngiven three tries at any game each visit.", 165, 500, "Goudy Old Style Regular.ttf", 17)
--display.newText("\"So, then! What games will you play\nin our shop today? \n \n", 165, 500, "Goudy Old Style Regular.ttf", 17)
else
display.newText("\"Welcome back, player! You have\nthree tries left. So, what games\nwill you try your hand at?", 165, 500, "Goudy Old Style Regular.ttf", 17)
end
end
local function sceneDiceRoll()
--local show background graphics
--draw dice on screen, with function ready to shake/transition on screen when accelerometer
--transition hands up and drop dice animation
-- if dice = # chosen then give coins
end
local function sceneCardChoose()
-- a function that defines the mechanics of the card game.
--draw several options on the screen:
--3 cards, earn 5 coins.
--6 cards, earn 15 coins.
--9 cards, earn 30 coins.
--The player needs 5 coins to read another chapter, but by increasing card numbers,
--depending on the player's choice, show the images of the cards (with whatever numbers, always set)
-- on the screen and allow the player to choose a card. Make a shuffling animation.
-- lay all the cards on the screen, now with randomised positions defined by a number.
--the player may choose one. Event listener, if the number defined = card number, the card flips,
--shows its number, and the player wins the coins. defer to earntCoins.
--if the player chooses the wrong card, show him a "WRONG CARD" result, and ask if he would like another
--round or to exit to the main shop.
return
end
local function sceneGuessNumber()
--this game is not created, but is a dummy function that's shown here. It's included in the
--options to show that if this were a real game, that's what it would look like
return
end
end
错误消息显示您所需的全部信息: mainl.lua:160:应为'newText'字符串提供错误的参数1,已获取 桌子 如果转到代码的第160行,您将发现以下内容:
display.newText(displayNovelText)
上面的几行可以执行此操作:
local displayNovelText = display.newText("sometext")
请参阅display.newText文档,了解如何正确使用此功能
您将看到display.newText不返回字符串,而是返回文本对象。
它也不接受文本对象作为输入。这就是错误消息告诉您的
要访问displayNovelText的文本,您必须编写displayNovelText.text,您可以将其填充到display.newText以及其他选项中
请确保您始终阅读所使用函数的文档。如果你不知道你在处理什么,你就不可能成功。错误消息说明了你所需要的一切: mainl.lua:160:应为'newText'字符串提供错误的参数1,已获取 桌子 如果转到代码的第160行,您将发现以下内容:
display.newText(displayNovelText)
上面的几行可以执行此操作:
local displayNovelText = display.newText("sometext")
请参阅display.newText文档,了解如何正确使用此功能
您将看到display.newText不返回字符串,而是返回文本对象。
它也不接受文本对象作为输入。这就是错误消息告诉您的
要访问displayNovelText的文本,您必须编写displayNovelText.text,您可以将其填充到display.newText以及其他选项中
请确保您始终阅读所使用函数的文档。如果你不知道你在处理什么,你就不可能成功。嗨,小猪,谢谢你提供了到文档的链接!你说得对,它很有用。我过去只使用谷歌论坛的答案,因为文档的语言对我来说很混乱,课堂上的老师不喜欢文档;我的大部分学习都是实验性的或是简单的powerpoints。我想我已经提出了在铭牌选项中使用.text属性更改的问题,然后错误地认为我可以在不创建此类选项的情况下对普通文本执行相同的操作。你的回答很有帮助,现在我也有了很好的资源。非常感谢!学习如何阅读文档并习惯它是软件开发中最重要的事情。我不知道科罗纳,我从来没有用过它。。。但是我知道如何快速找到这样的信息,因为它是存在的,这并不总是caseHi,Pieget,感谢您提供了到文档的伟大链接!你说得对,它很有用。我过去只使用谷歌论坛的答案,因为文档的语言对我来说很混乱,课堂上的老师不喜欢文档;我的大部分学习都是实验性的或是简单的powerpoints。我想我已经提出了在铭牌选项中使用.text属性更改的问题,然后错误地认为我可以在不创建此类选项的情况下对普通文本执行相同的操作。你的回答很有帮助,现在我也有了很好的资源。非常感谢!学习如何阅读文档并习惯它是软件开发中最重要的事情 发展。我不知道科罗纳,我从来没有用过它。。。但我知道如何快速找到这些信息,因为它确实存在,但情况并非总是如此