بروز خطا
[message]
اشتراک در سوال
رای ها
[dataList]
ترکیب پروژه Bouncing Ball و باران
سلام
من بعد از دیدن اموزش bouncing ball و باران این به فکرم رسید که یه برنامه بنویسم که این دوتا رو ترکیب کنم. به این صورت با هر کلیک روی صفحه در محل کلیک یه توپ جدید با رنگ رندوم اینجا بشه و بقیه عمل مشابه پروژه bouncing ball باشه و توپ به سمت پایین حرکت و... . اما حالا هرچی که کار میکنم برنامه به صورت صحیح کار نمیکنه. چند توپ با رنگ های رندوم ایجاد میشه ولی نه در محل کلیک
package com.mohsen.customcontrol;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;
public class BounceBallView extends ImageView {
private Paint circlePaint;
private float x = 0;
private float y = 0;
private float y0 = 0;
private float v0 = 0;
private float t = 0;
private float direction = 1.0f;
private long jumpTime;
private static final int STROKE_WIDTH = 2;
private static final int radius = 5;
private ArrayList<Balls> balls = new ArrayList<Balls>();
private static class Balls {
private float x;
private float y;
private int color = Color.rgb((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255));
}
public BounceBallView(Context context) {
super(context);
initialize(context);
}
public BounceBallView(Context context, AttributeSet attrs) {
super(context, attrs);
initialize(context);
}
public BounceBallView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initialize(context);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
updatePhysic();
updateRender();
}
private void initialize(Context context) {
circlePaint = new Paint();
circlePaint.setAntiAlias(true);
circlePaint.setStrokeWidth(STROKE_WIDTH);
circlePaint.setStyle(Style.FILL_AND_STROKE);
updatePhysic();
updateRender();
}
private Thread physicThread;
private Thread renderThread;
private void updatePhysic() {
for (final Balls ball: balls) {
physicThread = new Thread(new Runnable() {
@Override
public void run() {
jumpTime = System.currentTimeMillis();
while (true) {
try {
Thread.sleep(5);
x += direction;
if (x >= getWidth() - radius || x <= radius) {
direction *= -1;
}
t = (System.currentTimeMillis() - jumpTime) / 100.0f;
y = 0.5f * 9.8f * t * t + v0 * t + y0;
if (y >= getHeight() - radius) {
jumpTime = System.currentTimeMillis();
v0 = -80.0f;
y0 = getHeight() - radius;
}
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
physicThread.start();
}
}
private void updateRender() {
renderThread = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(30);
postInvalidate();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
renderThread.start();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
Balls ball = new Balls();
ball.x = event.getX();
ball.y = event.getY();
x = ball.x;
y = ball.y;
balls.add(ball);
updatePhysic();
updateRender();
invalidate();
break;
}
}
return true;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (Balls ball: balls)
{
canvas.drawCircle(x, y, 5, circlePaint);
circlePaint.setColor(ball.color);
}
}
}
کد رو بالا نوشتم ولی چون خیلی طولانیه میتونید فایلش رو از اینجا دانلود کنید
http://uplod.ir/xuctprh6po1x/BounceBallView.java.htm
میدونم کار خیلی سختیه ولی اگه کمکم کنید خیلی خوشحال میشم
+1
0
اگر در محل کلیک تولید نمیشه پس الگوریتم شما ایراد داره . اگر این محل کلیک شدن با محل ترسیم توپ کمی فرق داره مثلا جایی که کلیک میکنید بغلش توپ رسم میشه پس شما باید مرکز توپ رو محاسبه کنید و از اون قسمت بگید رسمش انجام بشه . (9 سال پیش)
برای این سوال پاسخی وجود ندارد.
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .