【发布时间】:2014-02-25 15:44:45
【问题描述】:
我为此制作了自己的脚本,但是它不想正常工作:
- 当您不直接触摸图像时,它不会做出反应。
- 如果触摸正常但稍微移动了一点,那就是卡顿了,拍摄按钮不起作用。
如果它像这样工作,我会很高兴:
- 当触摸按钮周围或周围时,旋转我的船。 完成
- 当您稍微移动手指时,仍要旋转飞船。 完成
- 如果您只是点击,然后稍微旋转一下。 完成
- 如果您将手指放在按钮上,则旋转 5°,稍长一点后旋转约 8°。 完成
- 现在,当我触摸其中一个 dpad 并触摸拍摄按钮时,它会停止旋转。这需要修复,我不知道如何。我需要能够在旋转时进行拍摄。
好吧,这就是我用我的代码尝试过的。
local isPressed = false
local tmr_holdFlight
local touchedXTimes = 0
function holdingLeft()
if isPressed == true then
if rotationOfship > 0 then
if touchedXTimes < 10 then
rotationOfship = rotationOfship-2
touchedXTimes = touchedXTimes + 1
ship.rotation = rotationOfship
elseif touchedXTimes > 9 then
rotationOfship = rotationOfship-5
touchedXTimes = touchedXTimes + 1
ship.rotation = rotationOfship
elseif touchedXTimes > 29 then
rotationOfship = rotationOfship-8
touchedXTimes = touchedXTimes + 1
ship.rotation = rotationOfship
end
print(touchedXTimes)
else
rotationOfship = 360
end
elseif isPressed == false then
timer.cancel(tmr_hold)
isPressed = false
end
end
function holdingRight()
if isPressed == true then
if rotationOfship < 360 then
if touchedXTimes < 10 then
rotationOfship = rotationOfship+2
touchedXTimes = touchedXTimes + 1
ship.rotation = rotationOfship
elseif touchedXTimes > 9 then
rotationOfship = rotationOfship+5
touchedXTimes = touchedXTimes + 1
ship.rotation = rotationOfship
elseif touchedXTimes > 29 then
rotationOfship = rotationOfship+8
touchedXTimes = touchedXTimes + 1
ship.rotation = rotationOfship
end
print(touchedXTimes)
else
rotationOfship = 0
end
elseif isPressed == false then
timer.cancel(tmr_hold)
end
end
function onDpadLeftTouch(event)
-- body
if event.phase=='began' then
display.getCurrentStage():setFocus(event.target)
end
if event.phase=='began' then
isPressed = true
if tmr_hold ~= nil then timer.cancel(tmr_hold) end
tmr_hold = timer.performWithDelay( 8, holdingLeft, 0)
elseif event.phase == "ended" then
isPressed = false
timer.cancel(tmr_hold)
display.getCurrentStage():setFocus(nil)
touchedXTimes = 0
end
end
function onDpadRightTouch(event)
-- body
if event.phase=='began' then
display.getCurrentStage():setFocus(event.target)
end
if event.phase=='began' then
isPressed = true
if tmr_hold ~= nil then timer.cancel(tmr_hold) end
tmr_hold = timer.performWithDelay( 8, holdingRight, 0)
elseif event.phase=='ended' then
isPressed = false
timer.cancel(tmr_hold)
display.getCurrentStage():setFocus(nil)
touchedXTimes = 0
end
end
local function pointAtDistance(angle, distance)
-- Convert angle to radians as lua math functions expect radians
local r = math.rad(angle)
local x = math.cos(r) * distance
local y = math.sin(r) * distance
return x, y
end
local isShooting = false
function resetShooting()
isShooting = false
end
function onLaserButtonTouch(event)
if event.phase == "began" and isShooting == false then
isShooting = true
bullet = display.newImageRect("images/laser.png",math.random(5,20),5/2)
bullet.x = halfW
bullet.y = halfH
bullet.name = "bullet"
bullet.rotation = rotationOfship-90
physics.addBody( bullet, "dynamic", { isSensor=true, radius=10} )
group:insert(bullet)
ship:toFront()
audio.play( laserSound, { channel=2, loops=0} )
local newX, newY = pointAtDistance(rotationOfship-90, 400)
bullet:setLinearVelocity( newX/2, newY/2 )
tmr_shoot = timer.performWithDelay( math.random(300,400), resetShooting, 1)
elseif event.phase=='ended' then
end
end
dpad_left = display.newImageRect("images/dpad/left.png", 78/2, 78/2)
dpad_left.x = 50
dpad_left.y = 260
group:insert(dpad_left)
dpad_left:addEventListener("touch", onDpadLeftTouch)
dpad_right = display.newImageRect("images/dpad/right.png", 78/2, 78/2)
dpad_right.x = 100
dpad_right.y = 260
group:insert(dpad_right)
dpad_right:addEventListener("touch", onDpadRightTouch)
laser_button = display.newImageRect("images/dpad/laser.png", 78/2, 78/2)
laser_button.x = 430
laser_button.y = 260
group:insert(laser_button)
laser_button:addEventListener("touch", onLaserButtonTouch)
【问题讨论】:
-
你的描述很难理解,所以我重新格式化以显示它是如何出现的,请看一下。假设是这样,那么你的问题就太多了:一次解决每一件事情。就像,首先让要求 1 工作,然后是 2,然后是 3,然后是 4。在 1 工作之前不要从 2 开始。您可能会发现在 SO 上您只剩下这四个项目中的一个,然后您可以大大简化您的问题,您将获得更多有用的帮助(詹姆斯的回答是一个勇敢的努力,但很难判断它是否再次提及所有 4 个项目,太多了)。
-
您好,感谢您的帮助,所以我昨天坐下来解决了所有 4 个问题。谢谢你告诉我我应该一块一块地解决它。我认为它为我节省了一些时间:D。现在我只有一个问题,然后我就解决了所有问题。并且问题已添加到列表及其编号 5 中。其他一切都按我希望的方式工作。谢谢你的时间:)
-
很高兴听到,这比我预期的要好。我把它变成了一个答案。
-
是的,现在只剩下第 5 个问题。 :/ 你知道怎么解决吗?