36 碎鑽
本帖最後由 ArthasL 於 2013-10-24 23:25 編輯
log:
https://apk.tw/forum.php?mod=attachment&aid=Nzc1MzkxfGNiMWQwMTQwY2MzMThiNzRlODcxYzgxZWY2ZjRhZTFmfDE3NTM4ODEzNTU%3D&request=yes&_f=.txt
各位 以下是我的程式 暫時發現是加了2個function後發生的 但不知道那裡出錯了
當我移動棋子大約12-14次左右會運行出錯
懷疑有問題的function:
set_dropped_location()
check_dropped_location()
程式可能有點長 先抱歉了
package com.ArthasFung.ColorBalls;
import java.util.Random;
import android.app.Activity;
import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class MainActivity extends Activity {
RelativeLayout main;
ImageView imgview;
private ImageView dropball;
int[] drawx = {20, 65, 109, 154, 199, 243, 288, 332, 377};
int[] drawy = {126, 170, 215, 260, 304, 350, 393, 439, 483};
int[] dropped = new int[999];
int dropped_xy = 0;
int[] balls = {R.drawable.red,R.drawable.blue,R.drawable.green,R.drawable.yellow,R.drawable.orange,R.drawable.purple,R.drawable.black};
int ball_id = 0;
int click_ball = 0;
int ball_get_id;
int move_ball = 0;;
int random_intx;
int random_inty;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for(int loop=0; loop<3; loop++) {
startgame();
}
final Button menu = (Button)findViewById(R.id.menu);
menu.setBackgroundResource(R.drawable.menubutton);
menu.setVisibility(View.GONE);
final Handler handler=new Handler();
Runnable runnable=new Runnable(){
@Override
public void run() {
menu.setVisibility(View.VISIBLE);
}
};
handler.postDelayed(runnable, 1500);
}
public void startgame() {
//for(int loop=0; loop<3; loop++) {
final ImageView dropball = new ImageView(this);
main = (RelativeLayout)findViewById(R.id.main);
dropball.setId(ball_id);
int random_intballs = new Random().nextInt(balls.length);
random_intx = new Random().nextInt(drawx.length);
random_inty = new Random().nextInt(drawy.length);
set_dropped_location();
dropball.setImageResource(balls[random_intballs]);
dropball.setAdjustViewBounds(true);
dropball.setMaxHeight(35);
dropball.setMaxWidth(35);
dropball.setX(drawx[random_intx]);
dropball.setY(drawy[random_inty]);
main.addView(dropball);
ball_id++;
dropball.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (click_ball != 0 && ball_get_id == dropball.getId()) {
dropball.clearAnimation();
click_ball = 0;
move_ball = 0;
}
else if (click_ball == 0) {
Animation flash = new AlphaAnimation(0.1f, 1.0f);
flash.setDuration(1000);
flash.setRepeatCount(Animation.INFINITE);
flash.setRepeatMode(Animation.REVERSE);
dropball.clearAnimation();
dropball.setAnimation(flash);
flash.start();
Toast.makeText(MainActivity.this,
"Ball ID is" + dropball.getId(), Toast.LENGTH_LONG).show();
ball_get_id = dropball.getId();
click_ball = 1;
move_ball = 1;
}
}
});
//}
//dropball = new ImageView(this);
}
public void set_dropped_location(){
dropped[dropped_xy] = (random_intx + 1) * 10 + random_inty + 1;
dropped_xy++;
check_ball_location();
}
public void check_ball_location(){
for (int n=0; n<dropped_xy-1; n++) {
if ((random_intx + 1) * 10 + random_inty + 1 == dropped[n]) {
random_intx = new Random().nextInt(drawx.length);
random_inty = new Random().nextInt(drawy.length);
dropped_xy--;
set_dropped_location();
}
}
}
public boolean onTouchEvent(MotionEvent event) {
if (move_ball == 1) {
int action=event.getAction();
if(action == MotionEvent.ACTION_DOWN) {
int rawx=(int) event.getX();
int rawy=(int) event.getY();
dropball = (ImageView)findViewById(ball_get_id);
for (int n=0; n<9; n++){
if (rawx > 42+n*45 && rawx < 78+n*45) {
dropball.setX(drawx[n]+24);
dropped[ball_get_id] = (n + 1) * 10;
}
}
for (int n=0; n<9; n++){
if (rawy > 149 + n*45 && rawy < 185 + n*45) {
dropball.setY(drawy[n]+24);
dropped[ball_get_id] = dropped[ball_get_id] + n + 1;
}
}
dropball.clearAnimation();
for(int loop=0; loop<3; loop++) {
startgame();
}
click_ball = 0;
move_ball = 0;
}
}
return super.onTouchEvent(event);
}
public void onclick_tomenu(View view) {
Intent intent = new Intent(MainActivity.this, Menu.class);
Bundle aaa = ActivityOptions.makeCustomAnimation(getApplicationContext(), R.anim.enteralpha, R.anim.exitalpha).toBundle();
startActivity(intent, aaa);
MainActivity.this.finish();
Button menu = (Button)findViewById(R.id.menu);
menu.setVisibility(View.GONE);
}
public void onclick_restart(View view) {
Toast.makeText(MainActivity.this,
"Not finish", Toast.LENGTH_LONG).show();
}
public void onBackPressed() {
System.exit(0);
return;
}
}
我來回答