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

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

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 !


Hôm này mình giớ thiệu các bạn về textchange loại text có thể thay đổi thông tin khi ứng dụng đang chạy trong lập trình andengine

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






package com.example.demogameandengine;

import java.util.concurrent.CountDownLatch;

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.ChangeableText;
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;
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;



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



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


}
// 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(200 , 200, mFont, "XIN CHÀO ANDENGINE !");

text.setRotation(90);
scene.attachChild(text);

// text font
Text textcustom = new Text(200, 400, font, "hello every body !");
textcustom.setRotation(90);
scene.attachChild(textcustom);



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



// text change
final ChangeableText textchanges = new ChangeableText(200, 600, 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();


return scene;
}

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

}


}



<


br>

◄ Back Next ►
 

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