我对编程非常陌生,我正在尝试了解如何在corona SDK中的特定时间显示文本或更新文本。
这是一个音乐应用,我希望文本看起来与音乐同步,就像卡拉OK,但更简单。只是为了显示歌词的句子,然后在特定时间用下一个替换它。 此外,我希望如果用户重播声音,计时器重新启动为0,以便文本和音乐仍然会同步
如果你这样做,请告诉我。我真的很难理解如何做到这一点
谢谢!
答案 0 :(得分:1)
带有数组的简单计时器可用于实现您想要的效果。
这个想法是在上一个计时器被触发后的一个计时器周期后启动一个计时器。
看看下面的代码:
i = 2
--change the text to next line
function changeText( event )
if i > #lyrics then
timer.cancel(curTimer);
print("timer destroyed")
else
print("lyrics[".. i .."]: "..lyrics[i].line)
lyricsDisp.text = lyrics[i].line
curTimer = timer.performWithDelay (lyrics[i].time, changeText,1)
end
i = i + 1
end
--initialize lyrics in an array
lyrics = {}
lyrics[1] = {}
lyrics[1].line = "First line of song"
lyrics[1].time = 0
lyrics[2] = {}
lyrics[2].line = "Second line of song"
lyrics[2].time = 2000
lyrics[3] = {}
lyrics[3].line = "Third line of song"
lyrics[3].time = 5000
lyrics[4] = {}
lyrics[4].line = "Fourth line of song"
lyrics[4].time = 1000
lyrics[5] = {}
lyrics[5].line = "Fifth line of song"
lyrics[5].time = 3000
--restart song event handler
restartSong = function( self, event )
if event.phase == 'ended' then
timer.cancel(curTimer)
lyricsDisp.text = lyrics[1].line
i = 2
curTimer = timer.performWithDelay(lyrics[i].time, changeText,1)
end
end
--Initialize the lyrics text
lyricsDisp = display.newText(lyrics[i-1].line, 0, 0, native.systemFont,30)
lyricsDisp.x , lyricsDisp.y = display.contentWidth/2 , display.contentHeight/2
--start the lyrics timer
curTimer = timer.performWithDelay(lyrics[i].time, changeText,1)
--display object to restart the lyrics
restartText = display.newText("Reset", 0, 0, native.systemFont,30)
restartText.x , restartText.y = display.contentWidth/2 , display.contentHeight/2 + 200
restartText.touch = restartSong
restartText:addEventListener('touch', restartText)
答案 1 :(得分:0)
例如:
local lyrics_label = display.newtext("This is my first line.... line 1",160,100,nil,15)
local lyrics_lines_array = {"This is my first line.... line 2",
"This is my second line.... line 3",
"This is my third line.... line 4",
"This is my fourth line.... line 5"}
local index = 0
local function textChange()
index = index + 1
lyrics_label.text = lyrics_lines_array[]
end
timer.performWithDelay(1000,textChange,#lyrics_lines_array)
--[[ here 1000 ==> the time/delay to change the text ]]--