在碰撞后重新设置生成点。

基本上发生的情况是对象在屏幕外以随机位置生成,然后在屏幕中流动进出。它们流出屏幕时会在屏幕外的随机位置重置,但是如果玩家与它碰撞,我无法使其发生。

总之,我该如何使对象在与玩家碰撞时重新生成在屏幕外?

这是对象代码。

UFO = 显示新图像(“ ufo.png”)
  UFO.name = “ UFO”
  UFO.x = 640
  UFO.y = 100
  UFO.speed = math.random2,6)
  UFO.initY = UFO.y
  UFO.amp = math.random20,100)
  UFO.angle = math.random1,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)
            如果(event.other.name =='UFO')则
                    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 = 0onComplete = function()display.remove(scoreAnim)scoreAnim = nil end })
                    scoreAnim:setTextColor(0,255,12)
                    - 增加分数
                    collectedN.text = tostring(tonumber(collectedN.text)+ 1

ship:addEventListener(“ collision”,onCollision,ship,addTime)

点赞
用户2186639
用户2186639

如果我没理解错,你不知道什么是碰撞检测。你应该学习这个页面:http://developer.coronalabs.com/content/game-edition-collision-detection

以下是编辑部分:

function ship:collision(event)
    if (event.other.name == 'UFO') then
          timer.performWithDelay( 50, function() event.other.x = math.random( 0, 320 ); event.other.y = math.random( 0.480 ); end, 1 )
          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)

这样就可以了。你可以在那里修改随机值。在碰撞时,Corona物理引擎将锁定所有物理对象,所以你不能在碰撞时移动你的对象。因此,你应该在碰撞后立即移动你的对象。

2013-04-11 21:35:26