2014-10-18 117 views
0

我有一個應用程序,我正在使用AS3爲iOS和Android開發。我目前在這個項目中遇到了幾個性能問題。這是我第一個應用程序和遊戲。所以任何類型的反饋或洞察力都非常感謝。我的比賽是一個越野自行車比賽。它允許用戶按下一個按鈕(gasPress)向上滑輪,另一個按鈕(制動器)向下滑動。一旦按下「gasPress」按鈕,布爾型「holdGas」被設置爲true。在我的「gameLoop」(Enter_Frame事件)中,自行車向上旋轉,如果「holdGas」等於true。按壓剎車的代碼是一樣的,只是它將自行車旋轉。我的建築物在屏幕上滑動,造成了自行車移動的錯覺。構建影片剪輯緩存爲位圖。我使用了同一建築物影片剪輯的兩個實例。一旦第一個剪輯從舞臺上滑落,它將放在另一個之後。在一個循環的機制大聲笑。我遇到的這個問題是,當我按下「gasPress」按鈕時,它在前輪/向上旋轉之前滯後。當它滯後時,在後臺移動的建築物將跳過。它每次都發生。所以如果你繼續點擊gasPress(這是遊戲的一部分),屏幕上的物體會不斷跳躍。它使遊戲無法播放。我已經通過完全刪除代碼來向上/向下移動自行車來測試按鈕,遊戲仍然會跳過。當氣體被按下並且自行車向上旋轉時,我注意到更多的跳過或滯後。我現在有幀速率設置爲32,它也做了同樣在24當我按下按鈕時,我的遊戲滯後

