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

Bài này mình hướng dẫn các bạn sử dụng text bình thường trong andengine
Một thành phần không thể thiếu để hiển thị các dữ liệu của trò chơi.


Dưới đây là video hướng dẫn .(like và G+ ủng hộ mình nha!)






Code :



package com.example.demogameandengine;

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.ColorBackground;
import org.anddev.andengine.entity.text.Text;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.graphics.Color;
import android.graphics.Typeface;

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;
 
 
 // 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);
 }
// 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= new Text(200 , 200, mFont, "XIN CHÀO ANDENGINE !");
  
  text.setRotation(90);
  scene.attachChild(text);
  
  
  
  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