如何覆盖先前创建的newText对象?
2013-8-8 1:13:18
收藏:0
阅读:137
评论:2
在这里,我创建了一个按钮,按下按钮时调用一个名为 "politeGenerator" 的函数。
local politeButton = widget.newButton
{
left = 35,
top = 335,
width = 70,
height = 70,
defaultFile = "images/politeRu.png",
overFile = "images/politeWhiteRu.png",
onPress = politeGenerator,
}
这是函数(实际上是在按钮上方编写的)。
math.randomseed(os.time())
local function politeGenerator()
local firstRandomPart = math.random(1,3)
local secondRandomPart = math.random(1,3)
local thirdRandomPart = math.random(1,3)
local firstComplPart = {"我爱你", "我需要你", "我恳求你"}
local secondComplPart = {"像疯狂一样爱你", "你的眼睛颜色", "你的嘴唇"}
local thirdComplPart = {"我希望你会成为我的!", "我在发抖!", "这是我想要的全部!"}
local politeCompliment = firstComplPart[firstRandomPart]..secondComplPart[secondRandomPart]..thirdComplPart[thirdRandomPart]
local complimentItself = display.newText(politeCompliment, 30, 150, 200, 200, "Lobster", 18)
end
好的,现在我在模拟器中看到一些随机生成的文本。但是当我再次按下按钮时,旧文本不会消失,新文本会出现在旧文本上,以此类推。但是我希望每次按下按钮时文本都被覆盖。我尝试了 event.phase == "began",我尝试了 complimentItself:removeSelf(),但都无济于事。有谁能帮忙吗?我只是不明白为什么按下按钮时变量不会被覆盖。
点赞
用户2186639
我正在使用这个函数创建和更新文本对象。很简单:
function createText( text, xPos, yPos, fontSize, color, refPoint )
local myText = display.newText( "", 0, 0, native.systemFont, fontSize )
myText.text = text
if refPoint == "CL" then
myText:setReferencePoint( display.CenterLeftReferencePoint )
elseif refPoint == "CR" then
myText:setReferencePoint( display.CenterRightReferencePoint )
elseif refPoint == "C" then
myText:setReferencePoint( display.CenterReferencePoint )
end
myText.x = xPos
myText.y = yPos
if color then myText:setTextColor( color[1], color[2], color[3] )
else myText:setTextColor(255, 255, 255) end
function myText:update( t, refPoint )
myText.text = t.text or myText.text
myText.size = t.fontSize or myText.size
if refPoint == "CL" then
myText:setReferencePoint( display.CenterLeftReferencePoint )
elseif refPoint == "CR" then
myText:setReferencePoint( display.CenterRightReferencePoint )
elseif refPoint == "C" then
myText:setReferencePoint( display.CenterReferencePoint )
end
myText.x = t.xPos or myText.x
myText.y = t.yPos or myText.y
end
return myText
end
例如:
local myText = createText( "Random text", 50, 160, 20, { 0, 0, 0 }, "C" )
myText:update( { text = "Updated random text", size = 30, xPos = 400, yPos = 300 }, "C" )
2013-07-31 10:01:20
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
你可以使用
.text来更改旧文本。我用一个定时器重新编码了你的代码,以便查看文本是否正在更改。你的代码问题在于,当你调用politeGenerator()函数时,你总是初始化一个局部变量complimentItself,它不会覆盖现有的文本,而是创建一个新文本,重叠旧文本。local firstComplPart = {"我爱", "我需要", "我乞求"} local secondComplPart = {"你像疯子一样", "你的眼睛颜色", "你的嘴唇"} local thirdComplPart = {"而且我希望你会是我的!", "我发抖了!", "这就是我想要的全部!"} local politeCompliment = firstComplPart[firstRandomPart]..secondComplPart[secondRandomPart]..thirdComplPart[thirdRandomPart] local complimentItself = display.newText(politeCompliment, 30, 150, 200, 200, "Lobster", 18) local function listener() politeCompliment = firstComplPart[math.random(1,3)]..secondComplPart[math.random(1,3)]..thirdComplPart[math.random(1,3)] complimentItself.text = politeCompliment end timer.performWithDelay( 1000, listener, 0 )