Hello,
I am making a program similar to Aimbooster where the player has to click on the target (ellipse) before it disappears. At the moment, i got the targets (ellipses) to spawn at random locations using random() on their x and y axis. In addition, I created the expand/shrink animation of the targets.
Currently, I am struggling to let the targets spawn at a given time interval and spawning them until, lets say, when the player loses 3 lives (when one target disappears = 1 life gone). I haven’t coded the lives/player interaction yet as you can see in my code as I am really stuck on the spawning. summary: At the moment, the program spawns 50 targets (ellipses) at the same time but I would like it to spawn at a given time interval and instead be spawned infinite amount of times (not limited to 50) until player loses all lives.
My knowledge is very limited so, if possible, to explain it with basic syntax.
Here’s my sketch’s code:
Target[] target;//declaring array variable
void setup(){
size(1000,1000);//canvas size
target = new Target[50];//creates 50 new targets.
for(int i=0;i<target.length;i++){
target[i] = new Target();//new object
}
frameRate(60);
}
void draw(){
background(0);
for(Target targets: target){
targets.draw();//calls draw() method from Target class
}
}
My Target class code:
class Target{
boolean targetExpand;
float x,y,radius;
color colour;
Target(){
targetExpand = true;
radius = 0;//initial radius for target
colour = 255;
x = random(1000);//random x axis position
y = random(1000);//random y axis position
}
void draw(){
stroke(100);
fill(colour);//initial target's colour
ellipse(x,y, radius, radius);//target's location and size
targetAnimation();//calling targetAnimation method
}
void targetAnimation(){
if (targetExpand && radius<150){//targetExpand = true && radius is less than 150: increment radius
radius++;
}
else{//targetExpend = false: reverse (decrement)
targetExpand = false;
radius--;
}
if (radius==0){ //If radius = 0, targetExpand = true: repeat first if statement
targetExpand = true;
}
}
}
I don’t know how to start, that’s why you won’t see any attempts…well, I did attempt several times but I deleted it since it doesn’t make sense lol.
First you will need to change your data structure in order to have as many target as you want. Instead of using a simple array, you can use an arrayList
So you would need to replace that:
Target[] target;
by that:
arrayList<Target> target;
Then to handle the creation of your target, we can create a dedicated method:
void spawnTarget() {
target.add(new Target());
}
Now for the timing, we’ll need to store the last time a target was spawn and so speed at wich you want the target to spawn so add those variables to the beginning of the sketch: int lastTargetSpawn and `int spawnDeltaTime
Now in your draw function you just need to check if the time between the last spawn time and now is superior to the target time and if so spawn a target:
Hi jb4x,
Thanks for taking your time to help me
I temporarily commented out my for loops in my setup() and draw() methods, and added your code in.
When i ran the program, it said NullPointerException and highlighted
from the spawnTarget() method. What went wrong? I’m new to this arraylist so i’m not sure how to tackle this
I looked at the @GoToLoop solution but i prefer not to use a custom library until I learn the fundamentals lol
Thanks, it got rid of the error.
However, when I run the sketch, nothing spawns.
Do i need to set a value for lastTargetSpawn or spawnDeltaTime variable?
Sorry if I may seen as being spoon fed, I just can’t wrap myself around with programming…