public function TheGame() { 
      //Connect to tapjoy's ad network 
      //tjClass = new TapJoyClass(); 
      //stage.scaleMode = StageScaleMode.NO_SCALE; 
      //stage.align = StageAlign.TOP_LEFT; 
      //Give game focus so the keyboard keys will work on computer 
      stage.focus = this; 
      globalStage = this.stage; 
      //Checks if ScoreData and Bike data shared object has been created, if not it creates 
      scoreInfo = SharedObject.getLocal ("ScoreData"); 
      bikeInfo = SharedObject.getLocal ("BikeData"); 
      //scoreInfo.clear(); 
      //bikeInfo.clear(); 
      mostSprocketsCollected = scoreInfo.data.mostSprocketsCollected; 
      //Handling background 
      background1 = new Background; 
      background2 = new Background; 
      addChild (background1); 
      addChild (background2); 
      background1.cacheAsBitmap = true; 
      background2.cacheAsBitmap = true; 
      background1.mouseChildren = false; 
      background1.mouseEnabled = false; 
      background2.mouseChildren = false; 
      background2.mouseEnabled = false; 
      background1.x = background1.width; 
      background2.x = background1.x + background2.width; 
      //Handling buildings 
      building1 = new Buildings; 
      building2 = new Buildings; 
      addChild (building1); 
      addChild (building2); 
      building1.cacheAsBitmap = true; 
      building2.cacheAsBitmap = true; 
      building1.mouseChildren = false; 
      building1.mouseEnabled = false; 
      building2.mouseChildren = false; 
      building2.mouseEnabled = false; 
      building1.x = building1.width; 
      building2.x = building1.x + building2.width; 
      //Choose which bike will be used 
      if(bikeInfo.data.EquippedBike == undefined){ 
       //Start off with original bike 
       whichBike = "OriginalDirtBike"; 
       bikeInfo.data.BikeColor = "OriginalDirtBike"; 
      }else{ 
       whichBike = bikeInfo.data.EquippedBike; 
      } 
      if(whichBike == "Banshee"){ 
       //Add far wheels first 
       wheelClip2 = new Banshee_Far_WheelClip; 
       addChild (wheelClip2); 
       //wheelClip2. = 208.30; 
       wheelClip2.y = 350; 
       //Add Banshee 
       bitmapBike = new Banshee_Bitmap(); 
       bitmapBike.gotoAndStop (bikeInfo.data.BikeColor); 
       addChild (bitmapBike); 
       bitmapBike.y = 399.80; 
       bitmapBike.x = 0; 
       //Add frontWheels 
       wheelClip = new Banshee_WheelClip(); 
       wheelClip.backTire.frontFace.gotoAndStop (bikeInfo.data.BikeColor); 
       wheelClip.frontTire.frontFace.gotoAndStop (bikeInfo.data.BikeColor); 
       addChild (wheelClip); 
       wheelClip.x = 187.70; 
       wheelClip.y = 399; 
      }else if(whichBike == "SupermotoDRZ400"){ 
       //Add Wheels 
       wheelClip = new WheelClip(); 
       wheelClip.backTire.gotoAndStop (bikeInfo.data.BikeColor); 
       wheelClip.frontTire.gotoAndStop (bikeInfo.data.BikeColor); 
       addChild (wheelClip); 
       wheelClip.x = 0; 
       wheelClip.y = 409.45; 
       //Add bike 
       bitmapBike = new SuperMoto_Bitmap(); 
       bitmapBike.gotoAndStop (bikeInfo.data.BikeColor); 
       addChild (bitmapBike); 
       bitmapBike.y = 399.80; 
      }else if(whichBike == "OriginalDirtBike"){ 
       //Add wheels 
       wheelClip = new OriginalBike_WheelClip(); 
       wheelClip.backTire.gotoAndStop (bikeInfo.data.BikeColor); 
       wheelClip.frontTire.gotoAndStop (bikeInfo.data.BikeColor); 
       addChild (wheelClip); 
       wheelClip.x = 0; 
       wheelClip.y = 405; 
       //Add Bike 
       bitmapBike = new OriginalBike_Bitmap(); 
       addChild (bitmapBike); 
       bitmapBike.gotoAndStop (bikeInfo.data.BikeColor); 
       bitmapBike.y = 399.80; 
       //Add rider 
       //rider = new Rider_Original_bitmap(); 
       //addChild (rider); 
       //rider.y = 353.1 
      } 
      //bitmapBike.mouseChildren = false; 
      //bitmapBike.mouseEnabled = false; 
      //bitmapBike.cacheAsBitmapMatrix = bitmapBike.transform.concatenatedMatrix; 
      //bitmapBike.cacheAsBitmap = true; 
      // 
      trace ("bitmap bike : " + getSize (bitmapBike)); 

      //rider.cacheAsBitmapMatrix = rider.transform.concatenatedMatrix; 
      //rider.cacheAsBitmap = true; 
      rider.mouseChildren = false; 
      rider.mouseEnabled = false; 

      gasPress.addEventListener (MouseEvent.MOUSE_DOWN, hitGas); 
      gasPress.addEventListener (MouseEvent.MOUSE_UP, releaseGas); 
      brake.addEventListener (MouseEvent.MOUSE_DOWN, hitBrake); 
      brake.addEventListener (MouseEvent.MOUSE_UP, releaseBrake); 
      stage.addEventListener (KeyboardEvent.KEY_DOWN, useKeyboard); 
      stage.addEventListener (KeyboardEvent.KEY_UP, stopUseKeyboard); 
      addEventListener (Event.ADDED_TO_STAGE, init); 
      ScoreBoardFormat = new TextFormat(); 
      ScoreBoardFormat.bold = true; 
      sprocketVector = new Vector.<MovieClip>(); 
      sprocketPool = new SprocketPool(Sprocket, pool_SprocketAmount); 
      stage.addEventListener (Event.RESIZE, resizeObjects); 
      stage.dispatchEvent(new Event(Event.RESIZE)); 
      if(Accelerometer.isSupported){ 
       //on mobile device 
      }else{ 
       //on desktop 
       gravity = 15; // low values for phone 
       dy = 60; // low values for phone 
       backgroundDx = -160; 
       buildingDx = -300; 
       dx = 300; // speed and direction 
      } 
      //start bike off in a wheelie 
      bitmapBike.rotation = -45; 
      rider.rotation = -45; 
      wheelClip.x = -15; 
      rider.x = bitmapBike.x; 
      //Add the first sprocket 
      addSprocket(); 
     } 

    //Following Functions are Gas and Brake related 
     public function hitGas (e:MouseEvent){ 
      e.stopImmediatePropagation(); 
      holdGas = true; 
      pressedBrake = "no"; 
     } 
     public function hitBrake (e:MouseEvent){ 
      e.stopImmediatePropagation(); 
      pressedBrake = "yes"; 
     } 
     public function releaseGas (e:MouseEvent){ 
      e.stopImmediatePropagation(); 
      holdGas = false; 
     } 
     public function releaseBrake (e:MouseEvent){ 
      e.stopImmediatePropagation(); 
      pressedBrake = "no"; 
     } 




    //Game Loop 
     public function gameLoop (e:Event):void{ 
      e.stopImmediatePropagation(); 
      var timePassed:int = getTimer()-lastTime; 
      lastTime += timePassed; 

      if (whichBike == "SupermotoDRZ400"){ 
       //Keep wheels in position with the bike 
       wheelClip.rotation = bitmapBike.rotation; 
       wheelClip.x = bitmapBike.x; 
       //make tires spin 
       wheelClip.frontTire.rotation += 75*timePassed/1000; 
       wheelClip.backTire.rotation += 75*timePassed/1000; 
       rider.rotation = bitmapBike.rotation - 1; 
       rider.x = bitmapBike.x + 25; 
      }else if (whichBike == "OriginalDirtBike"){ 
       //Keep wheels in position with the bike 
       wheelClip.rotation = bitmapBike.rotation - 1.5; 
       wheelClip.rotation = bitmapBike.rotation; 
       wheelClip.x = bitmapBike.x + 16.5; 
       //make tires spin 
       wheelClip.frontTire.rotation += 75*timePassed/1000; 
       wheelClip.backTire.rotation += 75*timePassed/1000; 
       //rider 
       rider.x = bitmapBike.x; 
       rider.rotation = bitmapBike.rotation; 
      }else if (whichBike == "Banshee"){ 
       rider.rotation = bitmapBike.rotation; 
       rider.x = bitmapBike.x; 
       //First wheels 
       wheelClip.x = bitmapBike.x; 
       wheelClip.rotation = bitmapBike.rotation; 
       wheelClip.frontTire.frontFace.rotation += 75*timePassed/1000; 
       wheelClip.frontTire.backFace.rotation += 75*timePassed/1000; 
       wheelClip.backTire.frontFace.rotation += 75*timePassed/1000; 
       wheelClip.backTire.backFace.rotation += 75*timePassed/1000; 
       //second wheels 
       wheelClip2.x = bitmapBike.x; 
       wheelClip2.rotation = bitmapBike.rotation; 
       wheelClip2.farBackTire.frontFace.rotation += 75*timePassed/1000; 
       wheelClip2.farFrontTire.frontFace.rotation += 75*timePassed/1000; 
       wheelClip2.farFrontTire.backFace.rotation += 75*timePassed/1000; 
      } 

      if(popUp != null){ 
       this.setChildIndex (popUp, this.numChildren - 2); 
      } 
      this.setChildIndex (rider, this.numChildren - 1); 
      //Stores the bikes current rotation point 
      var currRotation = bitmapBike.rotation; 

      //make shadow follow bike 
      var bikeX:int = bitmapBike.x; 
      newShadow.x = bikeX + 150; //109.8; 
      //ANITMATING THE DRIVER!!!! 
      if(currRotation <= -11){ //This should occur at a lower 
       rider.gotoAndPlay ("sitDown"); 
       sitDown = true; 
       newShadow.gotoAndStop ("fullShadow"); 
      } 
      if(currRotation <= -15){ 
       newShadow.gotoAndStop("three4thShadow"); 
       leanBack = true; 
      } 
      //just change shadow with this one 
      if(currRotation <= -25){ 
       newShadow.gotoAndStop ("midShadow"); 
      } 
      if(currRotation <= -30){ 
       rider.gotoAndPlay ("LeanBack"); 
       leanBack = true; 
       newShadow.gotoAndStop ("smallShadow"); 
      } 
      if(currRotation <= -34){ //Highest 
       rider.gotoAndPlay ("LeanBack2"); 
       leanBack = true; 
       newShadow.gotoAndStop ("smallestShadow"); 
      } 
      if(currRotation <= -35){ //Highest 
       rider.gotoAndPlay ("LeanBack2"); 
       leanBack = true; 
       newShadow.gotoAndStop ("allTheWayBack"); 
      } 
      //if player holds or hit the brake 
      if(pressedBrake == "yes") { //for some reason the opposite is working right now 
       var currBikePosition:int; 
       currBikePosition = bitmapBike.rotation; 
       bitmapBike.rotation += 2; 
       currBikePosition = 0; 
      } 
      //gravity pulling bike down 
      bitmapBike.rotation += gravity*timePassed/1000; 
      bitmapBike.rotation += gravity*timePassed/1000; 
      //if player holding gas, do wheeliez 
      if(holdGas == true){ 
       //Make bike wheelie 
       bitmapBike.rotation -= dy*timePassed/1000; 
       rider.rotation -= dy*timePassed/1000; 
      } 
      //Move Bike to center, then move buildings 
      if(bitmapBike.x <=187){ 
       //move bike 
       bitmapBike.x += dx*timePassed/1000; 
      }else{ 
       //move buildings and background 
       building1.x += buildingDx*timePassed/1000; 
       building2.x += buildingDx*timePassed/1000; 
       //buildings 
       if(building1.x <= -25){ 
        building1.x = building2.x + building1.width; 
       }else if(building2.x <= -25){ 
        building2.x = building1.x + building2.width; 
       } 
       background1.x += backgroundDx*timePassed/1000; 
       background2.x += backgroundDx*timePassed/1000; 
       //background 
       if(background1.x <=0){ 
        background1.x = background2.x + background1.width; 
       }else if (background2.x <=0){ 
        background2.x = background1.x + background2.width; 
       } 
      } 
+0

如果我的理解它是正確的,即使沒有氣體,你也經歷了滯後?我最好的猜測是你對待移動背景的方式。嘗試刪除背景正在移動,然後嘗試徹底刪除背景,看看會發生什麼,然後回報:)通常,您的代碼沒有任何明顯的錯誤。您可能需要刪除「cacheAsBitmap」,因爲這會導致額外的工作,除非您有一個複雜的形狀。這看起來像一個「試驗和錯誤」的方法,所以只是嘗試刪除一些東西,看看它是否正常工作 – 2014-10-19 00:11:11

