2013-04-11 43 views
0

基本上会发生什么事是对象从屏幕随机位置产生,然后流入和流出屏幕视图。当他们在屏幕上流动时,他们在屏幕上的任意位置再次重新设置屏幕,但是如果玩家与屏幕发生碰撞,我无法做到这一点。碰撞后重新设置产卵

所以总结一下,如何让玩家在与玩家碰撞时将对象位置重新屏蔽掉?

继承人对象代码。

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) 

船代码:的addEventListener( “碰撞”,onCollision,船舶,ADDTIME)

回答

0

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

+0

嘿感谢您的回复和链接。我确实了解大多数碰撞检测,并且有一些工作的设置,但是只有在碰撞时才能破坏。我只是努力尝试重新屏蔽它的身体,让它流回来。有点像浮动可收藏的明星。任何想法如何我可以再次重置其位置? – 2013-04-12 13:30:28

+0

如果您编辑您的问题,包括您检测碰撞的代码块,我可以帮助easly – 2013-04-12 13:48:39

+0

感谢您的帮助,我按照您的要求编辑了我的问题。希望能帮助到你。 – 2013-04-12 14:19:24