【问题标题】:Re-set spawn after collision碰撞后重新设置生成
【发布时间】:2013-04-11 18:52:12
【问题描述】:

基本上发生的情况是对象在随机位置从屏幕生成,然后流入和流出屏幕视图。 随着屏幕的流动,他们再次在屏幕外的随机位置重置,但是如果玩家与它发生碰撞,我无法做到这一点。

总而言之,我如何让对象位置在与玩家碰撞时再次从屏幕上重生?

这是目标代码。

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)

【问题讨论】:

    标签: lua coronasdk collision


    【解决方案1】:

    如果我没有误会你不知道碰撞检测。你应该研究这个页面: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 物理会在碰撞时间内锁定所有物理对象。所以你应该在碰撞后立即移动你的物体。

    【讨论】:

    • 您好,感谢您的回复和链接。我确实了解大多数碰撞检测并设置了一些工作但仅在碰撞时破坏。我只是在努力尝试再次将它的身体从屏幕上重置,以便它重新流入。有点像一个漂浮的收藏明星。知道如何再次重置其位置吗?
    • 如果您编辑问题,包括检测到冲突的代码块,我可以轻松提供帮助
    • 感谢您的帮助,我按照您的要求编辑了我的问题。希望对您有所帮助。
    • 我不明白你为什么要删除 UFO 命名对象。您不想更新它的位置而不是删除它吗?
    • 我在上次处理它时试图重新生成它,但从未成功。我确实想在碰撞后将其位置更新为随机 x 和 y。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-04
    相关资源
    最近更新 更多