0

我对编程很陌生,我正在尝试了解如何在电晕 SDK 中的特定时间显示文本或更新它。

这是为了音乐应用,我希望文本与音乐同步,就像卡拉 OK 一样,但更简单。只是显示歌词的一个句子,然后在特定时间用下一个替换它。另外我希望如果用户重播声音,计时器会重新启动到 0,以便文本和音乐仍然同步

如果你该怎么做,请告诉我。我真的很难理解如何去做

谢谢 !

4

2 回答 2

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)
于 2013-04-09T12:53:18.680 回答
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 ]]--
于 2013-04-08T06:43:45.720 回答