+0

@DanielMesSer謝謝你的幫助。遊戲實際上運行非常平穩,直到按下這些按鈕。 (包括建築運動)它然後滯後。在按下按鈕之前,我的口吃會有點週期性,但沒有任何不利之處。絕對不會像按下按鈕時那樣接近。我把建築物拿出來,遊戲看起來很棒。我將嘗試移動他們使用+ =而不是時間增加x,看看它是如何工作的。你能想到的其他方法嗎? – rtpenick 2014-10-19 01:53:08

+0

@DanielMesSer使用+ =移動建築物x =原來是最差的10倍。當按下按鈕時,所有東西都會靜止不動。使用+ =方法取決於幀速率。如果談到一個立場仍然即時猜測按鈕正在減慢幀速率 – rtpenick 2014-10-19 02:10:51

回答

0

你是如何處理的「背景圖片」的遮蔽?形象有多大?

最有可能的原因是您的自行車在移動時不得不重新繪製所有內容,因爲它無法再使用緩存位圖的舊值。而且由於你的背景很大,這會導致你注意到的性能下降。

作爲開始,你應該定義一個scrollRect的的「巨大的運動背景」,而不是僅僅繪製整個背景。這隻會導致顯示的圖像部分被髮送到GPU。如bg.scrollRect = new Rectangle(offsetX,offsetY, 1280, 1024);

閱讀http://gskinner.com/blog/archives/2006/11/understanding_d.html對scrollRect的簡短介紹。

CacheAsBitmap可能沒有做你想做的事情,如果你改變每一幀的displayobject的外觀,你實際上會失去性能,因爲你首先必須繪製項目,然後將它們放入一個位圖,然後繪製該位圖。然後在下一幀中,最終重做完全相同的東西(因爲舊的緩存位圖不再有效)。 cacheAsBitmap對於在複雜的矢量形狀對於一定數量的幀保持不變的情況下非常有用。

爲了更全面的優化指南,請閱讀以下兩個巨大的PDF文件: http://help.adobe.com/en_US/as3/mobile/flashplatform_optimizing_content.pdf http://help.adobe.com/en_US/as3/dev/as3_devguide.pdf

+0

我認爲這是幀率。我使用Adobe Scout測試了這款遊戲,並注意到每次鼠標停止事件觸發時,幀頻下降的幅度都很大。 IT人員會繼續保持其設定的速度,直到再次點擊該按鈕。我將鼠標按下事件切換爲鍵盤事件,並且使用按鍵而不是按鈕完成遊戲。 (即使在電腦上,我可以看到按鈕被按下時的滯後)。當我使用鍵盤事件Scout顯示我的幀速率仍然在設定的速率。我還使用加速計移動自行車。仍然沒有滯後。 – rtpenick 2014-10-20 09:43:57

+0

我甚至將我的顯示對象的鼠標屬性設置爲false。這也沒有幫助 – rtpenick 2014-10-20 09:50:11

+0

我發現了一個很好的EASY Fix,我的遊戲只是爲了確保將所有的矢量圖形符號緩存爲位圖,而對我來說這個大修正是當我將SWF發佈設置,硬件加速設置爲GPU和我的Android清單 gpu這個工作真棒,並把我的大型圖形非常滯後的遊戲變成一個快速和平穩的運行遊戲。 – 2014-10-21 07:35:45

0
在發佈設置設爲您的硬件加速GPU

,將瘦掉處理器

相關問題