Hello everyone, I have a concern, I am trying to do the following: create objects with a tap on the device, but when counting the objects, the onTap function seems to create twice as many objects.
The principal:
import android.view.MotionEvent;
import ketai.ui.*;
KetaiGesture gesture;
ArrayList<Modulo> mds = new ArrayList<Modulo>();
int diam;
void setup() {
fullScreen();
orientation(PORTRAIT);
gesture = new KetaiGesture(this);
textSize(80);
diam = 100;
Modulo m1 = new Modulo(1,10,100);
Modulo m2 = new Modulo(2,50,100);
Modulo m3 = new Modulo(3,100,100);
mds.add(m1);
mds.add(m2);
mds.add(m3);
text(mds.size(),350,200);
}
void draw() {
background(0,255,0);
if (mds.size()>0){
for (int i= mds.size()-1; i>=0; i--) {
Modulo s = mds.get(i);
if (s.isActiva()==false) {
mds.remove(s);
} else {
s.display();
text(mds.size(),100,250);
}
}
}
}
void onTap(float x, float y) {
//agrega dos objetos?? :c
mds.add(new Modulo(mds.size(), x, y));
}
void mousePressed() {
}
void mouseDragged() {
}
public boolean surfaceTouchEvent (MotionEvent event) {
super.surfaceTouchEvent(event);
return gesture.surfaceTouchEvent(event);
}
The modulo:
class Modulo{
PVector pos;
int id;
boolean activa;
Modulo(int id_,float x, float y){
id=id_;
pos = new PVector (x,y);
activa = true;
}
void setActiva(boolean activa_){
activa = activa_;
}
void update(){
pos.x = mouseX;
pos.y = mouseY;
}
void display(){
pushStyle();
stroke(245,0,0);
fill(255,0,0);
ellipse(pos.x,pos.y,diam,diam);
popStyle();
text(id,pos.x,pos.y);
}
boolean isActiva(){
return activa;
}
boolean over () {
if (dist(mouseX, mouseY, pos.x, pos.y) < diam/2) {
return true;
} else {
return false;
}
}
void mouseDragged(){
if (dist(mouseX, mouseY, pos.x, pos.y) > diam/2)
return; //
update();
}
void mousePressed(){
if (dist(mouseX, mouseY, pos.x, pos.y) > diam/2)
return;
update();
}
}
Is there a way to create a single object with the onTap function?