简体   繁体   English

如何阻止对象在Corona SDK的同一路径上移动

[英]How to stop objects moving on the same path in Corona sdk

I have player and enemy objects on the scene. 我在现场有玩家和敌人的物体。 The enemies are generated randomly at the specified path, and the other one is player which is generated from the opposite side of enemy. 敌人是在指定路径上随机生成的,另一个是从敌人另一侧生成的玩家。 I want to stop both these objects if they come some distance apart on the same path, and if any one of the two objects are destroyed by the firing then the other remaining object whether it must be enemy or player must start moving. 如果这两个物体在同一条路径上相距一定距离,并且两个物体中的任何一个被射击击毁,那么我要停止这两个物体,然后剩下的另一个物体(无论是敌人还是玩家)必须开始移动。

I have implemented below code, and it is working for the first time when they both are in front of each other and 200 distance apart. 我已经实现了下面的代码,并且当它们彼此面对面并且相距200距离时,它第一次起作用。 Suppose player fire the enemy then the enemy gets destroyed, and player starts moving but if now another enemy comes in front of the player then player does not stop, it keeps on moving, but the enemy stops. 假设玩家射击敌人,然后敌人被消灭,并且玩家开始移动,但是如果现在有另一个敌人出现在玩家面前,则玩家不会停止,它会继续移动,但是敌人会停止。 Please suggest some solution. 请提出一些解决方案。 Thanks... 谢谢...

for j=#enemy, 1, -1 do
  if(enemy[j].y~=nil and player.y~=nil then
   diff=player.y-enemy[j].y 
   if(enemy[j].x==player.x) then 
     if(diff<=200) then
       enemy[j].Speed=0
       player.speed=0
     end
   end
  else
  player.speed=-50
  enemy[j].Speed=2
  end
end

You are setting player.speed=-50 for any nil enemy[j].y 您正在为任何nil enemy[j].y设置player.speed=-50 enemy[j].y

Perhaps you should move the line 也许你应该改变思路

player.speed=-50

to above the for loop. for循环之上。 That way, the player will move by default, but can be stopped by any enemy. 这样,玩家将默认移动,但可以被任何敌人阻止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM