as3斜率检测非均匀地形

时间:2015-09-24 05:11:16

标签: actionscript-3 flash terrain

我想创建一个2D游戏,其中汽车沿着不规则形状的山丘的边缘移动。我想使用基本的as3(没有像Box2d或Nape这样的物理引擎)。通过大量的研究,我发现了this这就是我想要的东西,但只有逻辑和没有源代码。有人可以帮助我使用一段可以在as3中执行此操作的代码吗?还建议是否有更好的替代方案来获得所需的输出。

1 个答案:

答案 0 :(得分:0)

我自己对这样的代码知之甚少,而且我无法发表评论,因为我的名声很短:'(但你链接的文章确实有一个与蠕虫式程序的另一个链接可能对您有帮助的代码。Here's指向该网站的链接,我还会提供一些适当的代码,您可以查看它是否对您有用。

所以这个程序有一个监听器,如果任何键关闭,如果它是你的程序正在使用的键(移动,跳跃的空间或其他),则将布尔值设置为true,该监听器看起来像这样

    public function key_down(e:KeyboardEvent) {
        if (e.keyCode==37) {
            left_key=true;
        }
        if (e.keyCode==39) {
            right_key=true;
        }
        if (e.keyCode==32) {
            space_key=true;
        }
    }

然后它会在释放密钥时有一个后续的监听器

    public function key_up(e:KeyboardEvent) {
        if (e.keyCode==37) {
            left_key=false;
        }
        if (e.keyCode==39) {
            right_key=false;
        }
        if (e.keyCode==32) {
            space_key=false;
        }
    }

最后,舞台上有一个ENTER_FRAME侦听器,用于移动运行此功能的角色

    public function move_character(e:Event) {
        //If left key is pressed, we'll move the character to the left
        if (left_key) {
            for (i=0; i<3; i++) {//Do you remember when we made the character fall? We had to move the character pixel by pixel
                if (! terrain_bmpd.hitTest(new Point(terrain_bmp.x,terrain_bmp.y),0x01,new Rectangle(character.x-6,character.y-10,1,17))) {
                    character.x--;  /*If the character doesn't hit the ground, we can move left. However,
                                    the character may be sunk under the ground. We have to lift it*/
                    while (terrain_bmpd.hitTest(new Point(terrain_bmp.x,terrain_bmp.y),0x01,new Rectangle(character.x-5,character.y+9,10,1))) {
                        character.y--;
                    }
                }
            }
        }
        if (right_key) {//Well, that's the same for the right key
            for (i=0; i<3; i++) {
                if (! terrain_bmpd.hitTest(new Point(terrain_bmp.x,terrain_bmp.y),0x01,new Rectangle(character.x+5,character.y-10,1,17))) {
                    character.x++;
                    while (terrain_bmpd.hitTest(new Point(terrain_bmp.x,terrain_bmp.y),0x01,new Rectangle(character.x-5,character.y+9,10,1))) {
                        character.y--;
                    }
                }
            }
        }
        if (space_key&&! jumping) {//That's easy: if he isn't jumping and you press space, his speed will be negative and he'll jump
            character_speed=-10;
            jumping=true;//Now the character can't jump again
        }

        character_speed++;//Every frame we will increase character's speed
        if (character_speed>0) {
            //If the speed is positive, we will check a collision between the terrain and the rectangle below the character
            for (i=0; i<character_speed; i++) {//We check the collision pixel by pixel...
                if (! terrain_bmpd.hitTest(new Point(terrain_bmp.x,terrain_bmp.y),0x01,new Rectangle(character.x-5,character.y+9,10,1))) {
                    character.y++;//If there isn't a collision, the character will fall
                } else {
                    jumping=false;//If there's a collision with the ground, the character isn't jumping
                    character_speed=0;//The speed is 0, because the character hit the ground
                }
            }
        } else {
            for (i=0; i<Math.abs(character_speed); i++) {//If the speed is negative, the for loop won't work. We have to use Math.abs().
            //Now we will check the collision between the terrain and the rectangle above the character
                if (! terrain_bmpd.hitTest(new Point(terrain_bmp.x,terrain_bmp.y),0x01,new Rectangle(character.x-5,character.y-10,10,1))) {
                    character.y--;
                } else {
                    character_speed=0;//Well, that's the same: the character hit the ground
                }
            }
        }
    }

抱歉,我无法提供任何个人经验,如果您需要更多,我上面链接的文章将提供更多信息。