【问题标题】:AndEngine : Showing All sprites images sameAndEngine:显示所有精灵图像相同
【发布时间】:2015-07-01 09:38:23
【问题描述】:

enter image description here我有 4 个精灵。我将所有 4 个精灵放在移动屏幕上的不同位置。但它正在使用最后添加的精灵图像(sprite4.png)所有精灵图像(sprite1.png、sprite2.png、sprite3.png 和 sprite4.png)。 简而言之,它显示所有精灵图像相同。 代码如下,请分享经验

// the below code is running without error but displaying same images in all the sprites
package com.example.test1;

import org.andengine.engine.camera.Camera;
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.util.FPSLogger;
import org.andengine.input.touch.TouchEvent;
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;

/*;
 * (c) 2010 Nicolas Gramlich
 * (c) 2011 Zynga
 *
 * @author Nicolas Gramlich
 * @since 15:13:46 - 15.06.2010
 */
public class Puzzle extends GameActivity {
    // ===========================================================
    // Constants
    // ===========================================================

    private static final int CAMERA_WIDTH = 720;
    private static final int CAMERA_HEIGHT = 480;
    final Scene scene = new Scene();

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

    private BitmapTextureAtlas mBitmapTextureAtlas;
    private ITextureRegion spriteTextureRegion1;
    private ITextureRegion spriteTextureRegion2;
    private ITextureRegion spriteTextureRegion3;
    private ITextureRegion spriteTextureRegion4;


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

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

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

    @Override
    public EngineOptions onCreateEngineOptions() {
    //  Toast.makeText(this, "Touch & Drag the face!", Toast.LENGTH_LONG).show();

        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(), 96, 96, TextureOptions.BILINEAR);
        this.spriteTextureRegion1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "sprite1.png", 0, 0);
        this.spriteTextureRegion2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "sprite2.png", 0, 0);
        this.spriteTextureRegion3 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "sprite3.png", 0, 0);
        this.spriteTextureRegion4= BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "sprite4.png", 0, 0);   


    //  scene.attachChild(sprite1);  

        this.mBitmapTextureAtlas.load();
    }

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


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

        /* set sprite1 positions on screen */
        final float sprite1_posX = (40);  
        final float sprite1_posY = (40);
        final Sprite sprite1 = new Sprite(sprite1_posX, sprite1_posY, this.spriteTextureRegion1, this.getVertexBufferObjectManager()) {
            @Override
            public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
                this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2, pSceneTouchEvent.getY() - this.getHeight() / 2);
                return true;
            }
        };
        //sprite1.setScale(3);
        scene.attachChild(sprite1);
        scene.registerTouchArea(sprite1);
        scene.setTouchAreaBindingOnActionDownEnabled(true);

        /* set sprite2 positions on screen  */
        final Sprite sprite2 = new Sprite(sprite1_posX, sprite1_posY + (sprite1.getHeight()*3 + 10), this.spriteTextureRegion2, this.getVertexBufferObjectManager()) {
            @Override
            public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
                this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2, pSceneTouchEvent.getY() - this.getHeight() / 2);
                return true;
            }
        }; 
        //sprite2.setScale(3);
        scene.attachChild(sprite2);
        scene.registerTouchArea(sprite2);
        scene.setTouchAreaBindingOnActionDownEnabled(true); 

         /* set sprite3 positions on screen  */
        final Sprite sprite3 = new Sprite(sprite1_posX, sprite1_posY + (sprite1.getHeight() + 20), this.spriteTextureRegion3, this.getVertexBufferObjectManager()) {
            @Override
            public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
                this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2, pSceneTouchEvent.getY() - this.getHeight() / 2);
                return true;
            }
        };
    //  sprite3.setScale(3);
        scene.attachChild(sprite3);
        scene.registerTouchArea(sprite3);
        scene.setTouchAreaBindingOnActionDownEnabled(true);


    /*  set sprite3 positions on screen  */
    final Sprite sprite4 = new Sprite(sprite1_posX, sprite1_posY + (sprite1.getHeight() + 30), this.spriteTextureRegion4, this.getVertexBufferObjectManager()) {
        @Override
        public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
            this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2, pSceneTouchEvent.getY() - this.getHeight() / 2);
            return true;
        }
    };
//  sprite4.setScale(3);
    scene.attachChild(sprite4);
    scene.registerTouchArea(sprite4);
    scene.setTouchAreaBindingOnActionDownEnabled(true);

    return scene;
}
}

【问题讨论】:

  • 能否请您重新表述您的问题? A)所有图片都位于同一个地方吗? B)它们的大小是否相同,而它们不应该相同? C还有什么问题吗?

标签: andengine


【解决方案1】:

抱歉,我知道是什么问题:

    this.spriteTextureRegion1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "sprite1.png", 0, 0);
    this.spriteTextureRegion2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "sprite2.png", 0, 0);
    this.spriteTextureRegion3 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "sprite3.png", 0, 0);
    this.spriteTextureRegion4= BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "sprite4.png", 0, 0);

最后,你在哪里放置 0, 0 - 你声明你的区域应该在纹理图集中的位置。想象一下你贴在纸上的贴纸。你放一个,然后你放另一个,第三个和第四个。这样你只能看到最上面的那个。所以你必须把你的区域放在纹理图集的不同位置。如果您的所有图像都是 40x40,它应该看起来像这样(您的图集也可能更大(512x512):

this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 512, 512, TextureOptions.BILINEAR);

    this.spriteTextureRegion1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "sprite1.png", 0, 0);
    this.spriteTextureRegion2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "sprite2.png", 50, 0);
    this.spriteTextureRegion3 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "sprite3.png", 0, 50);
    this.spriteTextureRegion4= BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "sprite4.png", 50, 50);

【讨论】:

  • 精灵的位置在 (0,0) 下很好,但我的问题如下所示
  • 我在我的问题中添加了问题图像,它对所有四个精灵使用相同的图片,而我想要不同的地方
  • 是的,问题在于您的 ITextureRegions 在纹理图集中的定位。这与将精灵放置在场景中不同。请像我向您展示的那样更改它(查看行尾的数字),它会很好。
  • Motyczka -- 太棒了!老大,问题解决了。非常感谢。我认为您是可以帮助我开发我的第一个 andEngine 游戏的人。我需要你的帮助,我正在开发一个 android 游戏,用户将获得图像或风景,他/她只需将这些碎片拖到正确的位置即可完成图片,请建议我是否朝着正确的方向前进或者我需要使用不同的方法来实现这一点
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多