2013-04-22 88 views
1

我試圖讓科羅納一個遊戲,如果我點擊並按住屏幕上的輪將旋轉,但只有當我不斷點擊屏幕旋轉如何我改變這一點,以便爲只要我的手指按在屏幕上,輪子會旋轉?這裏是我的代碼:科羅娜SDK觸摸階段 - 觸摸並保持

local physics = require "physics" 
physics.start() 

--Variables 

--[bike = display.newImage("bike.png") 
--bike.x = 70 
--bike.y = 290 
--physics.addBody(bike, {friction = 0.3, bounce = 0.2}) 

wheel1 = display.newImage("wheel.png") 
wheel1.x = 480/2 
wheel1.y = 320/2 

wheel2 = display.newImage("wheel.png") 
wheel2.x = 480/2 + 50 
wheel2.y = 320/2 - 50 

driveBtn = display.newImage("drive.png") 


local function driveFunction(event) 


    wheel1.rotation = wheel1.rotation + 3 
    wheel2.rotation = wheel2.rotation + 3 


end 


Runtime:addEventListener("touch", driveFunction) 
+0

[ot]你好,歡迎來到SO。我希望我能夠在14歲時爲corona SDK編碼,所以,我猜這很不錯! – Saturnix 2013-04-23 01:15:03

回答

0

這是一個非常簡單的解決方案。順便說一句,你很早就開始發展,保持編碼^^如果你不理解下面的代碼或邏輯背後的邏輯,只需發表評論和問。

local physics = require "physics" 
physics.start() 

--Variables 

--[bike = display.newImage("bike.png") 
--bike.x = 70 
--bike.y = 290 
--physics.addBody(bike, {friction = 0.3, bounce = 0.2}) 

local wheel1 = display.newImage("wheel.png") 
wheel1.x = 480/2 
wheel1.y = 320/2 

local wheel2 = display.newImage("wheel.png") 
wheel2.x = 480/2 + 50 
wheel2.y = 320/2 - 50 

local driveBtn = display.newImage("drive.png") 

local function rotateWheel() 
     wheel1.rotation = (wheel1.rotation + 3) % 360 
     wheel2.rotation = (wheel2.rotation + 3) % 360 
end 

local function driveFunction(event) 
    if event.phase == "began" then 
      display.getCurrentStage():setFocus(wheel1) 
      wheel1.isFocus = true 
      Runtime:addEventListener("enterFrame", rotateWheel) 
    elseif wheel1.isFocus then    
      if event.phase == "moved" then 
      elseif event.phase == "ended" then 
       Runtime:removeEventListener("enterFrame", rotateWheel) 
       display.getCurrentStage():setFocus(nil) 
       wheel1.isFocus = false 
      end 
    end 
end 

Runtime:addEventListener("touch", driveFunction) 
+0

好的,謝謝你的回覆!但模擬器加載main.lua文件完全正常,但是當我在屏幕上點擊一個錯誤出現:行:30 試圖索引字段「目標」(一個零值)我不明白,我已經嘗試它無數次,但沒有運氣:/ – 2013-04-23 17:56:55

+0

我編輯了代碼 – 2013-04-23 21:37:13