Making a zombie survival game and the bullets, after they’re shot, continue to follow the mouse. The PVector “t” cant be initialized in keyReleased but it needs to only give a single value per shot. Instead it keep updating it’s value.
class Bullet
{
PVector pos;
PVector spd;
float damage;
PVector t = null;
PVector d;
boolean setTarg = false;
//constructor
Bullet (float x, float y)
{
pos = new PVector (x, y);
}
void update ()
{
display();
move();
}
//methods
void display ()
{
fill (#EDED54);
ellipse (pos.x, pos.y, 7, 7);
}
void move ()
{
if (t != null)
{
d = PVector.sub(t, pos);
d.setMag(10);
pos.add(d);
}
if (setTarg = true)
{
t = new PVector(mouseX, mouseY);
}
}
boolean gone()
{
if (pos.y <= 0 || pos.y >= 600 || pos.x >= 800 || pos.x <= 0)
return true;
return false;
}
void keyReleased ()
{
if (key == ' ')
{
setTarg = true;
} else
{
setTarg = false;
}
}
}
it’s better when you post an entire Sketch - then we can run it. (or a runable minimal version)
In general, I’d say, when you click the mouse (mousePressed, not keyReleased) you init a new Bullet
(in an ArrayList) and pass to it once and for all the current mouse position as a target. Then the bullet flies to the target.
Chrisir
Heres the main class. It’s also keyReleased because I want the spacebar to be what shoots the bullets.
//ArrayLists
ArrayList<Bullet> bList = new ArrayList<Bullet>();
ArrayList<Zombie> zList = new ArrayList<Zombie >();
//global variables
int stage = 1;
int score = 0;
int timer;
//fonts
PImage background1;
PImage background2;
//PFont font;
//player
Player player;
//-------------------------------------------------------------------
void setup ()
{
size (800, 600);
background1 = loadImage ("zombie.png");
background2 = loadImage ("sand.png");
//font = loadFont("text.ttf");
//Player
player = new Player ();
createZombies();
}
//-------------------------------------------------------------------
void draw ()
{
stages ();
}
//-------------------------------------------------------------------
void stages ()
{
if (stage == 0) //homescreen
{
image (background1, 0, 0);
//createFont(
} else if (stage == 1) //play screen
{
desert ();
timer();
score();
displayZombies();
player.display();
displayBullets();
}
}
//-------------------------------------------------------------------
void desert ()
{
image (background2, 0, 0);
}
//-------------------------------------------------------------------
void createZombies()
{
for (int i=0; i<8; i++)
{
for (int j=0; j<3; j++)
{
Zombie z = new Zombie (i*80+100, j*50+50);
zList.add(z);
}
}
}
//-------------------------------------------------------------------
void displayZombies()
{
for (int i=0; i<zList.size(); i++)
{
Zombie z = zList.get(i);
z.update();
}
}
//-------------------------------------------------------------------
void displayBullets()
{
for (int i=0; i<bList.size(); i++)
{
Bullet b = bList.get(i);
b.update();
if (b.gone()) bList.remove(b);
}
}
//-------------------------------------------------------------------
void keyReleased ()
{
if (key == ' ')
{
Bullet b = new Bullet(player.pos.x, player.pos.y);
bList.add(b);
}
}
//-------------------------------------------------------------------
void timer ()
{
timer = millis();
fill (0);
textSize(30);
text ("TIME:", 10,30);
text (timer/1000+"s", 85, 30);
}
//-------------------------------------------------------------------
void score ()
{
fill (0);
textSize(30);
text ("SCORE:"+score,640,30);
}
//-------------------------------------------------------------------
void mouseReleased ()
{
}
//-------------------------------------------------------------------
There are these 2 very old sketches that set a mouse aim trajectory:
- Multiple Bullets
- Spit Fire →
http://Studio.ProcessingTogether.com/sp/pad/export/ro.91nVQMnL$v06L