Blog chia sẻ kinh nghiệm lập trình Android

Tiếp tục loạt bài hướng dẫn lập trình game android bằng thư viện andengine
Hôm nay mình hướng dẫn các bạn cách sử dụng các hiệu ứng mà thư viện andengine hỗ trợ cho các textture

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



package com.example.demogameandengine;

import java.util.concurrent.CountDownLatch;

import javax.microedition.khronos.opengles.GL10;

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.modifier.AlphaModifier;
import org.anddev.andengine.entity.modifier.ParallelEntityModifier;
import org.anddev.andengine.entity.modifier.RotationModifier;
import org.anddev.andengine.entity.modifier.ScaleModifier;
import org.anddev.andengine.entity.modifier.SequenceEntityModifier;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.text.ChangeableText;
import org.anddev.andengine.entity.text.Text;
import org.anddev.andengine.entity.text.TickerText;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.font.FontFactory;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.ui.activity.BaseGameActivity;
import org.anddev.andengine.util.HorizontalAlign;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.CountDownTimer;

public class MainActivity extends BaseGameActivity {

 
 private static final int CAMERA_WIDTH = 480;// độ rộng màn hình hiển thị
 private static final int CAMERA_HEIGHT = 800;// chiều cao màn hình hiển thị
 
 

 
 private Texture mFontTexture;
 private Font mFont;

 
 // text custom font 
    private Texture texture;
    private Font font;
    
// text change 
    
    private Texture texturetextchange;
    private Font fonttextchange;
   
// tạo biến đêm
     int count = 0;
     
    // text ticker 
     
     private Texture mFontTextureTicker;
     private Font mFontTicker;
 

     

 
 
 // load game : kick thước camera : khung nhìn ...
 //PORTRAIT khởi tạo màn hình xoay ngang
 @Override
 public Engine onLoadEngine() {
  final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
  final EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.PORTRAIT, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
  engineOptions.getTouchOptions().setRunOnUpdateThread(true);
  return new Engine(engineOptions);
 }

 
 // loaad các file nguồn như âm thành hình ảnh
 @Override
 public void onLoadResources() {
  this.mFontTexture = new Texture(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
  
  this.mFont = new Font(this.mFontTexture, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 32, true, Color.BLACK);
  this.mEngine.getTextureManager().loadTexture(this.mFontTexture);
  this.mEngine.getFontManager().loadFont(this.mFont);
  
  
  FontFactory.setAssetBasePath("font/"); //  câu lệnh này để chỉ tới thư mục font chưa các font chữ
  
  // load text font
  // mình sẽ sử dụng text này để làm ví dụ, các laoị text khác cũng tương tự 
  // ở bài trước  mình để màu vàng giờ mình để màu đen
  texture= new Texture(256, 256,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
  font = FontFactory.createFromAsset(this.texture, this,"BRUSHSCRIPTSTD.OTF", 45, true, Color.BLACK);
  mEngine.getTextureManager().loadTexture(texture);
  mEngine.getFontManager().loadFont(font);
  
  

  // load text change
    texturetextchange= new Texture(512, 512,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    fonttextchange = FontFactory.createFromAsset(this.texturetextchange, this,"LCD.ttf", 32, true, Color.BLUE);
    mEngine.getTextureManager().loadTexture(texturetextchange);
    mEngine.getFontManager().loadFont(fonttextchange);

     
// load text ticker
    
    this.mFontTextureTicker = new Texture(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    
    this.mFontTicker = new Font(this.mFontTextureTicker, Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD_ITALIC), 24, true, Color.RED);
    this.mEngine.getTextureManager().loadTexture(this.mFontTextureTicker);
    this.mEngine.getFontManager().loadFont(this.mFontTicker);
    
 
    
 }
// scene phương thwucs hiển thị chính 
 @Override
 public Scene onLoadScene() {
  this.mEngine.registerUpdateHandler(new FPSLogger());

  final Scene scene = new Scene(2);
 
  scene.setBackground(new ColorBackground(0.23f, 1, 0));
  
  // text
  Text text= new Text(50 , 50, mFont, "XIN CHÀO ANDENGINE !");
  
  //text.setRotation(90);
  scene.attachChild(text);
  
  // KHAI BÁO CÁ HIỆU ỨNG TRÊN TEXT
  
  Text textcustom = new Text(50, 600, font, "hello every body !");
  
  //   CÁC HIỆU ỨNG ĐƠN
  //textcustom.setRotation(90);  //xoay 90 độ theo chiều kim đồng hồ @@
  
  //textcustom.setAlpha(0.1f); // làm mờ text còn 0.1  => text đã bị mờ đi
  
  //textcustom.setScale(2); // phóng to text lên 2 lần => text được phóng to như ý
  //textcustom.setScaleX(2); // phóng to 2 lần theo trục ox => chữ được kéo dãn theo oX
  //textcustom.setScaleY(3);// phóng to theo oy 3 lần  => kéo dài text theo OY
  
  //textcustom.setPosition(0, 500);// đặt laị vị trí text ban đầu là 50, 600 => thay đổi thành 0 . 500
  
  
  
 // KẾT HỢP THÀNH ĐA HIỆU ỨNG
  //xoay vòng tròn từ 0 tới 360 độ trong 5 giây => ok
  //textcustom.registerEntityModifier(new SequenceEntityModifier(new RotationModifier(5, 0, 360)));
 
  // làm sáng đậm dần chữ từ 00 tới rõ nét nhất 1 trong 10 giây => ok
  //textcustom.registerEntityModifier(new SequenceEntityModifier(new AlphaModifier(10, 0.0f, 1.0f)));
  
  // phóng to chữ từ 0.5 tới kick thước cũ là 1  trong 10s => ok
  //textcustom.registerEntityModifier(new SequenceEntityModifier(new ScaleModifier(10, 0.5f, 1.0f)));
//  
//  kết hợp 2 lần phóng to thu nhỏ ta đk hiệu ứng phóng to  nhỏ to nhỏ trong 5s => ok
//  textcustom.registerEntityModifier(new SequenceEntityModifier(new ScaleModifier(5, 0.5f, 1.0f),
//    new ScaleModifier(10, 1f, 0.5f)));
//  
//  
//   kết hơp các hiẹuu ứng vào 1 bằng cách 
  
  //textcustom.registerEntityModifier(new SequenceEntityModifier(hiệu ứng1, hiệu ứng 2, ...hiệu ứng n));
//  textcustom.registerEntityModifier(
//     new SequenceEntityModifier(
//      new ParallelEntityModifier(
//        new AlphaModifier(4, 0.0f, 1.0f),
//        new ScaleModifier(4, 0.5f, 1.0f)),
//        new RotationModifier(4, 0, 360))  );
  
  
  scene.attachChild(textcustom);
  
  
  
  
 
  // text change
    final ChangeableText textchanges = new ChangeableText(50, 150, fonttextchange, " count = "+0+"  ");
    scene.attachChild(textchanges);
    
    
    
    
    
    
   // textchanges.setRotation(90);
    
    CountDownTimer t = new CountDownTimer(30000,100) {
     
     @Override
     public void onTick(long millisUntilFinished) {
      // TODO Auto-generated method stub
      count++;
      textchanges.setText(" count = "+count);
      
     }
     
     @Override
     public void onFinish() {
      
     }
    };
    t.start();
    
 

    // text tickker
        String stt = "chào mừng các bạn đã đến với lập trình\n" +
          "game android bằng thư viện Andengine" +
          "\n\n Chúc các bạn vui vẻ !";
        
        final Text textticker = new TickerText(10, 200, this.mFontTicker,stt, HorizontalAlign.CENTER, 20);
        textticker.registerEntityModifier(
          new SequenceEntityModifier(
            new ParallelEntityModifier(
              new AlphaModifier(10, 0.0f, 1.0f),
              new ScaleModifier(10, 0.5f, 1.0f)
            ),
            new RotationModifier(5, 0, 360)
          )
        );
        textticker.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
        scene.attachChild(textticker);


    
    
  return scene;
 }
 
// chưa cần quan tâm lắm 
 @Override
 public void onLoadComplete() {
  // TODO Auto-generated method stub
  
 }

}




0 comments:

Post a Comment

http://knlaptrinhandroid.blogspot.com/

 
Top