如何查看数组中的每个值是否都小于某个数字?

问题描述 投票:0回答:1

我希望游戏中的角色在平台上弹跳时向上移动。为此,我计算了每个平台与角色之间的x和y距离,然后将这些值放在两个数组中:一个用于x距离,一个用于它们之间的y距离。如果角色在平台上,我希望它向上移动。如何确定数组中的值是否小于某个数字?我知道我的代码效率很低,但是我也不知道如何改善它。

    function loop(e:Event):void{

var plats:Array = new Array();
//adding platforms to array
  plats.push(plat1);
  plats.push(plat2);

//calculating distance between platform and character

var distx1 = Math.sqrt((doodler.x - plat1.x)*(doodler.x - plat1.x));
var disty1 = Math.sqrt(((doodler.y + 50) - plat1.y)*((doodler.y + 50) - plat1.y));
var distx2 = Math.sqrt((doodler.x - plat2.x)*(doodler.x - plat2.x));
var disty2 = Math.sqrt(((doodler.y + 50) - plat2.y)*((doodler.y + 50) - plat2.y));

//adding distance calculations to distance arrays
var disx:Array = new Array();
disx.push(distx1);
disx.push(distx2);

var disy:Array = new Array();
disy.push(disty1);
disy.push(disty2);

  for (var i:int = 0; i < disx.length; i++) {
   //this is where i'm confused
   if(disx[i] < 65 && disy[i] < 3){

      doodler.y -= 100;

       }
arrays actionscript-3 flash bounce
1个回答
0
投票

allany之间的区别。

var aFlag:Boolean = true;

for (var i:int = 0; i < disx.length; i++)
{
    aFlag &&= disx[i] < 65;
    aFlag &&= disy[i] < 3;
}

// At this point, aFlag is true ONLY if
// all the conditions are true at the same time.

if (aFlag)
{
    doodler.y -= 100;
}
© www.soinside.com 2019 - 2024. All rights reserved.