【问题标题】:as3 slope detection for non uniform terrains非均匀地形的 as3 坡度检测
【发布时间】:2015-09-24 05:11:15
【问题描述】:

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

【问题讨论】:

    标签: actionscript-3 flash terrain


    【解决方案1】:

    我自己对这样的代码知之甚少,我无法发表评论,因为我的声誉很短 7 :'( 但您链接的文章确实有另一个指向蠕虫式程序的链接可能对您有帮助的代码。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
                    }
                }
            }
        }
    

    对不起,我无法提供任何个人经验,如果您想要更多,那么我上面链接的文章将提供更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-02
      • 2015-10-01
      • 1970-01-01
      • 2019-12-29
      相关资源
      最近更新 更多