4

enter image description hereI have 4 sprites. I am placing all 4 sprites in different place on the mobile screen. But it is taking the last added sprite image that is (sprite4.png) all the sprites images(sprite1.png, sprite2.png, sprite3.png and sprite4.png). In short its displaying all the sprite images same. The code is given below, please share your experience

// 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;
}
}
SMT
  • 469
  • 1
  • 5
  • 17
  • Could you please rephrase your problem? A) are all pictures positioned in the same place? B) Are they same size while they should not be? C any other problem? – Łukasz Motyczka Jul 01 '15 at 12:01

1 Answers1

2

Oh sorry I know what is the problem:

    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);

At the end, where you put 0, 0 - you declare where your region should be located on the Texture atlas. Imagine stickers you put on the sheet of paper. You put one, then on this you put another, and third and fourth. This way you can see only the one that is on top. So you have to put your regions in different places of the texture atlas. If all your images are 40x40 it should look like this (also your atlas might be bigger (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);
Łukasz Motyczka
  • 1,169
  • 2
  • 13
  • 35
  • The position of the sprites are fine with (0,0), but my problem is shown below – SMT Jul 02 '15 at 03:55
  • I have added the problem image in my question that its using same picture for all four sprites where as i want different – SMT Jul 02 '15 at 04:02
  • Yeah, the problem is in positioning of your ITextureRegions in the Texture atlas. It is not the same as placing the sprite in the scene. Please change it like I have shown you (look at the numbers in the end of lines) and it will be fine. – Łukasz Motyczka Jul 02 '15 at 04:43
  • Motyczka -- Great! boss, the problem is solved. Thanks a lot. I think you are the person who can help me in development of my first andEngine game. I need your help, i am developing an android game in which user will be provided with pieces of image or scenery he/she just need to drag those pieces at their correct location to complete the picture, please suggest whether i am going in right direction or i need to use different approach to achieve this – SMT Jul 02 '15 at 05:00