int missileSpeed = 5; class Missile{ int x,y, tx,ty; Missile(int a, int b, int c, int d){ this.x = a; this.y = b; this.tx = c; this.ty = d; } //Draw and move the missile void process(){ if(this.x < this.tx) this.x += missileSpeed; else if(this.x > this.tx) this.x -= missileSpeed; else this.x = this.tx; if(this.y < this.ty) this.y += missileSpeed; else if(this.y > this.ty) this.y -= missileSpeed; else this.y = this.ty; //Draw the Missile fill(50); rect(this.x, this.y, 10,10); } } class Launcher{ int x,y, count; Launcher(int a, int b){ this.x = a; this.y = b; this.count = 10; } void process(){ fill(255,0,0); rect(this.x, this.y, 10, 20); this.count--; if(this.count <= 0){ this.count = 10; //Make a missile at same position of launcher, set target-> THE MOUSE! missilez.add(new Missile(this.x, this.y, mouseX, mouseY)); } } } ArrayList missilez = new ArrayList(); ArrayList launchy = new ArrayList(); void setup (){ size(400,400); } void draw(){ fill(255); rect(0,0,400,400); for(int i=0; i < launchy.size(); i++) launchy.get(i).process(); for(int i=0; i < missilez.size(); i++) missilez.get(i).process(); for(int i=0; i < missilez.size(); i++){ if(abs(missilez.get(i).x - missilez.get(i).tx) < missileSpeed && abs(missilez.get(i).y - missilez.get(i).ty) < missileSpeed){ missilez.remove(i); i--; } } } void mousePressed(){ launchy.add(new Launcher(mouseX, mouseY)); //Add a launcher //missilez.add(new Missile(100,100, mouseX, mouseY)); //Add a missile //You could also add a missile this way // Missile newMissile = new Missile(100,100, mouseX, mouseY); // missilez.add(newMissile); }