We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello, I wrote this code in which you have a traingle that points towards you mouse around a circle, if you press your left mouse it will shoot a bullet and al of that works fine but when I replace
size(400, 400);
with
fullScreen();
the bullets disapear straight after I shoot them and I cant figure out why. Here's the whole code with 400 x 400 resolution
float angle, rotation, speed, canShootCounter;
int basesize = 250;
int speedfactor = 1;
int version = 3;
int x, y, i;
Player player = new Player();
PVector location;
ArrayList<Bullet> bullets;
boolean canShoot = true;
void setup() {
size(400, 400);
bullets = new ArrayList<Bullet>();
player = new Player();
}
void draw() {
player.update();
background(255);
ellipseMode(CENTER);
ellipse(width/2, height/2, basesize, basesize);
x = (int)(width/2+165*cos(atan2( mouseY - height/2, mouseX - width/2)));
y = (int)(height/2+165*sin(atan2( mouseY - height/2, mouseX - width/2)));
pushMatrix();
translate(x, y);
rotate(atan2( mouseY - height/2, mouseX - width/2)-PI/2);
triangle(-30, -10, 0, +50, +30, -10);
popMatrix();
for (i = bullets.size()-1; i >= 0; i--) {
Bullet bullet = bullets.get(i);
bullet.update();
}
}
class Player {
Player() {
location = new PVector(x, y);
}
void update() {
if (mousePressed == true) {
if (canShoot == true) {
bullets.add( new Bullet());
canShoot = false;
canShootCounter = 0;
}
}
if (canShoot == false) {
canShootCounter ++;
if (canShootCounter == 10){
canShoot = true;
}
}
}
}
class Bullet {
Bullet() {
location= new PVector(x, y);
rotation = atan2( mouseY - height/2, mouseX - width/2 );
speed = 5;
}
void update() {
location.x = location.x + cos(rotation)*speed;
location.y = location.y + sin(rotation)*speed;
ellipse(location.x, location.y, 10, 10);
if (location.x > 0 && location.x < width && location.y > 0 && location.x < height) {
}
else {
bullets.remove(i);
}
}
}
I hope someone can help. :)
Answers
Change your line 75 for
if ((location.x > -width/2 || location.x < width/2 ) || (location.y > -height/2 || location.y < height/2)) {
***Edit: Notice also in your line 75, you mixed x with y in the last comparison.
Kf
Thank you now its working! ;)