我希望游戏中的角色在平台上弹跳时向上移动。为此,我计算了每个平台与角色之间的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;
}
all和any之间的区别。
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;
}