根据随机值改变精灵帧
changing sprite frame depending on random value
我有一个按钮列表(当前是图像),我想根据从列表中获取的值更改图像颜色
buttons = {"btn1","btn2","btn3","btn4"}
local buttonSheetData = {
width = 150,
height = 150,
numFrames = 2,
sheetContentWidth = 300,
sheetcontentheight = 150,
}
local buttonSheet = graphics.newImageSheet("image/buttonSS.png", buttonSheetData)
local sequenceData = {
{name = "black", start = 1, count = 1},
{name = "red", start = 2, count = 1}
}
local btn1 = display.newSprite(buttonSheet, sequenceData)
btn1.x = 100
btn1.y = 90
local btn2 = display.newSprite(buttonSheet, sequenceData)
btn2.x = 200
btn2.y = 230
local btn3 = display.newSprite(buttonSheet, sequenceData)
btn3.x = 300
btn3.y = 90
local btn4 = display.newSprite(buttonSheet, sequenceData)
btn4.x = 400
btn4.y = 230
x = buttons[math.random(#buttons)]
x:setFrame(2)
圆圈目前是黑色的。每次我 运行 我希望它从列表中获取一个随机值并将颜色更改为红色的代码。所以当我 运行 代码
时有一个不同的红色圆圈
我一直收到错误消息:
"Attemp to call method 'setSequence' (a nill value)"
您的代码将字符串和变量名视为可以互换。第一行:
buttons = {"btn1","btn2","btn3","btn4"}
创建一个 table 字符串,所以行:
x = buttons[math.random(#buttons)]
会将 x
设置为 buttons
的随机条目,它们是字符串,因此下一行 (x:setFrame(2)
) 正在调用字符串中不存在的方法。
相反,创建一个 table 按钮:
buttons = {btn1,btn2,btn3,btn4} -- creates a table of buttons
x = buttons[math.random(#buttons)] -- x is a random entry of buttons (a button)
我有一个按钮列表(当前是图像),我想根据从列表中获取的值更改图像颜色
buttons = {"btn1","btn2","btn3","btn4"}
local buttonSheetData = {
width = 150,
height = 150,
numFrames = 2,
sheetContentWidth = 300,
sheetcontentheight = 150,
}
local buttonSheet = graphics.newImageSheet("image/buttonSS.png", buttonSheetData)
local sequenceData = {
{name = "black", start = 1, count = 1},
{name = "red", start = 2, count = 1}
}
local btn1 = display.newSprite(buttonSheet, sequenceData)
btn1.x = 100
btn1.y = 90
local btn2 = display.newSprite(buttonSheet, sequenceData)
btn2.x = 200
btn2.y = 230
local btn3 = display.newSprite(buttonSheet, sequenceData)
btn3.x = 300
btn3.y = 90
local btn4 = display.newSprite(buttonSheet, sequenceData)
btn4.x = 400
btn4.y = 230
x = buttons[math.random(#buttons)]
x:setFrame(2)
圆圈目前是黑色的。每次我 运行 我希望它从列表中获取一个随机值并将颜色更改为红色的代码。所以当我 运行 代码
时有一个不同的红色圆圈我一直收到错误消息: "Attemp to call method 'setSequence' (a nill value)"
您的代码将字符串和变量名视为可以互换。第一行:
buttons = {"btn1","btn2","btn3","btn4"}
创建一个 table 字符串,所以行:
x = buttons[math.random(#buttons)]
会将 x
设置为 buttons
的随机条目,它们是字符串,因此下一行 (x:setFrame(2)
) 正在调用字符串中不存在的方法。
相反,创建一个 table 按钮:
buttons = {btn1,btn2,btn3,btn4} -- creates a table of buttons
x = buttons[math.random(#buttons)] -- x is a random entry of buttons (a button)