2014-01-13 245 views
0

比方说,我有一个数组5个对象,我将所有他们沿x轴这样的:移动物体

vx = 5; 

for (i:int = 0; i < objects.length; i++) 
{ 
objects[i].x += vx; 
} 

我想使这个。 如果数组'object'中的任何对象碰到PointA,则将该数组中的所有对象移动到左侧,例如set vx * = -1;

我只能让这个:

for (i:int = 0; i < objects.length; i++) 
{ 
// move right 
objects[i].x += vx; 

if (objects[i].hitTest(PointA)) 
{ 
    // move left 
    vx *= -1; 
} 
} 

这将改变对象的方向,但我需要等待所有OBJETS命中点A。

如何更改数组中所有对象的方向,如果它们中的任何一个碰到PointA?

+0

它看起来像你想改变方向一次**任何**的物体到达目的地,对吗?您不打算立即将它们全部重置为起源,而是一旦到达PointA就继续递增地移动它们。 – Atriace

回答

0

我不知道动作脚本,但你应该设置一个布尔值外的for循环,像bGoingRight

检查,这是真实的,正确的移动物体,如果走错一步的对象离开了。当你传递hitTest时,你应该把布尔值改为false。

粗糙例

var bGoRight:Boolean = true; 

    for (i:int = 0; i < objects.length; i++) 
    { 
    if(bGoRight) 
    { 
    // move right 
    objects[i].x += vx; 
    } 
    else 
    { 
    // move left 
    vx *= -1; 
    } 

    if (objects[i].hitTest(PointA)) 
    { 
    // if any object hit the point, set the flag to move left 
    bGoRight = false; 
    } 
} 
+0

林不知道你是否明白我的问题。 –

+0

如果object1碰到pointA,我很容易将object1移动到左边,然后如果object2碰到PointA,将object2移动到左边等等......但是如何让object1碰到PointA,将所有其他对象移动到左边,而不仅仅是object1? –

+0

如果bGoRight设置为false,则所有对象都将向左移动,因为您将在绘制时检查该对象。所以当一个对象碰到PointA时,只需将bGoRight从true切换到false。 – JMG

0

所以你需要检查已经打到点A的对象,存储它们,然后检查更新的存储数量相当于你的对象数组。然后,当这种情况下,你可以改变vx变量。这可能是这个样子:

//set a variable for storing collided object references 
//note: if you are doing this in the IDE remove the 'private' attribute 
private var _contactList:Array = []; 

for (i:int = 0; i < objects.length; i++) 
{ 
    // move right 
    objects[i].x += vx; 

    if (objects[i].hitTest(PointA)) 
    { 
      if(contactList.length == objects.length) { 
      // move left 
      vx *= -1; 
      //clear our contact list 
      contactList.length = 0; 
      } 
      else if (noContact(objects[i])) { 
       contactList.push(objects[i]); 
      } 
    } 
} 

然后在其他功能if语句noContact(),如果你再在IDE中这样做,你将需要删除private属性。

private function noContact(obj:*):Boolean { 
    if (contactList.indexOf(obj) != -1) { 
     return false; 
    } 
    return true; 
} 

你可以做到这一点的另一种方式是这样的(如对方回答说一个布尔值的方式),但在你的存储设置正确的依赖:

//set this variable for directionRight (boolean) 
private var directionRight:Boolean = true; 

for (i:int = 0; i < objects.length; i++) 
{ 
    // move right 
    objects[i].x += vx; 

    if (objects[i].hitTest(PointA)) 
    { 
      //we are moving to the right, object[i] is the last object 
      if(i == objects.length-1 && directionRight) { 
      // move left 
      directionRight = !directionRight; 
      vx *= -1; 
      } 
      else if (i == 0 && !directionRight)) { 
       // move right 
       directionRight = !directionRight; 
       vx *= -1; 
      } 
    } 
}