We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm learning Processing by programming a very basic shoot'em'up. Here is the code :
game.pde
PlayerShip theShip;
void setup() {
size(1024, 768);
theShip = new PlayerShip(0, height-80);
}
void draw() {
background(0);
theShip.draw();
}
void keyPressed() {
if (key == CODED) {
if (keyCode == LEFT) {
theShip.setLeft(true);
} else if (keyCode == RIGHT) {
theShip.setRight(true);
}
}
if (key == ' ') {
theShip.setFire(true);
}
}
void keyReleased() {
if (key == CODED) {
if (keyCode == LEFT) {
theShip.setLeft(false);
} else if (keyCode == RIGHT) {
theShip.setRight(false);
}
}
if (key == ' ') {
theShip.setFire(false);
}
}
MoveableObject.pde
abstract class MoveableObject {
protected PVector position;
protected PVector velocity;
protected PImage img;
MoveableObject(float startx, float starty) {
position = new PVector(startx, starty);
}
MoveableObject(PVector startPosition) {
position = startPosition;
}
protected void init(String image, float velX, float velY) {
img = loadImage(image);
velocity = new PVector(velX, velY);
}
protected abstract void update();
public void draw() {
if (position.x < 0) {
position.x = 0;
}
if (position.x > width-img.width) {
position.x = width-img.width;
}
if (position.y < 0) {
position.y = 0;
}
if (position.y > height-img.height) {
position.y = height-img.height;
}
image(img, position.x, position.y);
}
}
PlayerBullet.pde
class PlayerBullet extends MoveableObject {
private final float VELOCITY_X= 0;
private final float VELOCITY_Y = -5;
private boolean visible = false;
PlayerBullet(float startx, float starty) {
super(startx, starty);
init("bullet.png", VELOCITY_X, VELOCITY_Y);
}
PlayerBullet(PVector startPosition) {
super(startPosition);
init("bullet.png", VELOCITY_X, VELOCITY_Y);
}
private void setPosition(float x, float y) {
position.x = x;
position.y = y;
}
public boolean isVisible() {
return visible;
}
public void hide() {
visible = false;
}
public void show(PVector startPosition) {
visible = true;
setPosition(startPosition.x, startPosition.y);
}
public void update() {
position.add(velocity);
if (position.y < 10) {
hide();
}
}
@ Override
public void draw() {
update();
super.draw();
}
}
PlayerShip.pde
class PlayerShip extends MoveableObject {
private final int VELOCITY_X = 10;
private final int VELOCITY_Y = 0;
private PlayerBullet bullet;
private boolean turnLeft;
private boolean turnRight;
private boolean fired;
PlayerShip(int startx, int starty) {
super(startx, starty);
init("spaceship2.png", VELOCITY_X, VELOCITY_Y);
}
PlayerShip(PVector startPosition) {
super(startPosition);
init("spaceship2.png", VELOCITY_X, VELOCITY_Y);
}
private PVector getBulletStartPosition() {
return new PVector(position.x + img.width/2 -10 , position.y - 25);
}
public void setLeft(boolean value) {
turnLeft = value;
}
public void setRight(boolean value) {
turnRight = value;
}
public void setFire(boolean value) {
fired = value;
}
public void update() {
if (turnLeft) {
velocity.x = VELOCITY_X * -1;
position.add(velocity);
}
if (turnRight) {
velocity.x = VELOCITY_X;
position.add(velocity);
}
if (fired) {
bullet.show(getBulletStartPosition());
}
}
@ Override
public void init(String image, float velX, float velY) {
super.init(image, velX, velY);
bullet = new PlayerBullet(getBulletStartPosition());
}
@ Override
public void draw() {
update();
super.draw();
if (bullet.isVisible()) {
bullet.draw();
}
}
}
Here is my problem : when I press my space bar, the bullet is stuck in front of the ship. I have to release the space bar for the bullet to move. I can't figure this out. Can you help me ?
Thanks in advance
Answers
Please format your code..
Sorry for the formatting. However, it seems that the @Override keyword is not correctly parsed
Select code and press "C" in the toolbar of the comment section to format your code. Press two time get better indentation
Dont set theShip.setFire(false); when keyReleased. Set this false after it collides with the ship. Simple :)
Thank you for your answer :-)
But it doesn't work : when I remove this line, the bullet doesn't start. It stays in front of the ship and never start to move upwards, whether I release the bar or not :-(
can you share folder will all the zip. I cant find images so I cant run your code.
Here it is : https://drive.google.com/open?id=0BwiL3dZGFyLCWU0wOWJxdC1MS00
Thank you for having a look :-)
Just copy this code into you game.pde
This forum has some glitches, sad! Just place an extra space between
@
&Override
. ~:>BtW, it's not a keyword but an annotation interface: ;;)
http://docs.Oracle.com/javase/8/docs/api/java/lang/Override.html
http://docs.Oracle.com/javase/tutorial/java/annotations/index.html
All Java keywords are lowercase too:
http://docs.Oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html
2 online "bullet" examples: :D
you are loading a new image every time you create a bullet? that's gonna drag.
do all your image loading in setup. use a global PImage. reference that in your bullet class.