2015-09-24 28 views
1

我想創建一個二維遊戲,其中一輛汽車沿着不規則形狀的山丘的邊緣移動。我想使用基本的as3(沒有像Box2d或Nape這樣的物理引擎)。通過大量的研究,我發現this這正是我想要的東西,但只有邏輯和沒有源代碼。有人可以用一段可以在as3中執行此操作的代碼來幫助我嗎?還建議是否有更好的選擇來獲得所需的輸出。as3非均勻地形的坡度檢測

回答

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; 
     } 
    } 

最後階段對移動運行此功能

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 
       } 
      } 
     } 
    } 

抱歉,我不能人物的ENTER_FRAME監聽器監聽後續提供任何個人經驗,如果您想要更多,那麼我上面鏈接的文章將提供更多信息。