Thứ Tư, 16 tháng 4, 2014

Bài 3 lập trình game andengine cơ bản customtext

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

Chào mừng các bạn đã đến với andengine - công cụ hỗ trợ lập trình game trên android

 Đây là công cụ khá mạnh và tốt cho lập trình 2D trên android

 - nhược điểm là ít tài liệu -  nhưng đến với tutorial này các bạn sẽ sớm trở thành một lập trình game android andengine chuyên nghiệp :

VD như các game mình viết trên andengine :

 https://www.youtube.com/watch?v=GeUyhIFxw28

https://www.youtube.com/watch?v=ziNVRqCJL3c

https://www.youtube.com/watch?v=cc-Gx0XBs68
https://www.youtube.com/watch?v=6Bn9f8yMvEc

và còn nhiều game khác !


Trong bài này mình hướng dẫn các bạn  hiển thị 1 text với font chữ tùy chọn .

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.font.FontFactory;

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;



// text custom font

private Texture texture;

private Font font;





// 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ữ



texture= new Texture(256, 256,TextureOptions.BILINEAR_PREMULTIPLYALPHA);



font = FontFactory.createFromAsset(this.texture, this,"BRUSHSCRIPTSTD.OTF", 45, true, Color.YELLOW);



mEngine.getTextureManager().loadTexture(texture);

mEngine.getFontManager().loadFont(font);



}

// 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);





Text textcustom = new Text(200, 300, font, "Chào các bạn !");

textcustom.setRotation(90);

scene.attachChild(textcustom);







return scene;

}



// chưa cần quan tâm lắm

@Override

public void onLoadComplete() {

// TODO Auto-generated method stub



}





}



◄ Back Next ►
 

Copyright 2014 Blog Tự Học Android - All Right Reserved Design by Garatemplate Published By Goo Quynh