1 | public final Bob bob; //主角 |
1 | this .bob = new Bob( 5 , 1 ); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | /**刷新界面**/
public void update(float deltaTime, float accelX) {
updateBob(deltaTime, accelX); //刷新主角
updatePlatforms(deltaTime); //刷新跳板
}
/**刷新Bob**/
private void updateBob(float deltaTime, float accelX) {
//碰撞跳板
if (bob.state != Bob.BOB_STATE_HIT && bob.position.y <= 0 .5f) bob.hitPlatform();
//主角x軸方向移動的速度
if (bob.state != Bob.BOB_STATE_HIT) bob.velocity.x = -accelX / 10 * Bob.BOB_MOVE_VELOCITY;
bob.update(deltaTime);
//豎直最大高度
heightSoFar = Math.max(bob.position.y, heightSoFar);
} |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | /**渲染遊戲中各種物體(Bob,跳板,松鼠,彈簧,城堡,金幣)**/
private void renderObjects() {
batch.enableBlending();
batch.begin();
//繪製跳板
renderPlatforms();
//繪製主角
renderBob();
batch.end();
}
/**渲染主角**/
private void renderBob() {
TextureRegion keyFrame; //對應狀態下的圖片
switch (world.bob.state) {
case Bob.BOB_STATE_FALL:
keyFrame = Assets.bobFall.getKeyFrame(world.bob.stateTime, Animation.ANIMATION_LOOPING);
break ;
case Bob.BOB_STATE_JUMP:
keyFrame = Assets.bobJump.getKeyFrame(world.bob.stateTime, Animation.ANIMATION_LOOPING);
break ;
case Bob.BOB_STATE_HIT:
default :
keyFrame = Assets.bobHit;
}
float side = world.bob.velocity.x < 0 ? - 1 : 1 ;
if (side < 0 )
batch.draw(keyFrame, world.bob.position.x + 0 .5f, world.bob.position.y - 0 .5f, side * 1 , 1 );
else
batch.draw(keyFrame, world.bob.position.x - 0 .5f, world.bob.position.y - 0 .5f, side * 1 , 1 );
} |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | package com.zhf.mylibgdx; import com.badlogic.gdx.graphics.g2d.TextureRegion; /**
* 動畫類
* @author ZHF
*
*/ public class Animation {
//動畫的兩種狀態,循環和不循環(不循環則動畫播放完畢後停留在最後一張圖片上)
public static final int ANIMATION_LOOPING = 0 ;
public static final int ANIMATION_NONLOOPING = 1 ;
//存儲圖片的數組
final TextureRegion[] keyFrames;
//動畫每幀持續時間
final float frameDuriation;
//在Assets中調用,大家自行參看
public Animation(float frameDuration, TextureRegion... keyFrames) {
this .frameDuriation = frameDuration;
this .keyFrames = keyFrames;
}
/**
* 截取動畫圖片
* @param stateTime 存在此狀態的時間
* @param mode 動畫模式
* @return
*/
public TextureRegion getKeyFrame(float stateTime, int mode) {
//需要返回的圖片在數組中的位置
int frameNumber = ( int ) (stateTime / frameDuriation);
//不循環,停留在最後一張圖片上
if (mode == ANIMATION_NONLOOPING) {
frameNumber = Math.min(keyFrames.length - 1 , frameNumber);
} else {
//循環模式
frameNumber = frameNumber % keyFrames.length;
}
//返回keyFrames數組
return keyFrames[frameNumber];
} } |
1 2 3 4 | //主角
public static Animation bobJump; //跳躍的動畫
public static Animation bobFall; //下落的動畫
public static TextureRegion bobHit; //碰撞圖片 |
1 2 3 4 | //主角
bobJump = new Animation( 0 .2f, new TextureRegion(items, 0 , 128 , 32 , 32 ), new TextureRegion(items, 32 , 128 , 32 , 32 ));
bobFall = new Animation( 0 .2f, new TextureRegion(items, 64 , 128 , 32 , 32 ), new TextureRegion(items, 96 , 128 , 32 , 32 ));
bobHit = new TextureRegion(items, 128 , 128 , 32 , 32 ); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | /**遊戲運行狀態**/
private void updateRunning ( float deltaTime) {
if (Gdx.input.justTouched()) {
guiCam.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0 ));
//點擊暫停
if (OverlapTester.pointInRectangle(pauseBounds, touchPoint.x, touchPoint.y)) {
Assets.playSound(Assets.clickSound);
state = GAME_PAUSED;
return ;
}
}
ApplicationType appType = Gdx.app.getType();
// should work also with Gdx.input.isPeripheralAvailable(Peripheral.Accelerometer)
if (appType == ApplicationType.Android || appType == ApplicationType.iOS) {
world.update(deltaTime, Gdx.input.getAccelerometerX());
} else {
float accel = 0 ;
if (Gdx.input.isKeyPressed(Keys.DPAD_LEFT)) accel = 5f;
if (Gdx.input.isKeyPressed(Keys.DPAD_RIGHT)) accel = -5f;
world.update(deltaTime, accel);
}
} |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /**生成關卡中除了Bob外所有的物體**/
private void generateLevel() {
float y = Platform.PLATFORM_HEIGHT / 2 ;
float maxJumpHeight = Bob.BOB_JUMP_VELOCITY * Bob.BOB_JUMP_VELOCITY / ( 2 * -gravity.y);
while (y < WORLD_HEIGHT - WORLD_WIDTH / 2 ) {
int type = rand.nextFloat() > 0 .8f ? Platform.PLATFORM_TYPE_MOVING : Platform.PLATFORM_TYPE_STATIC;
float x = rand.nextFloat() * (WORLD_WIDTH - Platform.PLATFORM_WIDTH) + Platform.PLATFORM_WIDTH / 2 ;
Platform platform = new Platform(type, x, y);
platforms.add(platform);
y += (maxJumpHeight - 0 .5f);
y -= rand.nextFloat() * (maxJumpHeight / 3 );
}
} |
1 | float maxJumpHeight = Bob.BOB_JUMP_VELOCITY * Bob.BOB_JUMP_VELOCITY / ( 2 * -gravity.y); |
1 | public static final Vector2 gravity = new Vector2( 0 , - 12 ); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | /**刷新界面**/ public void update( float deltaTime, float accelX) {
updateBob(deltaTime, accelX); //刷新主角
updatePlatforms(deltaTime); //刷新跳板
if (bob.state != Bob.BOB_STATE_HIT) checkCollisions(); } /**碰撞檢測**/ private void checkCollisions() {
// TODO Auto-generated method stub
checkPlatformCollisions(); //跳板的碰撞 } private void checkPlatformCollisions() {
if (bob.velocity.y > 0 ) return ;
int len = platforms.size();
for ( int i = 0 ; i < len; i++) {
Platform platform = platforms.get(i);
if (bob.position.y > platform.position.y) {
//調用工具類中矩形塊碰撞檢測
if (OverlapTester.overlapRectangles(bob.bounds, platform.bounds)) {
bob.hitPlatform();
listener.jump();
if (rand.nextFloat() > 0 .5f) {
platform.pulverize();
}
break ;
}
}
} } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | package com.zhf.mylibgdx; import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Vector2; /**
* 工具類:檢測各種碰撞
* @author ZHF
*
*/ public class OverlapTester {
/**檢測輸入的X,Y是否在輸入的矩形框r內**/
public static boolean pointInRectangle(Rectangle r, float x, float y) {
return r.x <= x && r.x + r.width >= x && r.y <= y
&& r.y + r.height >= y;
}
/**兩個矩形塊的碰撞檢測**/
public static boolean overlapRectangles (Rectangle r1, Rectangle r2) {
if (r1.x < r2.x + r2.width && r1.x + r1.width > r2.x && r1.y < r2.y + r2.height && r1.y + r1.height > r2.y)
return true ;
else
return false ;
}
/**點是否在矩形內**/
public static boolean pointInRectangle (Rectangle r, Vector2 p) {
return r.x <= p.x && r.x + r.width >= p.x && r.y <= p.y && r.y + r.height >= p.y;
} } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /**渲染**/
public void render () {
//重點講解
if (world.bob.position.y > cam.position.y) cam.position.y = world.bob.position.y;
cam.update();
//它的作用都是通過把映射矩陣綁定給SpritBatch,告訴SpritBatch怎麼去繪製圖形
batch.setProjectionMatrix(cam.combined);
//渲染背景
renderBackground();
//渲染遊戲中各種元素(Bob,跳板,松鼠,彈簧。。)下一講中會具體講到
renderObjects();
} |