2014-05-09 47 views
0

我努力工作,为我的比赛时承认此阵的两个实例具有相同的X或Y位置作为另一种方式,如果是移动重随机实例之一:位置定位阵列的两个实例是否在同一位置

for(i=0;i<8;i++) { 
     PlatformInstance[i] =new Platform(); 
    PlatformInstance[i].y= Math.random()*900; 
    PlatformInstance[i].x= Math.random() *1500; 
    stage.addChild(PlatformInstance[i]); 

} 

我的问题是,我尝试代码检测到一个平台实例具有相同的位置,相同的平台实例,并会不断地重新随机位置。 有没有区分不同实例的方法?

非常感谢。

编辑

唯一的代码,我能想到的是运行在一个循环中的if语句,看看

If (PlatformInstance[i].y == PlatformInstance[i].y)

显然这是否难道不工作,它的思维我知道这将不但是我想知道是否有一种方法可以做到:

If (Platforminstance[i].y == "other" Platforminstance[i].y 

或一些其他类似的话

+0

您尝试过哪些代码?此外,我认为你指的是“数组中的元素”,而不是“数组的实例”,对吗?你的措辞有点令人困惑 – VBCPP

+0

对不起,我的意思是数组的元素,我能想到的唯一代码是在循环中运行if语句来查看PlatformInstance [i] .y == PlatformInstance [i] .y ,显然这难道不工作,它的思考,我知道这不会,但是我想知道是否有这样做的方式: – user3620509

+0

如果(Platforminstance [I] .Y ==“其他” Platforminstance [I] .Y – user3620509

回答

-1

你似乎没有清楚地理解数组是如何工作的,我建议研究一下。像这样的东西应该工作

i=0; 
    j=0; 
    while(i < PlatformInstance.length -1) { 
     j++; 
     if (j == PlatformInstance.length) { 
     i++; 
     j=i+1; 
     } 

     if((Math.abs(PlatformInstance[i].x - PlatformInstance[j].x) < platformWidth) 
     || (Math.abs(PlatformInstance[i].y - PlatformInstance[j].y) < platformHeight)) { 

     PlatformInstance[j].y= Math.random()*900; 
     PlatformInstance[j].x= Math.random() *1500; 
     //Now you'll have start checking at the beginning again since you moved one 
     i=0; 
     j=0; 
     continue; 
     } 
    } 

也如先前提到的,PlatformInstance不是一个数组一个好名字。只有类名应该以大写字母开头。并且不应将一系列平台称为“实例”。我会建议将其更改为简单platforms

+0

非常感谢,这似乎是工作在一定程度上,我有唯一的问题是,因为平台大小合适,他们有时仍然重叠,是它能够使 如果(PlatformInstance [I] .X == PlatformInstance [j]的.X || PlatformInstance [I] .Y == PlatformInstance [J ]。 y) 成一个不直接相等的东西,但有一个更大的范围? 再次感谢 – user3620509

+0

编辑。你可以将'platformWidth'和'platformHeight'设置为你想要的。如果每个平台可以有不同的大小,这不会是一个很好的解决方案,尽管 – VBCPP

+0

非常感谢您!只是刚刚能够全面测试这个,但它完美的作品,它不再重叠!我非常感谢您的帮助! – user3620509

相关问题