Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android系統教程 >> Android開發教程 >> android中抽象公共類,復用公共方法為多個對象

android中抽象公共類,復用公共方法為多個對象

編輯:Android開發教程

今天學習如何實現憤怒的小鳥,用到了JBox2D構建物理世界(這個不是這篇文章重點,但是很謝謝這個開源玩意)

一般一個獨立的物體的實體類

import org.jbox2d.dynamics.Body;
import org.liky.angrybird.util.Globals;
import org.liky.angrybird.util.ImageUtils;
    
    
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
    
    
public class Bird {
    
    
private Body body;
    
    
private float r;
private int type;
    
    
public Bird(int type) {
switch (type) {
case 1:
r = Globals.PIECE_WIDTH / 2;
break;
}
this.type = type;
}
    
    
public void draw(Canvas canvas, Paint paint, Point nowPosition) {
canvas.save();
    
    
canvas.rotate((float) (body.getAngle() * 180 / Math.PI),
body.getPosition().x * Globals.RATE + nowPosition.x,
body.getPosition().y * Globals.RATE + nowPosition.y);
    
    
canvas.drawBitmap(ImageUtils.getBirdImg(type), body.getPosition().x
* Globals.RATE - r + nowPosition.x, body.getPosition().y
* Globals.RATE - r + nowPosition.y, paint);
    
    
if (Globals.birdCenterFlag) {
// 保證正在飛行的鳥的坐標在屏幕正中
//返回欄目頁:http://www.bianceng.cn/OS/extra/
nowPosition.x = (int) -(body.getPosition().x * Globals.RATE - Globals.SCREEN_WIDTH / 2);
nowPosition.y = (int) -(body.getPosition().y * Globals.RATE - Globals.SCREEN_HEIGHT / 2);
    
    
if (nowPosition.x > 0) {
nowPosition.x = 0;
}
if (nowPosition.y > 0) {
nowPosition.y = 0;
}
if (nowPosition.x < -Globals.SCREEN_WIDTH) {
nowPosition.x = -Globals.SCREEN_WIDTH;
}
if (nowPosition.y < -Globals.SCREEN_HEIGHT) {
nowPosition.y = -Globals.SCREEN_HEIGHT;
}
}
canvas.restore();
}
    
    
public Body getBody() {
return body;
}
    
    
public void setBody(Body body) {
this.body = body;
this.body.m_userData = this;
}
    
    
public int getType() {
return type;
}
    
    
public void setType(int type) {
this.type = type;
}
    
    
public float getR() {
return r;
}
    
    
public void setR(float r) {
this.r = r;
}
    
    
}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved