2012-09-19 31 views
5

मैं दो डिवाइसों के साथ एंडींजिन, शाखा जीएलएस 2 के अंतिम संस्करण का उपयोग कर रहा हूं: एचटीसी डिजायर और गैलेक्सी नेक्सस।एंडइंजिन - स्प्राइट ग्रुप नए अटैचमेंट पर ब्लिंक

स्क्रीन पर स्क्रॉल करने वाले स्प्राइट्स के साथ स्प्राइट समूह का उपयोग करते समय मुझे कोई समस्या है। स्क्रीन के शीर्ष पर एक स्प्राइट समूह से नए स्प्राइट संलग्न होते हैं और जब वे नीचे से निकलते हैं तो अलग हो जाते हैं। और मैं बहुत मेमोरी का उपयोग करने से बचने के लिए पूल का उपयोग करता हूं।

जैसे ही कुछ स्प्राइट अलग हो जाते हैं, कुछ नए संलग्न स्प्राइट कुछ फ्रेम के लिए यादृच्छिक रूप से झपकी लगने लगते हैं। यह बहुत परेशान है और मुझे नहीं पता कि क्यों ...

मैंने रीसाइक्लिंग करते समय स्पिइट्स को स्पष्ट (गलत) सेट करने की कोशिश की, मैंने पूल के बिना भी कोशिश की लेकिन यह कोई चीज़ नहीं बदली।

मुझे लगता है कि स्प्राइट समूह में एक बग हो सकता है, लेकिन यह सुनिश्चित नहीं है कि कहां है। मैंने स्प्रिट ग्रुप में स्प्राइट ग्रुप में बच्चों को संलग्न करने की कोशिश की ताकि यह सुनिश्चित किया जा सके कि यह बिना किसी किस्मत केUpUpateSpriteBatch() के लूप के दौरान नहीं हुआ है।

एंडइंजिनएक्समल्स प्रोजेक्ट पर आधारित एक उदाहरण यहां दिया गया है। समस्या को देखने के लिए आप प्रोजेक्ट लॉन्च कर सकते हैं, प्रोजेक्ट लॉन्च कर सकते हैं और प्रोजेक्ट लॉन्च कर सकते हैं और स्प्रिटबैच को सरल/ड्रॉइंग कर सकते हैं।

किसी भी विचार के लिए अग्रिम धन्यवाद!

package org.andengine.examples; 

import java.util.Iterator; 

import org.andengine.engine.camera.Camera; 
import org.andengine.engine.handler.timer.ITimerCallback; 
import org.andengine.engine.handler.timer.TimerHandler; 
import org.andengine.engine.options.EngineOptions; 
import org.andengine.engine.options.ScreenOrientation; 
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy; 
import org.andengine.entity.scene.Scene; 
import org.andengine.entity.scene.background.Background; 
import org.andengine.entity.sprite.Sprite; 
import org.andengine.entity.sprite.batch.SpriteGroup; 
import org.andengine.entity.util.FPSLogger; 
import org.andengine.opengl.texture.TextureOptions; 
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas; 
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory; 
import org.andengine.opengl.texture.region.ITextureRegion; 
import org.andengine.opengl.vbo.VertexBufferObjectManager; 
import org.andengine.ui.activity.SimpleBaseGameActivity; 
import org.andengine.util.adt.list.SmartList; 
import org.andengine.util.adt.pool.GenericPool; 

public class SpriteBatchExample extends SimpleBaseGameActivity { 
    // =========================================================== 
    // Constants 
    // =========================================================== 

    private static final int CAMERA_WIDTH = 720; 
    private static final int CAMERA_HEIGHT = 480; 

    // =========================================================== 
    // Fields 
    // =========================================================== 

    private BitmapTextureAtlas mBitmapTextureAtlas; 
    private ITextureRegion mFaceTextureRegion; 

    private float mSecondsElapsedSinceLastGeneration = 0; 

    // =========================================================== 
    // Constructors 
    // =========================================================== 

    // =========================================================== 
    // Getter & Setter 
    // =========================================================== 

    // =========================================================== 
    // Methods for/from SuperClass/Interfaces 
    // =========================================================== 

    @Override 
    public EngineOptions onCreateEngineOptions() { 
     final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT); 

     return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera); 
    } 

    @Override 
    public void onCreateResources() { 
     BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/"); 

     this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 32, 32, TextureOptions.BILINEAR); 
     this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "face_box.png", 0, 0); 
     this.mBitmapTextureAtlas.load(); 
    } 

    @Override 
    public Scene onCreateScene() { 
     this.mEngine.registerUpdateHandler(new FPSLogger()); 

     final Scene scene = new Scene(); 
     scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f)); 

     final SpriteGroup spriteGroup = new SpriteGroup(this.mBitmapTextureAtlas, 500, this.getVertexBufferObjectManager()); 
     spriteGroup.setPosition(0, 0); 
     scene.attachChild(spriteGroup); 

     final SpritePool lPool = new SpritePool(mFaceTextureRegion, getVertexBufferObjectManager()); 
     final SmartList<Sprite> lSpriteList = new SmartList<Sprite>(); 

     final float lCharactersPeriod = 0.4f; 

     scene.registerUpdateHandler(new TimerHandler(0.05f, true, new ITimerCallback() { 
      @Override 
      public void onTimePassed(final TimerHandler pTimerHandler) { 
       final float lSecondsElapsedSinceLastUpdate = 0.1f; 

       final Iterator<Sprite> li = lSpriteList.iterator(); 
       while (li.hasNext()) { 
        final Sprite lChar = li.next(); 
        boolean lRemoveChar = false; 

        // Character destruction OR movement 
        final float lY = lChar.getY(); 
        if (lY > CAMERA_HEIGHT) { 
         lRemoveChar = true; 
        } else { 
         lChar.setPosition(lChar.getX(), lY + 60 * lSecondsElapsedSinceLastUpdate); 
        } 

        if (lRemoveChar) { 
         // Remove character from scene 
         lChar.detachSelf(); 
         lPool.recyclePoolItem(lChar); 
         li.remove(); 
        } 
       } 

       // Character generation 
       mSecondsElapsedSinceLastGeneration += lSecondsElapsedSinceLastUpdate; 
       if (mSecondsElapsedSinceLastGeneration > lCharactersPeriod) { 
        // generate sprite 
        final Sprite lSprite = lPool.obtainPoolItem(); 
        lSprite.setPosition((float) Math.random() * CAMERA_WIDTH, 0); 
        spriteGroup.attachChild(lSprite); 
        lSpriteList.add(lSprite); 
        mSecondsElapsedSinceLastGeneration -= lCharactersPeriod; 
       } 
      } 
     })); 

     return scene; 
    } 

    // =========================================================== 
    // Methods 
    // =========================================================== 

    // =========================================================== 
    // Inner and Anonymous Classes 
    // =========================================================== 
    static class SpritePool extends GenericPool<Sprite> { 
     // =========================================================== 
     // Constants 
     // =========================================================== 

     // =========================================================== 
     // Fields 
     // =========================================================== 
     private final VertexBufferObjectManager mVertexBufferObjectManager; 
     private ITextureRegion mFaceTextureRegion; 


     // =========================================================== 
     // Constructors 
     // =========================================================== 
     public SpritePool(final ITextureRegion pFaceTextureRegion, final VertexBufferObjectManager pVertexBufferObjectManager) { 
      mFaceTextureRegion = pFaceTextureRegion; 
      mVertexBufferObjectManager = pVertexBufferObjectManager; 
     } 


     // =========================================================== 
     // Methods for/from SuperClass/Interfaces 
     // =========================================================== 
     @Override 
     protected Sprite onAllocatePoolItem() { 
      final Sprite lSprite = new Sprite(50, 0, mFaceTextureRegion, mVertexBufferObjectManager); 
      lSprite.setIgnoreUpdate(true); 
      return lSprite; 
     } 

     @Override 
     protected void onHandleRecycleItem(final Sprite pSprite) { 
     } 

     @Override 
     protected void onHandleObtainItem(final Sprite pSprite) { 
     } 
    } 
} 

उत्तर

3

मैं एक ही मुद्दा (पिछले समूह में दिखाई दे रहा स्प्राइट (रों) जोड़ा निमिष जब भी मैं अदृश्य के लिए किसी भी स्प्राइट सेट शुरू कर दिया) था, और SpriteGroup में उन 2 तरीकों ओवरराइट करके इसे हल:

SpriteGroup result = new SpriteGroup(atlas, capacity, vertexBufferObjectManager) { 
    @Override 
    protected boolean onUpdateSpriteBatch() { 
      return false; 
    } 

    @Override 
    protected void onManagedUpdate(float pSecondsElapsed) { 
      super.onManagedUpdate(pSecondsElapsed); 
      final SmartList<IEntity> children = this.mChildren; 
      if(children != null) { 
        final int childCount = children.size(); 
        for(int i = 0; i < childCount; i++) { 
          this.drawWithoutChecks((Sprite)children.get(i)); 
        } 
        submit(); 
      } 
    }; 

स्रोत: http://www.andengine.org/forums/gles2/blinking-last-sprite-in-spritegroup-t7617.html