Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.dotjogoqualquer;
- import java.util.ArrayList;
- import java.util.Random;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.util.Log;
- import android.view.MotionEvent;
- import android.view.View;
- public class JogoView extends View implements Runnable{
- private static final int INTERVALO = 50;
- private boolean running = true;
- private Paint paint;
- private Thread Jogo_Thread;
- private int adicionados = 0;
- private ArrayList<objetos> obj = new ArrayList<objetos>();
- public JogoView(Context context) {
- super(context);
- paint = new Paint();
- Jogo_Thread = new Thread(this);
- Jogo_Thread.setPriority(Thread.MIN_PRIORITY);
- Jogo_Thread.start();
- }
- @Override
- public void run() {
- while (running){
- try {
- Thread.sleep(INTERVALO);
- } catch (InterruptedException e) {
- Log.e("THREAD", e.getMessage());
- }
- update();
- }
- }
- public void update() {
- if (obj.size() >= 1){
- for(int x = 0; x < obj.size(); x++) {
- obj.get(x).y -= 15;
- if (obj.get(x).y < -50) {
- obj.remove(x); // <<<=================== AQUI
- }
- }
- }
- postInvalidate();
- }
- public void draw(Canvas canvas) {
- super.draw(canvas);
- canvas.drawColor(Color.BLACK);
- for(objetos e: obj) {
- paint.setColor(e.cor);
- canvas.drawCircle(e.x, e.y, e.raio, paint);
- }
- }
- public void release() {
- running = false;
- }
- public boolean onTouchEvent(MotionEvent event) {
- // ......
- switch(event.getAction()) {
- case MotionEvent.ACTION_DOWN:
- int touchX = (int) event.getX();
- int touchY = (int) event.getY();
- obj.add(new objetos());
- obj.get(adicionados).x = touchX;
- obj.get(adicionados).y = touchY;
- obj.get(adicionados).raio = 10;
- obj.get(adicionados).CorAleatoria();
- adicionados++;
- break;
- }
- return super.onTouchEvent(event);
- }
- }
- class objetos{
- int x;
- int y;
- int raio;
- int cor = Color.BLUE;
- public void CorAleatoria() {
- int[] cores = {Color.BLUE, Color.RED, Color.YELLOW, Color.GREEN};
- int ale = new Random().nextInt(cores.length);
- this.cor = cores[ale];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement