2014-03-13 49 views
0

我試圖在Starling中設置assetManager。但我得到的運行時錯誤:AssetManager starling錯誤

Texture cannot be null at starling.display::Image()[C:\lars\starling\starling\src\starling\display\Image.as:70] at Game()[C:\lars\onkel_reje_air_3\src\Game.as:41] at starling.core::Starling/initializeRoot()[C:\lars\starling\starling\src\starling\core\Starling.as:338] at starling.core::Starling/initialize()[C:\lars\starling\starling\src\starling\core\Starling.as:314] at starling.core::Starling/onContextCreated()[C:\lars\starling\starling\src\starling\core\Starling.as:519]

我有以下EmbeddedAssets.as:

package 
{ 
     import starling.display.Image; 
    import starling.textures.Texture; 
    /** 
    * ... 
    * @author Lars Gregersen 
    */ 
    public class EmbeddedAssets 
    { 

     [Embed(source="assets/bord.jpg")] 
     public static const bord:Class; 

    } 

} 

在我game.as:

package 
{ 
import starling.display.Sprite; 
import starling.display.MovieClip; 
import starling.display.Image; 
import starling.events.Event; 
import starling.text.TextField; 
import starling.textures.Texture; 
import starling.utils.AssetManager; 


    /** 
    * ... 
    * @author Lars Gregersen 
    */ 
    public class Game extends Sprite 
    { 

public var assets:AssetManager = new AssetManager(); 

public function Game(){ 


    assets.verbose = true; 
    assets.enqueue(EmbeddedAssets); 



     var backgroundImage:Image = new Image(assets.getTexture("bord")); 
     addChild(backgroundImage); 


     assets.loadQueue(function(ratio:Number):void{ 
    trace("Loading assets, progress:", ratio); 

    if (ratio == 1.0) 

}); 


     }//end constructor 




    }//end class 

}//end package 

而且我Main.as:

package 
{ 
    //import flash.desktop.NativeApplication; 
    import flash.events.Event; 
    import flash.display.Sprite; 
    import starling.core.Starling; 



    /** 
    * ... 
    * @author Lars Gregersen 
    */ 
    public class Main extends Sprite 
    { 
     private var _starling:Starling; 
     public function Main():void 
     { 
     _starling = new Starling(Game, stage); 
     _starling.showStats = true; 

     _starling.start(); 

     } 



    } 

} 

我有我的資產文件夾和.as fi在同一個文件夾(src)裏。使用Flashdevelop。它應該是簡單的,但...

回答

0

我認爲你需要一個步驟,你的資產類別應返回紋理的bitmapData:

VAR CLS:CLASS = getDefinitionByName( 'BORD')爲類; var bm:BitmapData = new cls(0,0);

3

這是因爲您太早調用方法'getTexture'。僅在資產已加載時調用此方法。

public function Game(){ 

    assets.verbose = true; 
    assets.enqueue(EmbeddedAssets); 

    assets.loadQueue(function(ratio:Number):void{ 

     trace("Loading assets, progress:", ratio); 
     if (ratio == 1.0) 
     { 
      var backgroundImage:Image = new Image(assets.getTexture("bord")); 
      addChild(backgroundImage); 
     } 

    }); 

}//end constructor