AS3 - 'for each'循环中的数组命中测试只适用于数组中的最后一个对象 [英] AS3 - array hit test in 'for each' loop only works properly with last object in array

查看:157
本文介绍了AS3 - 'for each'循环中的数组命中测试只适用于数组中的最后一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在AS3中构建一个平台游戏,将我的Pl​​atform类的所有实例放入一个Array中,然后为每个循环运行一个hitTestObject来检查玩家是否触摸到了其中的任何一个。



我的问题在于玩家会正确地对阵列中的每个平台进行反应(即停止下落并将其设置在平台的顶部),我只能执行跳转功能,而站在阵列中的最后一个平台。

我对ActionScript很新,所以我不知道为什么会发生这种情况 - 对于每个'循环意味着每个平台应该以相同的方式行事?一些代码当然适用于每个平台,但由于某种原因,不允许玩家跳跃。



如果有人知道这是为什么发生,可以提供一个解决方案, 我会很感激。这一直在推动我几天的墙壁。



以下是相关的代码。这个函数是在一个输入框架监听器上调用的。

pre $ private $ function collisionTestPlatforms(event:Event):void {
for每个(变种我:平台在aPlatforms){
if(player.hitTestObject(i)){
Player.touchingGround = true;
player.y = i.y - 25;
Player.yVelocity = 0;
} else {
Player.touchingGround = false;





$ b非常感谢! / p>

解决方案

与@DodgerThud的建议类似,您可以将条件 else 循环:

$ $ $ pre $私人功能collisionTestPlatforms(event:Event):void {
//默认情况下,玩家直到找到
//与其中一个平台发生碰撞为止
Player.touchingGround = false;
for each(var i:Platform in aPlatforms){
if(player.hitTestObject(i)){
Player.touchingGround = true;
player.y = i.y - 25;
Player.yVelocity = 0;
}
}
}


I'm trying to build a platform game in AS3 by placing all instances of my Platform class into an Array, then running a for each loop with a hitTestObject to check whether the player is touching any of them.

My problem is that whilst the player will react correctly to touching each of the platforms in the Array (i.e. stop falling and set y position to the top of the platform), I can only perform the jump function whilst standing on the last Platform in the Array.

I'm fairly new to ActionScript so I have no idea why this is happening - surely the 'for each' loop means that each platform should act in the same way? Some of the code certainly works for each platform, but for some reason not that which allows the player to jump.

If anyone knows why this is happening and can provide a solution, I would be very grateful. This has been driving me up the wall for several days now.

Here's the relevant code. The function is called on an enter frame listener.

private function collisionTestPlatforms (event:Event) : void {
        for each (var i:Platform in aPlatforms) {
            if (player.hitTestObject(i)) {
                Player.touchingGround = true;
                player.y = i.y - 25;
                Player.yVelocity = 0;
            } else {
                Player.touchingGround = false;
            }
        }
    }

Thank you very much!

解决方案

Similar to @DodgerThud's suggestion, you could take the conditional else out of the loop:

private function collisionTestPlatforms (event:Event) : void {
    //By default, the player is not touching the ground until we find 
    // a collision with one of the platforms
    Player.touchingGround = false;
    for each (var i:Platform in aPlatforms) {
        if (player.hitTestObject(i)) {
            Player.touchingGround = true;
            player.y = i.y - 25;
            Player.yVelocity = 0;
        }
    }
}

这篇关于AS3 - 'for each'循环中的数组命中测试只适用于数组中的最后一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