Thứ Sáu, 18 tháng 4, 2014

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

Tiếp tục load bài lập trình game trên android bằng thư viện andengine
Hôm nay mình hướng dẫn các bạn sử dụng ticker text 1 loại text có thể bao gồn rất nhiều hiệu ứng khác nhau như là xoay trong , phóng to , thu nhỏ, hiện từng chữ 1 ... cho tới hết text ,,, v,vv

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



Code :

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


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

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




// text change
final ChangeableText textchanges = new ChangeableText(50, 300, 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, 450, 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

}

}




◄ Back Next ►
 

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