I have a moving image as a background
PImage background;
int x=0; //global variable background location
boolean up;
boolean down;
Rocket myRocket;
Alien alien1,alien2;
void setup(){
size(800,400);
background = loadImage("spaceBackground.jpg");
background.resize(width,height);
myRocket = new Rocket();
alien1 = new Alien(800,200,4,-3);
alien2 = new Alien(800,200,5,2);
}
void draw ()
{
image(background, x, 0); //draw background twice adjacent
image(background, x+background.width, 0);
x -=4;
if(x == -background.width)
x=0; //wrap background
myRocket.run();
alien1.run();
alien2.run();
}
void keyPressed(){
if(keyCode == UP)
{
up = true;
}
if(keyCode == DOWN)
{
down = true;
}
}
void keyReleased(){
if(keyCode == UP)
{
up = false;
}
if(keyCode == DOWN)
{
down = false;
}
}
First Class. The alien goes up and down towards the rocket.
class Alien {
int x;
int y;
int speedX,speedY;
Alien(int x,int y,int dx,int dy){
this.x = x;
this.y = y;
this.speedX = dx;
this.speedY = dy;
}
void run(){
alien();
restrict();
}
void alien(){
fill(0,255,0);
ellipse(x,y,30,30);
fill(50,100,0);
ellipse(x,y,50,15);
x = x - speedX;
y = y + speedY;
}
void restrict(){
if (y < 15 || y > 380 ){
speedY = speedY * -1;
}
if (x == 0){
x = 800;
}
}
}
Second Class. You control the rocket going up and down
class Rocket {
int x;
int y;
int speedy;
Rocket(){
x = 40;
y = 200;
speedy = 3;
}
void run(){
defender();
move();
restrict();
}
void defender(){
fill(255,0,0);
rect(x,y,50,20);
triangle(x+50,y,x+50,y+20,x+60,y+10);
fill(0,0,255);
rect(x,y-10,20,10);
}
void move() {
if(up)
{
y = y - speedy;
}
if(down)
{
y = y + speedy;
}
}
void restrict(){
if (y < 10) {
y = y + speedy;
}
if (y > 380) {
y = y - speedy;
}
}
boolean IsShot(Rocket myRocket){
if (alien1.x == 40)
{
if(alien1.y>=y && alien1.y<=(y+50))
{
return true;
}
return false;
}
}
}
When one of the aliens hit the rocket i want the game to stop. On boolean IsShot(Rocket myRocket)
i keep getting error "The method must return a result type boolean."
alien1.x != 40
? – Lew Blochalien1.x != 40
? Look carefully at your code and trace the path out of the method. – Lew Bloch