2013-02-26 73 views
1

我一直在用AndEngine做一些Android编程,并且遇到了一些使用Hashtables的奇怪的东西。AndEngine的Java Hashtable

基本上,如果我这样做:

 BitmapTextureAtlas m_textureAtlas = new BitmapTextureAtlas(p_baseActivity.getTextureManager(), 1024, 1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA); 

    TextureRegion texture1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(m_textureAtlas, p_baseActivity, "Chrysanthemum.jpg", 0, 0); 

    m_textureAtlas.load(); 

    Sprite m_sprite1 = new Sprite(0, 0, texture1, p_baseActivity.getEngine().getVertexBufferObjectManager()); 
    this.attachChild(m_sprite1); 

一切的话罚款。但如果我这样做:

 BitmapTextureAtlas m_textureAtlas = new BitmapTextureAtlas(p_baseActivity.getTextureManager(), 1024, 1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA); 

    Hashtable<String, TextureRegion> test = new Hashtable<String, TextureRegion>(); 
    TextureRegion texture1 = test.put("1", BitmapTextureAtlasTextureRegionFactory.createFromAsset(m_textureAtlas, p_baseActivity, "Chrysanthemum.jpg", 0, 0)); 

    m_textureAtlas.load(); 

    Sprite m_sprite1 = new Sprite(0, 0, texture1, p_baseActivity.getEngine().getVertexBufferObjectManager()); 
    this.attachChild(m_sprite1); 

它(图像)闪烁,尺寸都是错误的。现在我可以为这个项目做第一套代码,但我不确定我是否做错了什么,或者我应该避免put()的返回值。

回答

2

Hashtable#put返回此散列表中指定键的先前值,如果没有,则返回null。在你的情况下,它是空的,因为你刚刚创建实例。

即使从示例中也不难理解为什么需要散列表。

Hashtable<String, TextureRegion> test = new Hashtable<String, TextureRegion>(); 
    TextureRegion texture1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(m_textureAtlas, p_baseActivity, "Chrysanthemum.jpg", 0, 0); 
    test.put("1", texture1); 
+0

owww我明白了!非常感谢。 – 2013-02-26 09:27:09