2013-02-12 80 views
3

ExampleAndEngine視差背景上的動畫小精靈幫助! :)

嗨,我的名字是喬納斯,我正在參加Android編程大學課程。 我總是不得不採取艱難的方式去AndEngine。

在上面的圖片中,你可以看到我使用三層的視差背景。 我正在製作一個sidescroller,所以最後一層是藍色線條,這是一條冰跡。我想爲這張照片製作動畫並將其放在曲目上。

Picture i want to animate

當我爲我是在模擬器apeshit會和我展示在屏幕一角的照片的小三角形以同樣的方式動畫的研究背景動畫兩人加載它。一個動畫的作品是這個:

(Picture that works.)

是我的動畫馬大的圖片或者是別的東西我做錯了。關於這件事,我在互聯網上找不到太多東西!我真的很感激我能得到的所有幫助!

這是我的代碼的一個例子,我知道它是一個ripoff ofEngine的例子,但我在這裏學習。

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 
// =========================================================== 
} 

在此先感謝...

/喬納斯

+1

您使用的是GLES 1還是GLES 2?另外,你是否在實際設備上嘗試過你的應用程序?模擬器有一種愚蠢的傾向。 – eBehbahani 2013-02-14 16:45:03

回答

1

您使用的是相同的textureatlas同時生成的紋理。這裏是你的紋理圖案定義:

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

他們Y尺寸那裏(128)對於這張圖片太小了。相反,爲玩家圖像製作第二張紋理圖集,至少有256個高度(它需要2的冪次),它會覆蓋它。

另外,我想確保你故意這樣做:

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

這些最後兩個數字(3,4)代表列和你的馬spritesheet的行數。但是,當我看着你的馬,我只有三看三,不是三四個:

enter image description here

這可能是你想要的是空格作爲動畫三個額外的電池,但要確保你這樣做的目的。 createTiledFromAsset()方法假定精靈在網格中適合。

+0

我很確定解決它。如果您同意並想要授予該綠色複選標記,則會非常感激。 ;) – 2013-02-14 21:35:59