Chào mừng các bạn tiếp tục với loạt bài hướng dẫn lập trình game trên android bằng thư viện andengine
Bài hôm nay mình sẽ hướng dẫn các bạn tạo 1 background tĩnh và động cho game .
Sau bài này các bạn đã có thể tạo cho mình những game có hiệu ứng đẹp mắt chỉ thiếu âm thanh nữa thôi.
Bài tiếp mình sẽ hướng dẫn các bạn làm được điều đó .


Background


Tree




Wind


Từ những bức ảnh đơn giản như vật thôi mà bạn đã có thể tạo ra một hoạt cảnh di chuyển mềm mại.


Video hướng dẫn : (like và G+ ủng hộ mình nha!)




Untitled 1 Link Source : DOWNLOAD HERE


Code MainActivity.class

package com.example.andengine_background;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.AutoParallaxBackground;
import org.anddev.andengine.entity.scene.background.ParallaxBackground.ParallaxEntity;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends BaseGameActivity {

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


 private Camera mCamera;
 
 private Texture mAutoParallaxBackgroundTexture;

 private TextureRegion mParallaxLayerBack;
 private TextureRegion mParallaxLayerMid;
 private TextureRegion mParallaxLayerFront;


 
 @Override
 public Engine onLoadEngine() {
  this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
  return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, 
    new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));

 }

 @Override
 public void onLoadResources() {
 
  
  this.mAutoParallaxBackgroundTexture = new Texture(1024, 1024, TextureOptions.DEFAULT);
  
  this.mParallaxLayerFront = TextureRegionFactory.createFromAsset(this.mAutoParallaxBackgroundTexture,
    this, "gfx/parallax_background_layer_front.png", 0, 0);
  
  this.mParallaxLayerBack = TextureRegionFactory.createFromAsset(this.mAutoParallaxBackgroundTexture
    , this, "gfx/parallax_background_layer_back.png", 0, 188);
  
  this.mParallaxLayerMid = TextureRegionFactory.createFromAsset(this.mAutoParallaxBackgroundTexture,
    this, "gfx/parallax_background_layer_mid.png", 0, 669);

  this.mEngine.getTextureManager().loadTextures( this.mAutoParallaxBackgroundTexture);
  
 }

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

  final Scene scene = new Scene();
  
  final AutoParallaxBackground autoParallaxBackground = new AutoParallaxBackground(0, 0, 0, 5);
  
  autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(0.0f, new Sprite(0, CAMERA_HEIGHT - 
    this.mParallaxLayerBack.getHeight()
    , this.mParallaxLayerBack)));
//  
//  autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(15.0f, new Sprite(0,
//    80, this.mParallaxLayerMid)));
//  
//  autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(10.0f, new Sprite(0,
//    CAMERA_HEIGHT - this.mParallaxLayerFront.getHeight(), this.mParallaxLayerFront)));
  
  scene.setBackground(autoParallaxBackground);
  
  return scene;
 }

 @Override
 public void onLoadComplete() {
  // TODO Auto-generated method stub
  
 }



}

0 comments:

Post a Comment

http://knlaptrinhandroid.blogspot.com/

 
Top