Анимированные спрайты AndEngine на фоне параллакса помогают! :)

Пример

Привет, меня зовут Джонас, и я учусь на университетских курсах по программированию для Android. Мне всегда приходится идти трудным путем и идти AndEngine.

На картинке выше вы можете видеть, что я использую фон Parallax с тремя слоями. Я делаю боковую прокрутку, поэтому нижний слой с синей линией, которая представляет собой ледяную дорожку, прокручивается. Я хочу оживить эту картинку и поставить на трек.

Изображение, которое я хочу анимировать

Когда я загружаю его, когда я анимирую двух человек в фоновом режиме, который анимирован таким же образом, эмулятор сходит с ума и показывает мне маленький треугольник изображения в углу экрана. Вот анимированная картинка, которая работает:

(Изображение, которое работает.)

Мое анимированное изображение лошади слишком большое или это что-то еще, что я делаю неправильно. Я не так много нашел в Интернете по этому вопросу! Я был бы очень признателен за всю помощь, которую я могу получить!

Это пример моего кода, и я знаю, что это плагиат примеров andEngine, но я здесь, чтобы учиться.

package com.example.towerofhanoi;

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.AutoParallaxBackground;
import org.andengine.entity.scene.background.ParallaxBackground.ParallaxEntity;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;
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.texture.region.TiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.ui.activity.SimpleBaseGameActivity;

/**
 * (c) 2010 Nicolas Gramlich
 *
 * @author Nicolas Gramlich
 * @since 19:58:39 - 19.07.2010
 */
public class MainActivity extends SimpleBaseGameActivity {
// ===========================================================
// Constants
// ===========================================================

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

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

private BitmapTextureAtlas mBitmapTextureAtlas;
private TiledTextureRegion mPlayerTextureRegion;
private TiledTextureRegion mEnemyTextureRegion;

private BitmapTextureAtlas mAutoParallaxBackgroundTexture;

private ITextureRegion mParallaxLayerBack;
private ITextureRegion mParallaxLayerMid;
private ITextureRegion mParallaxLayerFront;

// ===========================================================
// 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(), 256, 128, TextureOptions.BILINEAR);
    this.mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "player.png", 0, 0, 3, 4);
    this.mEnemyTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "enemy.png", 73, 0, 3, 4);
    this.mBitmapTextureAtlas.load();

    this.mAutoParallaxBackgroundTexture = new BitmapTextureAtlas(this.getTextureManager(), 1024, 1024);
    this.mParallaxLayerFront = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mAutoParallaxBackgroundTexture, this, "parallax_background_layer_front.png", 0, 0);
    this.mParallaxLayerBack = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mAutoParallaxBackgroundTexture, this, "parallax_background_layer_back.png", 0, 188);
    this.mParallaxLayerMid = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mAutoParallaxBackgroundTexture, this, "parallax_background_layer_mid.png", 0, 669);
    this.mAutoParallaxBackgroundTexture.load();
}

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

    final Scene scene = new Scene();
    final AutoParallaxBackground autoParallaxBackground = new AutoParallaxBackground(0, 0, 0, 5);
    final VertexBufferObjectManager vertexBufferObjectManager = this.getVertexBufferObjectManager();
    autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(0.0f, new Sprite(0, CAMERA_HEIGHT - this.mParallaxLayerBack.getHeight(), this.mParallaxLayerBack, vertexBufferObjectManager)));
    autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(-5.0f, new Sprite(0, 80, this.mParallaxLayerMid, vertexBufferObjectManager)));
    autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(-10.0f, new Sprite(0, CAMERA_HEIGHT - this.mParallaxLayerFront.getHeight(), this.mParallaxLayerFront, vertexBufferObjectManager)));
    scene.setBackground(autoParallaxBackground);

    /* Calculate the coordinates for the face, so its centered on the camera. */
    final float playerX = (CAMERA_WIDTH - this.mPlayerTextureRegion.getWidth()) / 2;
    final float playerY = CAMERA_HEIGHT - this.mPlayerTextureRegion.getHeight() - 5;

    /* Create two sprits and add it to the scene. */
    final AnimatedSprite player = new AnimatedSprite(playerX +230, playerY-180, this.mPlayerTextureRegion, vertexBufferObjectManager);
    player.setScaleCenterY(this.mPlayerTextureRegion.getHeight());
    player.setScale(2);
    player.animate(new long[]{100, 100, 100}, 6, 8, true);

    final AnimatedSprite enemy = new AnimatedSprite(playerX + 200, playerY -180, this.mEnemyTextureRegion, vertexBufferObjectManager);
    enemy.setScaleCenterY(this.mEnemyTextureRegion.getHeight());
    enemy.setScale(2);
    enemy.animate(new long[]{200, 200, 200}, 6, 8, true);

    scene.attachChild(player);
    scene.attachChild(enemy);

    return scene;
}

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

// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}

заранее спасибо...

/Джонас


person user2063918    schedule 12.02.2013    source источник
comment
Вы используете GLES 1 или GLES 2? Кроме того, пробовали ли вы свое приложение на реальном устройстве? Эмулятор имеет тенденцию быть глупым.   -  person eBehbahani    schedule 14.02.2013


Ответы (1)


Вы используете один и тот же текстурный атлас для создания обеих текстур. Вот ваше определение текстуры:

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

Их размер Y (128) слишком мал для этого изображения. Вместо этого создайте второй текстурный атлас для изображения игрока с высотой не менее 256 (требуется степень двойки), и он покроет его.

Кроме того, я хочу убедиться, что вы делаете это намеренно:

  this.mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "player.png", 0, 0, 3, 4);

Последние два числа (3 и 4) представляют собой количество столбцов и строк вашего спрайт-листа с лошадью. Но когда я смотрю на вашу лошадь, я вижу только три на три, а не три на четыре:

введите здесь описание изображения

Возможно, вы хотите, чтобы эти пробелы были тремя дополнительными ячейками в анимации, но убедитесь, что вы делаете это намеренно. Метод createTiledFromAsset() предполагает, что спрайты вписываются в сетку.

person Cameron Fredman    schedule 14.02.2013
comment
Я уверен, что это решает. Если вы согласны и хотите поставить зеленую галочку, мы будем очень признательны. ;) - person Cameron Fredman; 15.02.2013