2015-04-02 31 views
0

所以我使用光環,我試圖創建一個遊戲,有一個圓越來越大,直到它的點擊,它停止擴張,然後出現一個新的圓,然後開始擴張以及。遊戲的重點在於儘可能多的圈入彼此。我的問題是我已經能夠做出一個無限循環,以便這個循環永遠變得越來越小,但是現在我試圖讓它在點擊它時停止。我試過把它放在一個循環中,雖然點擊的數量是一個,但它會去,然後當它的兩個它會停止,但它不工作。你們能幫忙嗎?使圓擴張直到點擊

下面是我現在的代碼,除了出現一個小圓圈以外什麼也沒有發生。

circa = display.newCircle(display.contentWidth/2, display.contentHeight/2, 20) 
circa:setFillColor(noFillColor) 
circa.strokeWidth=5 
circa:setStrokeColor(1,0,0) 

local function numberOfCircles (event) 

    if (event.numTaps==1) then 
    x=1 
    elseif (event.numTaps==2) then 
    x=2 
    end 

end 

Runtime:addEventListener("enterFrame" , numberOfCircles) 

while x==1 do 

    i = i + 1 

    local function startCircle(event) 
    resize = circa.path 
    circa.scale=transition.to(resize, {radius = 300, time=1000, x= event.x, y=event.y}) 
    end 


    local function endCircle(event) 
    reresize = circa.path 
    circa.scale = transition.to(resize, {radius = 20, time=1000, x= event.x, y = event.y}) 
    end 

    timer.performWithDelay(1000*i, startCircle) 
    timer.performWithDelay(2000*i+1,endCircle) 
end 


if x == 2 then 
    circa.setLinearVelocity(0, 0) 
end 

回答

1

實現這一目標的最佳方法是在代碼中進行2次更改。

首先,讓我們來看看你的無限循環。我們可以通過使用過渡重複模式來達到相同的效果。

然後,我們只爲您的轉換添加一個標籤,以便通過調用transition.cancel(「tagname」)輕鬆取消轉換。

這裏是你的代碼怎麼會:

circa = display.newCircle(display.contentWidth/2, display.contentHeight/2, 20) 
circa:setFillColor(noFillColor) 
circa.strokeWidth=5 
circa:setStrokeColor(1,0,0) 


local scaleFactor = 300/20 

transition.to(circa, {xScale=scaleFactor, yScale=scaleFactor, time=1500, transition= easing.continuousLoop,iterations=-1, tag="myTransition"}) 


local function onBackgroundTap(e) 

    print("user clicked on background. Let's stop transition") 

    transition.cancel("myTransition") 

end 

Runtime:addEventListener("tap", onBackgroundTap) 
+0

真棒!非常感謝! – dandy012 2015-04-13 17:19:07

+0

你知道我如何做出一個if語句,如果點擊的圓圈比前一個圓圈大,結束所有事情? – dandy012 2015-04-14 17:23:27

+0

哼。可能會添加一個存儲當前圓圈大小的「enterFrame」偵聽器,並保存先前的圓圈大小,然後在「tap」偵聽器上,我會檢查這些值(當前圓圈大小x之前的圓圈大小),然後取消轉換(如果案例) – rsc 2015-04-16 06:38:41