2013-04-22 83 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