AndEngine:显示所有精灵图像相同

4

图片描述我有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)还有其他问题吗? - Łukasz Motyczka
1个回答

2
抱歉,我知道问题出在哪里:
    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)处很好,但我的问题如下所示。 - SMT
我已在我的问题中添加了问题图像,它正在为所有四个精灵使用相同的图片,而我希望有不同的图片。 - SMT
是的,问题在于您的ITextureRegions在纹理图集中的定位方式与在场景中放置精灵的方式不同。请按照我向您展示的更改方式进行更改(请注意行末的数字),这样就没问题了。 - Łukasz Motyczka
Motyczka -- 太好了!老板,问题解决了。非常感谢你。我认为你是可以帮助我开发我的第一个andEngine游戏的人。我需要你的帮助,我正在开发一款安卓游戏,用户将会得到一些图像或者场景的碎片,他们只需要把这些碎片拖到正确的位置上就能完成图片。请建议我是否走在正确的方向上,或者我需要使用不同的方法来实现这个功能。 - SMT

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接