基本上发生的情况是对象在随机位置从屏幕生成,然后流入和流出屏幕视图。随着屏幕的流动,它们再次在屏幕外的随机位置重置,但是如果玩家与它发生碰撞,我无法做到这一点。
总而言之,我如何让对象位置在与玩家碰撞时再次从屏幕上重生?
这是目标代码。
UFO = display.newImage("ufo.png")
UFO.name = "UFO"
UFO.x = 640
UFO.y = 100
UFO.speed = math.random(2,6)
UFO.initY = UFO.y
UFO.amp = math.random(20,100)
UFO.angle = math.random(1,360)
physics.addBody(UFO, "static")
function moveUFO(self,event)
if self.x < -50 then
self.x = math.random(500,1500)
self.y = math.random(90,220)
self.speed = math.random(2,6)
self.amp = math.random(20,100)
self.angle = math.random(1,360)
else
self.x = self.x - self.speed
self.angle = self.angle + .1
self.y = self.amp*math.sin(self.angle)+self.initY
end
这是碰撞检测的代码
function ship:collision(event)
if (event.other.name == 'UFO') then
event.other:removeSelf(self)
scoreAnim = display.newText('+10', ship.x, ship.y-10, native.systemFontBold, 16)
transition.to(scoreAnim, {time = 1000, y = scoreAnim.y - 30, alpha = 0, onComplete = function() display.remove(scoreAnim) scoreAnim = nil end})
scoreAnim:setTextColor(0,255,12)
--increases score
collectedN.text = tostring(tonumber(collectedN.text) + 1)
ship:addEventListener("collision", onCollision, ship, addTime)