Corona SDK - 根据触摸使球弹跳
Corona SDK - Make a ball bounce based on touch
所以我试图让一个球落下,当它被触摸时,我需要它沿着球被触摸的方向弹跳。现在我所做的只是直线上升,但如果我要轻敲球的边缘,它需要开始朝那个方向前进,但也要向上。
该游戏是一款足球保持上升游戏,您点击,球会上升,如果它落地,得分会重置。简单的。但是我似乎找不到任何关于如何让球左右滚动以及向上滚动的例子。
local ball = display.newImageRect("soccerball.png", 112, 112)
ball.x = display.contentCenterX
ball.y = display.contentCenterY
physics.addBody(ball, "dynamic", {radius=50, bounce=0.3})
local function pushBall(event)
ball:applyLinearImpulse(0, -0.75, ball.x, ball.y)
tapCount = tapCount + 1
tapText.text = tapCount
end
ball:addEventListener("touch", pushBall)
上面的代码只允许球直线上升。没有滚动。
感谢您的帮助。
尝试
function ball:touch( event )
local phase = event.phase
if ( phase == 'ended' ) then
local x, y = ball.x - event.x, ball.y - event.y
self:applyLinearImpulse( x / 30, -math.abs(y) / 30, self.x, self.y )
--tapCount = tapCount + 1
--tapText.text = tapCount
end
return true
end
ball:addEventListener("touch")
所以我试图让一个球落下,当它被触摸时,我需要它沿着球被触摸的方向弹跳。现在我所做的只是直线上升,但如果我要轻敲球的边缘,它需要开始朝那个方向前进,但也要向上。
该游戏是一款足球保持上升游戏,您点击,球会上升,如果它落地,得分会重置。简单的。但是我似乎找不到任何关于如何让球左右滚动以及向上滚动的例子。
local ball = display.newImageRect("soccerball.png", 112, 112)
ball.x = display.contentCenterX
ball.y = display.contentCenterY
physics.addBody(ball, "dynamic", {radius=50, bounce=0.3})
local function pushBall(event)
ball:applyLinearImpulse(0, -0.75, ball.x, ball.y)
tapCount = tapCount + 1
tapText.text = tapCount
end
ball:addEventListener("touch", pushBall)
上面的代码只允许球直线上升。没有滚动。
感谢您的帮助。
尝试
function ball:touch( event )
local phase = event.phase
if ( phase == 'ended' ) then
local x, y = ball.x - event.x, ball.y - event.y
self:applyLinearImpulse( x / 30, -math.abs(y) / 30, self.x, self.y )
--tapCount = tapCount + 1
--tapText.text = tapCount
end
return true
end
ball:addEventListener("touch")