【问题标题】:Moving objects on the stage在舞台上移动物体
【发布时间】:2014-02-01 19:26:17
【问题描述】:

假设我在一个数组中有 5 个对象,我将它们全部沿 x 轴移动,如下所示:

vx = 5;

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

我想做这个。 如果数组“objects”中的任何对象命中 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;
 }
}

这会改变物体的方向,但我需要等待所有物体都击中 PointA。

如何改变数组中所有物体的方向,如果它们中的任何一个到达PointA?

【问题讨论】:

  • 您似乎想在 任何 对象到达目的地后改变方向,对吗?您不希望立即将它们全部重置为其原点,而是在到达 A 点后继续逐步反向移动它们?

标签: actionscript-3 flash actionscript


【解决方案1】:

我不知道动作脚本,但你应该在 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;
    }
 }

【讨论】:

  • 我不确定你是否理解我的问题。
  • 如果object1击中pointA将object1向左移动,然后如果object2击中PointA,则将object2向左移动,依此类推...但是如何制作如果object1击中PointA,将所有其他对象移动到左,不只是object1?
  • 如果 bGoRight 设置为 false,所有对象都将向左移动,因为您将在绘制它们时检查它们。因此,当一个物体击中 PointA 时,只需将 bGoRight 从 true 切换为 false。
  • 请给我一个代码示例,因为它对我来说根本没有意义。
  • 我现在明白你在说什么 JMG,我不敢相信它可以这么容易:) 谢谢大家,现在我可以很容易地左右移动对象了。
【解决方案2】:

因此,您需要检查已经到达 PointA 的对象,存储它们,然后检查更新的存储计数是否等于您的对象数组。然后,当满足这种情况时,您可以更改 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] );
           }
     }
}

然后是 else 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;
           }
     }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-18
    • 1970-01-01
    • 2013-10-19
    • 2014-04-03
    • 2014-10-25
    • 2012-12-13
    • 1970-01-01
    相关资源
    最近更新 更多