ผมมีปัญหาอยู่ที่ว่า
จะเขียนให้มันทำตามเงื่อนไขได้อย่างไร
ในที่นี้ ตัวโปรแกรมมีบอลให้ 3 ลูก แต่ ok ครับ พอลากไปจุดที่กำหนด คือ"Drag Here"
ok ครับ ภาพหายไป
แต่ว่าหากต้องการให้มันทำตามเงื่อนไขคือ ให้ลูกบอลลูกสีแดงลงไปก่อน ตามด้วย เขียว และน้ำเงิน หากครบ ก็ทำการ Intent ไปอีกหน้า
แต่ว่าหากไม่ตรงตามนี้ก็เด้งกลับไปที่เดิมครับ
ไม่ทราบว่าต้องเขียนอย่างไรครับ
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
public class DrawView extends View {
public int[] ball = new int[]
{ R.drawable.icebag, R.drawable.icebagh,R.drawable.icebagh};
public int[][] ball_position = new int[][]
{ { 400, 50 }, { 500, 50 },{ 600, 50 } };
public Bitmap bm[] = new Bitmap[ball.length];
public boolean[] goal = new boolean[ball.length];
public int screen_width, screen_height ;
int goal_line = 200;
int ball_id = -1;
int score = 0;
// int order_ball = 1; //ประกาศไว้ในเรื่องของการทำแบบเงื่อนไข
public DrawView(Context context, Display display) {
super(context);
this.setBackgroundResource(R.drawable.fa9);
screen_width = display.getWidth();
screen_height = display.getHeight();
for(int i = 0 ; i < ball.length ; i++) {
bm = BitmapFactory.decodeResource(getResources(), ball);
goal = false;
}
}
protected void onDraw(Canvas canvas) {
Paint p = new Paint();
p.setColor(Color.rgb(0x3e, 0x9c, 0xbc));
p.setAntiAlias(true);
// canvas.drawRect(0, screen_height - goal_line
// , screen_width, screen_height, p);
// p.setTextSize(50);
// p.setColor(Color.WHITE);
// canvas.drawText("Drag to Here", (screen_width / 2) - 140
// , screen_height - (( goal_line / 2) - 10), p);
// canvas.drawColor(0xFFFFFF);
p.setTextSize(70);
p.setColor(Color.rgb(0x3e, 0x9c, 0xbc));
canvas.drawText(String.valueOf(score), 50, 100, p);
for(int i = 0 ; i < ball.length ; i++) {
if(!goal) {
canvas.drawBitmap(bm, ball_position[0] - (bm.getWidth() / 2)
, ball_position[1] - (bm.getHeight() / 2), null);
}
}
}
public boolean onTouchEvent(MotionEvent event) {
int eventaction = event.getAction();
int X = (int)event.getX();
int Y = (int)event.getY();
switch (eventaction ) {
case MotionEvent.ACTION_DOWN: // touch down so check if the finger is on a ball
ball_id = -1;
for(int i = 0 ; i < ball.length ; i++) {
double radCircle = Math.sqrt( Math.pow(Math.abs(X - ball_position[0]), 2)
+ Math.pow(Math.abs(Y - ball_position[1]), 2) );
if(radCircle < bm.getWidth() / 2 && !goal) {
ball_id = i;
}
}
case MotionEvent.ACTION_MOVE:
if(ball_id != -1 && !goal[ball_id] ) {
ball_position[ball_id][0] = X;
ball_position[ball_id][1] = Y;
}
break;
case MotionEvent.ACTION_UP:
if(ball_id != -1 && !goal[ball_id] ) {
if(Y > screen_height - goal_line) {
goal[ball_id] = true;
score++;
}
}
break;
}
invalidate();
return true;
}
}}
ทำอย่างไรให้ลูกบอลสามารถเด้งกลับที่เดิมได้ (Android)
จะเขียนให้มันทำตามเงื่อนไขได้อย่างไร
ในที่นี้ ตัวโปรแกรมมีบอลให้ 3 ลูก แต่ ok ครับ พอลากไปจุดที่กำหนด คือ"Drag Here"
ok ครับ ภาพหายไป
แต่ว่าหากต้องการให้มันทำตามเงื่อนไขคือ ให้ลูกบอลลูกสีแดงลงไปก่อน ตามด้วย เขียว และน้ำเงิน หากครบ ก็ทำการ Intent ไปอีกหน้า
แต่ว่าหากไม่ตรงตามนี้ก็เด้งกลับไปที่เดิมครับ
ไม่ทราบว่าต้องเขียนอย่างไรครับ
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
public class DrawView extends View {
public int[] ball = new int[]
{ R.drawable.icebag, R.drawable.icebagh,R.drawable.icebagh};
public int[][] ball_position = new int[][]
{ { 400, 50 }, { 500, 50 },{ 600, 50 } };
public Bitmap bm[] = new Bitmap[ball.length];
public boolean[] goal = new boolean[ball.length];
public int screen_width, screen_height ;
int goal_line = 200;
int ball_id = -1;
int score = 0;
// int order_ball = 1; //ประกาศไว้ในเรื่องของการทำแบบเงื่อนไข
public DrawView(Context context, Display display) {
super(context);
this.setBackgroundResource(R.drawable.fa9);
screen_width = display.getWidth();
screen_height = display.getHeight();
for(int i = 0 ; i < ball.length ; i++) {
bm = BitmapFactory.decodeResource(getResources(), ball);
goal = false;
}
}
protected void onDraw(Canvas canvas) {
Paint p = new Paint();
p.setColor(Color.rgb(0x3e, 0x9c, 0xbc));
p.setAntiAlias(true);
// canvas.drawRect(0, screen_height - goal_line
// , screen_width, screen_height, p);
// p.setTextSize(50);
// p.setColor(Color.WHITE);
// canvas.drawText("Drag to Here", (screen_width / 2) - 140
// , screen_height - (( goal_line / 2) - 10), p);
// canvas.drawColor(0xFFFFFF);
p.setTextSize(70);
p.setColor(Color.rgb(0x3e, 0x9c, 0xbc));
canvas.drawText(String.valueOf(score), 50, 100, p);
for(int i = 0 ; i < ball.length ; i++) {
if(!goal) {
canvas.drawBitmap(bm, ball_position[0] - (bm.getWidth() / 2)
, ball_position[1] - (bm.getHeight() / 2), null);
}
}
}
public boolean onTouchEvent(MotionEvent event) {
int eventaction = event.getAction();
int X = (int)event.getX();
int Y = (int)event.getY();
switch (eventaction ) {
case MotionEvent.ACTION_DOWN: // touch down so check if the finger is on a ball
ball_id = -1;
for(int i = 0 ; i < ball.length ; i++) {
double radCircle = Math.sqrt( Math.pow(Math.abs(X - ball_position[0]), 2)
+ Math.pow(Math.abs(Y - ball_position[1]), 2) );
if(radCircle < bm.getWidth() / 2 && !goal) {
ball_id = i;
}
}
case MotionEvent.ACTION_MOVE:
if(ball_id != -1 && !goal[ball_id] ) {
ball_position[ball_id][0] = X;
ball_position[ball_id][1] = Y;
}
break;
case MotionEvent.ACTION_UP:
if(ball_id != -1 && !goal[ball_id] ) {
if(Y > screen_height - goal_line) {
goal[ball_id] = true;
score++;
}
}
break;
}
invalidate();
return true;
}
}}