下面是代碼 可其中出現了一個錯誤(做了標記),不知道怎麼解決,可否有高手幫我看看,能不能稍作修改一下,做成正確的。
就是這句錯了 myview = new BallView(this);
package sen.tan;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class dazhuankuaiActivity extends Activity {
BallView myview;
public static int screenWidth ;
public static int screenHeight;
private TextView textview;
Button kaishi,jieshu,guanyu;
ImageView image;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String string = "歡迎進入瘋狂打磚塊游戲";
image = (ImageView) this.findViewById(R.id.image);
image.setImageResource(R.drawable.img1);
textview =(TextView) this.findViewById(R.id.textview);
textview.setTextSize(20);
textview.setTextColor(Color.GREEN);
textview.setBackgroundColor(Color.RED);
textview.setText(string);
kaishi=(Button)findViewById(R.id.button1);
guanyu=(Button)findViewById(R.id.button2);
jieshu=(Button)findViewById(R.id.button3);
kaishi.setWidth(250);
guanyu.setWidth(200);
jieshu.setWidth(150);
kaishi.setTextSize(35);
guanyu.setTextSize(29);
jieshu.setTextSize(24);
jieshu.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v)
{
dazhuankuaiActivity.this.finish();
}
});
guanyu.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
textview.setText("本游戲由xxx制作,此游戲控制左右按鍵,來回移動擋板,不讓小球掉落,當小球把上方磚塊全部打完時,游戲勝利");
}
});
kaishi.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
screenWidth = dm.widthPixels;
screenHeight = dm.heightPixels;
setTitle("寬"+screenWidth+" 高"+screenHeight);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
`myview = new BallView(this);` //錯誤在這裡
setContentView(myview);
}
public boolean onKeyDown(int keyCode, KeyEvent event)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_MENU:
myview.resetGame();
break;
case KeyEvent.KEYCODE_DPAD_CENTER:
myview.ball_isRun = !myview.ball_isRun;
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
if(myview.ball_isRun){
if(myview.board_left<=myview.board_x_move)
{
myview.board_left=0;
myview.board_right=myview.board_length;
}else{
myview.board_left-=myview.board_x_move;
myview.board_right-=myview.board_x_move;}
}
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
if(myview.ball_isRun){
if(screenWidth-myview.board_right<=myview.board_x_move )
{
myview.board_left=screenWidth-myview.board_length;
myview.board_right=screenWidth;
}else{
myview.board_left+=myview.board_x_move;
myview.board_right+=myview.board_x_move;
}
}
break;
case KeyEvent.KEYCODE_DPAD_UP:
if(myview.ball_isRun){
if( myview.board_alterable_top==myview.board_default_top)
{
myview.board_alterable_top-=myview.boardYadd;
myview.board_alterable_bottom-=myview.boardYadd;
}
}
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
if(myview.ball_isRun){
if( myview.board_alterable_top==myview.board_default_top-myview.boardYadd )
{
myview.board_alterable_top=myview.board_default_top;
myview.board_alterable_bottom=myview.board_alterable_top+myview.board_thickness;
}
}
break;
case KeyEvent.KEYCODE_BACK:
dazhuankuaiActivity.this.finish();
break;
}//end switch
return false;
}
//按鍵彈起事件
}
);
}
}
package sen.tan;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.Shader;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class BallView extends SurfaceView implements
SurfaceHolder.Callback, Runnable {
//線程延時控制
final int ball_sleep=1;//毫秒,延時越大,球速越慢
final int ball_r= 8;//小球半徑
final float ball2_r= 8;//底下滾珠小球半徑
final int ballXorYadd = 4;//小球的基本位移。測試可行值:2,4
//獲取屏幕寬度和高度
int screen_width;//320;
int screen_height;//480;
//磚的屬性
int brick_width;//每塊磚寬
int brick_height;//每塊磚高
boolean brick_exist[];//磚是否存在
int k ;// 列//到for循環裡才初始化
int j ;// 行
int brick_left = brick_width*(k-1);//到判斷語句才初始化
int brick_right = brick_width*k;
int brick_top = brick_height*j;
int brick_bottom = brick_height*(j+1);
//擋板的屬性
int board_length;//擋板長度:80比較合適,可以隨意修改,但別超過screen_width
final int boardYadd = 16;//按上下鍵時擋板y方向位移量。經驗證4、8、16可以,12不行
final int board_x_move = 30;//擋板x方向位移量:可以隨意自定義
int board_left;//擋板左側(可變)
int board_right;//擋板右側(可變)
int board_thickness;//擋板厚度
int board_default_top;//即435,擋板的top面初始位置
int board_alterable_top;//擋板上側(可變)
int board_alterable_bottom;//擋板下側(可變)
int ball_default_x;//球的初始x坐標
int ball_default_y;//球的初始y坐標
//球的即時坐標(可變):
int ball_x;//球心橫坐標
int ball_y;//球心縱坐標
//球的前一步的y坐標
int ball_previous_y;
int ball_x_speed;//球的橫向偏移量//可變
int ball_y_speed;//球的縱向偏移量//可變
boolean ball_isRun;//球是否在動
// 控制循環
boolean mbLoop;
// 定義SurfaceHolder對象
SurfaceHolder mSurfaceHolder = null;
//獲得分數
int score;
/* 喚醒漸變渲染 */
Shader mRadialGradient = null;
//------------------------------------------------------------------------------------------------------//
public BallView(Context context) {
super(context);
// 實例化SurfaceHolder
mSurfaceHolder = this.getHolder();
// 添加回調
mSurfaceHolder.addCallback(this);
this.setFocusable(true);
//獲取屏幕寬度和高度
screen_width = dazhuankuaiActivity.screenWidth;//320
screen_height = dazhuankuaiActivity.screenHeight;//480
//磚的屬性
brick_width = screen_width/5;//每塊磚寬64
brick_height = screen_height/15;//每塊磚高32
//擋板的屬性
board_length = screen_width/4;//擋板長度:80比較合適,可以隨意修改,但別超過screen_width
board_left = (screen_width-board_length)/2;//擋板左側(可變)
board_right = (screen_width+board_length)/2;//擋板右側(可變)
board_thickness = 5;//擋板厚度
board_default_top = 13*screen_height/15;//即435,擋板的top面初始位置
board_alterable_top = board_default_top;//擋板上側(可變)
board_alterable_bottom = board_alterable_top+board_thickness;//擋板下側(可變)
ball_default_x = screen_width/2;//球的初始x坐標
ball_default_y = board_default_top - ball_r;//球的初始y坐標
//球的即時坐標(可變):
ball_x = ball_default_x;
ball_y = ball_default_y;
//球的前一步的y坐標
ball_previous_y = 0;
ball_x_speed = ballXorYadd;//球的橫向偏移量
ball_y_speed = ballXorYadd;//球的縱向偏移量
mbLoop = true;
ball_isRun = false;
score=0;
brick_exist = new boolean[25];
for (int i = 0; i < 25; i++) {
brick_exist[i] = true;
}
/* 構建RadialGradient對象,設置半徑的屬性 */
mRadialGradient = new RadialGradient(ball_x, ball_y, ball_r,//球中心坐標x,y,半徑r
new int[]{Color.WHITE,Color.BLUE,Color.GREEN,Color.RED,Color.YELLOW},//顏色數組
null,//顏色數組中每一種顏色對應的相對位置,為空的話就是平均分布,由中心向外排布
Shader.TileMode.REPEAT);//渲染模式:重復
}
public void resetGame(){
ball_isRun = false;
score =0;//分數
ball_x_speed = ballXorYadd;//球的橫向偏移量
ball_y_speed = ballXorYadd;//球的縱向偏移量
ball_x = screen_width/2;//球心起始橫坐標
ball_y = board_default_top - ball_r;//球心起始縱坐標
board_left = (screen_width-board_length)/2;//擋板左側
board_right = (screen_width+board_length)/2;//擋板右側
board_alterable_top = board_default_top;//擋板上側
board_alterable_bottom = board_alterable_top+board_thickness;//擋板下側
for (int i = 0; i < 25; i++) {
brick_exist[i] = true;
}
}
//---------------------------------繪圖循環開始----------------------------------
public void run() {
while (mbLoop&&!Thread.currentThread().isInterrupted()) {
try {
Thread.sleep(ball_sleep);
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
//球的前一步y坐標
ball_previous_y = ball_y;
if (ball_isRun) {
ballRunning();// 讓小球移動
boardPositionCheck();//檢測擋板是否處於“中線”位置,是就隨小球上升一步,直至到“上線”
hitWallCheck();//牆壁碰撞檢測
hitBoardCheck();//擋板碰撞檢測
hitBrickCheck();//磚塊碰撞檢測
}
synchronized (mSurfaceHolder)
{
Draw();
}
}
}
//---------------------------------繪圖循環結束----------------------------------
//------------------------------------------------------------------------------
//讓小球移動
public void ballRunning() {
ball_x += ball_x_speed;
ball_y -= ball_y_speed;
}
//朝左或朝右碰撞後小球水平方向逆向
public void ballLeftOrRightHit() {
ball_x_speed *= -1;
}
//朝上或朝下碰撞後小球豎直方向逆向
public void ballUpOrDownHit() {
ball_y_speed *= -1;
}
public void ballcornerHit() {
ball_x_speed *= -1;
ball_y_speed *= -1;
}
public void ballStraightUp() {//功能沒實現,這是多余代碼
ball_x_speed = 0;//注意在其他地方恢復
ball_y_speed *= -1;
}
//-------------------牆壁碰撞檢測開始-------------------------------------
public void hitWallCheck() {
// 左碰牆
if (ball_x <= ball_r &&
ball_y >= ball_r && ball_y <= screen_height)
{
ballLeftOrRightHit();
// 右碰牆
}
if (ball_x >= screen_width - ball_r && //不能寫為 else if,因為可能恰好碰到角落。
ball_y >= ball_r && ball_y <= screen_height)
{
ballLeftOrRightHit();
}
// 上碰牆
if (ball_x >= ball_r && ball_x <= screen_width - ball_r &&
ball_y <= ball_r + brick_height) //、、、、、、、、、、、、、、、25號修改
{
ballUpOrDownHit();
}
// 下碰牆
if (ball_x >= ball_r && ball_x <= screen_width - ball_r &&
ball_y >= screen_height-ball_r)
{
ballUpOrDownHit();
if(score<=10){
score = 0;
}
else score-=10;
}
}
//-------------------牆壁碰撞檢測結束-------------------------------------
//-----------------------------擋板碰撞檢測開始---------------------------
public void hitBoardCheck() {
// 下碰擋板正面
if (ball_x >= ball_r && ball_x <= screen_width - ball_r && //在屏幕內,起碼條件
ball_x >= board_left && ball_x <= board_right && //在擋板X域上方
ball_y == board_alterable_top - ball_r &&//球面與擋板相切
ball_previous_y <= board_alterable_top - ball_r //確定球是從上方下落
)
{
if(board_alterable_top==board_default_top-boardYadd ){//如果彈簧伸張,擋板位於上線
ballHitBoardlower();//作用:ball_y_move減小;擋板被打下;小球Y向運動反向
}
else if(board_alterable_top==board_default_top){//如果彈簧壓縮,擋板位於下線
boardHitBallHigher();//作用:ball_y_move增加;擋板彈上;小球Y向運動反向
}
}
//斜碰擋板右上角//經驗證有效
else if(Math.pow(board_right-ball_x, 2)+Math.pow(board_alterable_top-ball_y,2)<=Math.pow(ball_r, 2)&&
ball_x>board_right && ball_y<board_alterable_top)
{
ballcornerHit();
}
//斜碰擋板的左上角//經驗證有效
else if(Math.pow(board_left-ball_x, 2)+Math.pow(board_alterable_top-ball_y,2)<=Math.pow(ball_r, 2)&&
ball_x<board_left && ball_y<board_alterable_top)
{
ballcornerHit();
}
}
//-----------------------------擋板碰撞檢測結束--------------------------
private void boardHitBallHigher() {//增強
ballUpOrDownHit();//小球Y方向反向,ball_y_speed變為正數
if(ball_y_speed == ballXorYadd ){
ball_y_speed += ballXorYadd;//離開擋板後小球Y方向速度增強
}
if( boardYadd > ball_y_speed ){//在線程這一輪,小球上升多少,擋板就上升多少。
board_alterable_top = board_default_top - ball_y_speed;
board_alterable_bottom= board_alterable_top+board_thickness;//擋板下層面
}
}
//檢測擋板是否處於“中線”位置,是就隨小球上升一步,直至到“上線”
private void boardPositionCheck() {//還可直接利用球的位置刷新,board_top與球心相差ball_r
if(board_alterable_top < board_default_top && board_alterable_top > board_default_top-boardYadd){
//擋板隨球上升
if(board_alterable_top - ball_y_speed >= board_default_top-boardYadd){
board_alterable_top -= ball_y_speed;//擋板上層面
board_alterable_bottom= board_alterable_top+board_thickness;//擋板下層面
}
else{
board_alterable_top = board_default_top-boardYadd;//擋板上層面
board_alterable_bottom= board_alterable_top+board_thickness;//擋板下層面
}
}
}
private void ballHitBoardlower() {//減弱
board_alterable_top=board_default_top;
board_alterable_bottom=board_default_top+board_thickness;//擋板被打退
ballUpOrDownHit();//小球Y方向反向
if(ball_y_speed==2*ballXorYadd){
ball_y_speed -= ballXorYadd;//小球Y方向速度減弱
}
}
//磚塊碰撞檢測開始-----------------------------------------------------------
public void hitBrickCheck() {
for (int i = 0; i <25 ; i++) {
if (brick_exist[i]) {
k = i % 5+1;// 1,2,3,4,5循環
j = i / 5+1;// 1,1,1,1,1;2,2,2,2,2,;...;5,5,5,5,5
brick_left = brick_width*(k-1);
brick_right = brick_width*k;
brick_top = brick_height*j;
brick_bottom = brick_height*(j+1);
//朝下碰磚的top面 AAAAAAAAAAAAAAAAAAAAA
if(ball_x >= brick_left && ball_x <= brick_right &&
ball_y >= brick_top-ball_r && ball_y < brick_top)
{
ballUpOrDownHit();
brick_exist[i] = false;
score+=4;
//朝下正碰2磚中間,i磚右上角檢測
if(k!=5 && ball_x == brick_right)//如果不是第5列磚的右側邊
{
//如果磚[i+1]存在
if(brick_exist[i+1]){
brick_exist[i+1] = false;
score+=4;
}
}
//朝下正碰2磚中間,i磚左上角檢測
else if(k!=1 && ball_x == brick_left)//如果不是第1列磚的左側邊
{
//如果磚[i-1]存在
if(brick_exist[i-1]){
brick_exist[i-1] = false;
score+=4;
}
}
}
//朝上碰磚的bottom面 BBBBBBBBBBBBBBBBB
else if(ball_x >= brick_left && ball_x <= brick_right &&
ball_y > brick_bottom&& ball_y <= brick_bottom + ball_r )
{
ballUpOrDownHit();
brick_exist[i] = false;
score+=4;
//朝上正碰2塊磚中間--i磚的右下角檢測
if(k!=5 && ball_x == brick_right) //如果不是第5列磚的右側邊
{
if(brick_exist[i+1]){//如果磚[i+1]存在
brick_exist[i+1] = false;
score+=4;
}
}
//朝上正碰2塊磚中間--i磚的左下角檢測
else if(k!=1 && ball_x == brick_left) //如果不是第1列磚的左側邊
{
if(brick_exist[i-1]){//如果磚[i-1]存在
brick_exist[i-1] = false;
score+=4;
}
}
}
//朝右碰磚的left面CCCCCCCCCCCCCCCCC
else if(ball_x >= brick_left -ball_r&&ball_x < brick_left&&
ball_y >= brick_top && ball_y <= brick_bottom)
{
ballLeftOrRightHit();
brick_exist[i] = false;
score+=4;
//朝右正碰2塊磚中間,左下角檢測
if(j!=5 && ball_y == brick_bottom)//如果不是第5行磚的下側邊
{
if(brick_exist[i+5]){//如果磚[i+5]存在
brick_exist[i+5] = false;
score+=4;
}
}
//朝右正碰2塊磚中間,左上角檢測
else if(j!=1 && ball_y == brick_top)//如果不是第1行磚的上側邊
{
if(brick_exist[i-5]){//如果磚[i-5]存在
brick_exist[i-5] = false;
score+=4;
}
}
}
//朝左碰磚的right面DDDDDDDDDDDDDDDDDD
else if(ball_x >= brick_right && ball_x <= brick_right+ball_r&&
ball_y >= brick_top && ball_y <= brick_bottom)
{
ballLeftOrRightHit();
brick_exist[i] = false;
score+=4;
//朝左正碰2塊磚中間,右下角檢測
if(j!=5 && ball_y == brick_bottom )//如果不是第5行磚的下側邊
{
if(brick_exist[i+5]){//如果磚[i+5]存在
brick_exist[i+5] = false;
score+=4;
}
}
//朝左正碰2塊磚中間,右上角檢測
else if(j!=1 && ball_y == brick_top )//如果不是第1行磚上側邊
{
if(brick_exist[i-5]){//如果磚[i-5]存在
brick_exist[i-5] = false;
score+=4;
}
}
}
/////////////////////////////////////////
//斜碰i磚的左下角EEEEEEEEEEEEEEEEEEEEEEEE
else if(( i-1<0||(i-1>=0&&!brick_exist[i-1]) ) &&
(i+5>=25||(i+5<25&&!brick_exist[i+5]) )&&
Math.pow(brick_left-ball_x, 2)+Math.pow(brick_bottom-ball_y,2)<=Math.pow(ball_r, 2)&&
ball_x>brick_left-ball_r && ball_x<brick_left &&
ball_y>brick_bottom && ball_y<brick_bottom+ball_r )
{
ballcornerHit();
brick_exist[i] = false;
score+=4;
}
//斜碰i磚的右下角FFFFFFFFFFFFFFFFFFFFFFFFFF
else if( (i+1>=25||(i+1<25&&!brick_exist[i+1]) )&&
(i+5>=25||(i+5<25&&!brick_exist[i+5]) )&&
Math.pow(brick_right-ball_x, 2)+Math.pow(brick_bottom-ball_y,2)<=Math.pow(ball_r, 2)&&
ball_x>brick_right&&ball_x<brick_right+ball_r&&
ball_y>brick_bottom&&ball_y<brick_bottom+ball_r )
{
Log.v("----------", "right bottom hit"+i+":"+brick_exist[i]);
ballcornerHit();
brick_exist[i] = false;
score+=4;
}
//斜碰i磚的右上角GGGGGGGGGGGGGGGGGGGGGGGG
else if( (i+1>=25||(i+1<25&&!brick_exist[i+1]) )&&
(i-5<0||(i-5>0&&!brick_exist[i-5]) )&&
Math.pow(brick_right-ball_x, 2)+Math.pow(brick_top-ball_y,2)<=Math.pow(ball_r, 2)&&
ball_x>brick_right && ball_x<brick_right+ball_r&&
ball_y>brick_top-ball_r && ball_y<brick_top)
{
ballcornerHit();
brick_exist[i] = false;
score+=4;
}
//斜碰i磚的左上角HHHHHHHHHHHHHHHHHHHHHHHHHH
else if((i-1<0||(i-1>=0&&!brick_exist[i-1])) &&
(i-5<0||(i-5>=0&&!brick_exist[i-5])) &&
Math.pow(brick_left-ball_x, 2)+Math.pow(brick_top-ball_y,2)<=Math.pow(ball_r, 2)&&
ball_x>brick_left-ball_x && ball_x<brick_left &&
ball_y>brick_top-ball_r && ball_y<brick_top )
{
ballcornerHit();
brick_exist[i] = false;
score+=4;
}
}//end if
}//end for
}//end hitBrickCheck()
//磚塊碰撞檢測結束-----------------------------------------------------------
//------------------------------------------
public boolean gameOver(){
int count = 0;
for(boolean s:brick_exist){
if(!s){
count++;
}
}
if(count == 25){
return true;
}else{
return false;
}
}
//------------------------------------------
//---------------繪圖方法開始------------------------
public void Draw() {
// 鎖定畫布,得到canvas
Canvas canvas = mSurfaceHolder.lockCanvas();
if (mSurfaceHolder == null || canvas == null) {
return;
}
// 繪圖
Paint mPaint = new Paint();
// 設置取消鋸齒效果
mPaint.setAntiAlias(true);
mPaint.setColor(Color.BLACK);
// 繪制矩形--背景
canvas.drawRect(0, 0, screen_width, brick_height-2, mPaint);
mPaint.setColor(Color.GREEN);
// 繪制矩形--背景
canvas.drawRect(0, brick_height-2, screen_width, screen_height, mPaint);
mPaint.setColor(Color.RED);//設置字體顏色
mPaint.setTextSize(brick_height-7);//設置字體大小
canvas.drawText("得分:"+score, 0, brick_height-7, mPaint);
// 繪制頂層擋板````````````````````````````````````````````````````````25號修改
mPaint.setColor(Color.BLACK);//設置顏色
mPaint.setStrokeWidth(4);//設置粗細
canvas.drawLine(0, brick_height-2, screen_width, brick_height-2, mPaint);
for (int i = 0; i <25 ; i++) {
if (brick_exist[i]) {
k = i % 5+1;// 1,2,3,4,5循環
j = i / 5+1;// 1,1,1,1,1;2,2,2,2,2,;...;5,5,5,5,5
brick_left = brick_width*(k-1);
brick_right = brick_width*k;
brick_top = brick_height*j;
brick_bottom = brick_height*(j+1);
mPaint.setStyle(Paint.Style.FILL);// 設置實心畫筆
mPaint.setColor(Color.YELLOW);
canvas.drawRect(brick_left+1, brick_top+1, brick_right-1,brick_bottom-1, mPaint);
mPaint.setStyle(Paint.Style.STROKE);// 設置空心畫筆
mPaint.setStrokeWidth(2);//設置粗細
mPaint.setColor(Color.BLUE);
canvas.drawRect(brick_left+3, brick_top+3, brick_right-3,brick_bottom-3, mPaint);
}
}
// 設置實心畫筆
mPaint.setStyle(Paint.Style.FILL);
{
mPaint.setShader(mRadialGradient);
canvas.drawCircle(ball_x, ball_y, ball_r, mPaint);
}
Paint mPaint2 = new Paint();
// 設置取消鋸齒效果
mPaint2.setAntiAlias(true);
// 設置實心畫筆
mPaint2.setStyle(Paint.Style.FILL);
{
mPaint2.setColor(Color.BLACK);
/* 繪制矩形擋板 */
canvas.drawRect(board_left, board_alterable_top, board_right, board_alterable_bottom,mPaint2);
float board2_bottom = screen_height - 2*ball2_r;//164
float board2_top = board2_bottom - 4;//160
//線段端點坐標數組
float x0 = board_left+(board_right-board_left)/4;
float y0 = board_alterable_bottom;//440或者444
float springAdd = (board2_top- board_alterable_bottom)/8;//即2.5或者2:彈簧小線段的y向長度
float springWidth = 5.0f;//彈簧小線段的x向長度
float x1 = x0 - springWidth;
float y1 = y0 + springAdd;
float x2 = x0 + springWidth;
float y2 = y0 + 3*springAdd;
float x3 = x1 ;
float y3 = y0 + 5*springAdd;
float x4 = x2;
float y4 = y0 + 7*springAdd;
float x5 = x0 ;
float y5 = board2_top;//即460
float between_spring = (board_right-board_left)/2;
float pts[] = { x0,y0,x1,y1,
x1,y1,x2,y2,
x2,y2,x3,y3,
x3,y3,x4,y4,
x4,y4,x5,y5};
float pts2[] = {x0+between_spring,y0, x1+between_spring,y1,
x1+between_spring,y1, x2+between_spring,y2,
x2+between_spring,y2, x3+between_spring,y3,
x3+between_spring,y3, x4+between_spring,y4,
x4+between_spring,y4, x5+between_spring,y5};
mPaint2.setStrokeWidth(2);//設置彈簧粗細
//繪制2個彈簧
canvas.drawLines(pts, mPaint2);
canvas.drawLines(pts2, mPaint2);
//繪制下層擋板
canvas.drawRect(board_left, board2_top, board_right, board2_bottom,mPaint2);
mPaint2.setColor(Color.BLACK);//
// 繪制最下面的兩個“輪子”(圓心x,圓心y,半徑r,p)
canvas.drawCircle(board_left+ball2_r, screen_height-ball2_r, ball2_r, mPaint2);//圓
canvas.drawCircle(board_right-ball2_r, screen_height-ball2_r, ball2_r, mPaint2);
mPaint2.setColor(Color.WHITE);//
canvas.drawPoint(board_left+ball2_r, screen_height-ball2_r, mPaint2);//繪制左輪輪心
canvas.drawPoint(board_right-ball2_r, screen_height-ball2_r, mPaint2);//繪制右輪輪心
}//實心畫筆mPaint2結束
if(gameOver()){
// mbLoop = false;//注釋掉的話GAME OVER後也可重啟
ball_isRun = false;
mPaint2.setColor(Color.BLACK);//設置字體顏色
mPaint2.setTextSize(40.0f);//設置字體大小
canvas.drawText("GAME OVER", screen_width/32+40, screen_height/16*9-70, mPaint2);
mPaint2.setTextSize(20.0f);//設置字體大小
canvas.drawText("Press \"MENU\" button to restart,",
screen_width/32, //320/32=10
screen_height/16*9, //480/16=30,30*9=270
mPaint2);
canvas.drawText("Press \"BACK\" button to exit.",
screen_width/32, //320/32=10
screen_height/16*10, //480/16=30,30*10=300
mPaint2);
}
// 繪制後解鎖,繪制後必須解鎖才能顯示
mSurfaceHolder.unlockCanvasAndPost(canvas);
}// end of Draw()
//---------------------繪圖方法結束-----------------------------------------------
// 在surface的大小發生改變時激發
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
// 在surface創建時激發
public void surfaceCreated(SurfaceHolder holder) {
new Thread(this).start();// 開啟繪圖線程
}
// 在surface銷毀時激發
public void surfaceDestroyed(SurfaceHolder holder) {
// 停止循環
mbLoop = false;
}
}
下個界面的代碼是
package ab.cc;
import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
public class BaomingActivity extends Activity
{
BallView myView;
static int screenWidth;
static int screenHeight;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
/* 定義DisplayMetrics對象 */
DisplayMetrics dm = new DisplayMetrics();
/* 取得窗口屬性 */
getWindowManager().getDefaultDisplay().getMetrics(dm);
/* 窗口的寬度 */
screenWidth = dm.widthPixels;
/* 窗口的高度 */
screenHeight = dm.heightPixels;
//
setTitle("寬"+screenWidth+" 高"+screenHeight);
/* 設置為無標題欄 */
requestWindowFeature(Window.FEATURE_NO_TITLE);
/* 設置為全屏模式 */
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
myView = new BallView(this);
//設置顯示GameSurfaceView視圖
setContentView(myView);
}//end of onCreate()
//觸筆事件
public boolean onTouchEvent(MotionEvent event)
{
return true;
}
//按鍵按下事件
public boolean onKeyDown(int keyCode, KeyEvent event)
{
switch (keyCode)
{
//菜單按鍵-設為初始菜單
case KeyEvent.KEYCODE_MENU:
myView.resetGame();
//重新開始
break;
//中間按鍵
case KeyEvent.KEYCODE_DPAD_CENTER:
myView.ball_isRun = !myView.ball_isRun;//開始//暫停
break;
//左方向鍵
case KeyEvent.KEYCODE_DPAD_LEFT:
if(myView.ball_isRun){
if(myView.board_left<=myView.board_x_move)
{
myView.board_left=0;
myView.board_right=myView.board_length;
}else{
myView.board_left-=myView.board_x_move;
myView.board_right-=myView.board_x_move;}
}
break;
//右方向鍵
case KeyEvent.KEYCODE_DPAD_RIGHT:
if(myView.ball_isRun){
if(screenWidth-myView.board_right<=myView.board_x_move )
{
myView.board_left=screenWidth-myView.board_length;
myView.board_right=screenWidth;
}else{
myView.board_left+=myView.board_x_move;
myView.board_right+=myView.board_x_move;
}
}
break;
//上方向鍵
case KeyEvent.KEYCODE_DPAD_UP:
if(myView.ball_isRun){
if( myView.board_alterable_top==myView.board_default_top)
{
myView.board_alterable_top-=myView.boardYadd;
myView.board_alterable_bottom-=myView.boardYadd;
}
}
break;
//下方向鍵
case KeyEvent.KEYCODE_DPAD_DOWN:
if(myView.ball_isRun){
if( myView.board_alterable_top==myView.board_default_top-myView.boardYadd )
{
myView.board_alterable_top=myView.board_default_top;
myView.board_alterable_bottom=myView.board_alterable_top+myView.board_thickness;
}
}
break;
//返回鍵
case KeyEvent.KEYCODE_BACK:
this.finish();
break;
}//end switch
return false;
}
//按鍵彈起事件
public boolean onKeyUp(int keyCode, KeyEvent event)
{
return true;
}
public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event)
{
return true;
}
}
kaishi.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
screenWidth = dm.widthPixels;
screenHeight = dm.heightPixels;
setTitle("寬"+screenWidth+" 高"+screenHeight);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
`myview = new BallView(this);` //錯誤在這裡
setContentView(myview);
}
這裡面的這個`myview = new BallView(dazhuankuaiActivity.this);