這段代碼創建了一個大炮和3個氣球,大炮應該發射一顆子彈來破壞氣球,還有這些詞語。杜林大炮應該旋轉的過程中,當我從屏幕上釋放我的手指射擊。出於某種原因,它不響應,大炮不旋轉,也沒有射擊任何子彈。物理學在lua中不起作用
local score = 0
local scoreText
local scoreForLevelComplete
local background
local infoBar
local restartBtn
local cannon
local levelNum
local cannonCharge = {}
local shot = {}
local cannonBall
local impulse = 0
local balloons = {}
local cannonCharge = {}
local shot = {}
function scene:createScene(event)
local group = self.view
background = display.newImage("bkg_clouds.png")
group:insert(background)
background.x = 230
background.y = 195
scoreText = display.newText("0", 0, 0, native.systemFont, 32)
scoreText:setFillColor(0,0, 0)
scoreText.x = 87
scoreText.y = 28
group:insert(scoreText)
questionText = display.newText('a', display.contentCenterX, display.contentWidth/4, native.systemFont, 40)
group:insert(questionText)
infoBar = display.newImage ("infoBar.png")
group:insert(infoBar)
infoBar.x = 10
infoBar.y = 25
restartBtn = display.newImage ("restartBtn.png")
group:insert(restartBtn)
restartBtn.x = 470
restartBtn.y = 300
cannon = display.newImage ("cannon.png")
group:insert(cannon)
cannon.x = 10
cannon.y = 270
cannon.anchorX = 0.5
cannon.anchorY = 0.5
restartBtn.isVisible = true
local balloon = display.newImage ('balloon_fat_red.png', 495, 125)
group:insert(balloon)
balloon = display.newImage ('balloon_fat_red.png', 495, 175)
group:insert(balloon)
balloon = display.newImage ('balloon_fat_red.png', 495, 225)
group:insert(balloon)
local balloonText1 = display.newText('\227\129\130', 495, 125)
balloonText1:setFillColor(1,1, 0)
local balloonText2 = display.newText('\227\129\132', 495, 170)
balloonText2:setFillColor(1,1, 0)
local balloonText3 = display.newText('\227\129\134', 495, 225)
balloonText3:setFillColor(1,1, 0)
balloon.name = 'balloon'
physics.addBody(balloon)
balloon.bodyType = 'static'
table.insert(balloons, balloon)
group:insert(balloonText1)
group:insert(balloonText2)
group:insert(balloonText3)
function ballCollision(e)
if (e.other.name == 'balloon') then
scene.updateScore()
e.target:removeSelf()
print ('remove balloon text')
e.other:removeSelf()
audio.play(pop)
end
end
function cannonCharge:touch(e)
if(e.phase == 'began') then
impulse = 0
cannon.isVisible = true
Runtime:addEventListener('enterFrame', charge)
end
end
function charge()
local degreesPerFrame = 0.5
cannon.rotation = cannon.rotation - degreesPerFrame
impulse=impulse-0.2
if(cannon.rotation < -46) then
cannon.rotation = -46
impulse = -3.2
end
end
function shot:touch(e)
if(e.phase == 'ended') then
Runtime:removeEventListener('enterFrame', charge)
cannon.isVisible = true
cannon.rotation = 0
cannonBall = display.newImage('cannon ball.png', 84, 220)
physics.addBody(cannonBall, {density = 1, friction = 0, bounce = 0})
group:insert(cannonBall)
-- Shoot cannon ball
cannonBall:applyLinearImpulse(3, impulse, cannonBall.x, cannonBall.y)
--Collision listener
cannonBall:addEventListener ('collision', ballCollision)
end
end
end
這是我enterscene功能
function scene:enterScene(event)
local group = self.view
background:addEventListener('touch', cannonCharge)
background:addEventListener('touch', shot)
end
對不起,格式,我編輯它,還添加了一些事件監聽器,但氣球仍然不會消失後,球擊中他們。 – user3305142
函數cannonCharge:touch(e)和function shot:touch(e)持有對象,如果這就是你的意思。此外,只有第三個氣球消失,前兩個不會。 – user3305142