1

我正在尝试在 Corona 中制作一个游戏,如果我点击并按住屏幕,滚轮会旋转,但只有当我继续点击屏幕时它才会旋转我如何更改它,这样只要我的手指按下屏幕,轮子会转动吗?这是我的代码:

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)
4

1 回答 1

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 )
于 2013-04-23T01:10:48.823 回答