By using this site, you agree to our Privacy Policy and our Terms of Use. Close

Hey all. I'm trying to program a bitching game in Java, but am being held back by how lazy I was when I learnt java.

 

Anyway, I basically have Enemies, bullets and the player character(called SpaceShip), all of which inherit stuff from a class called body. I then have a class called game that gathers info and steps these things by changing them a small amount.

 

At the moment, I have the enemies firing in a fixed direction (I started with some Space Invaders code that I did for class) using

    public Double yVelocity = 3.0;
    public Double xVelocity = 3.0;

which is within the bullet class.

 

But then when I try to change it to (yes, I'm aware that this will give a variable speed for the bullets, atm I just want to get the direction right)

    public Double yVelocity = y-SpaceShip.y;
    public Double xVelocity = x-SpaceShip.x;

where x and y are the co-ordinates of the bullet and

SpaceShip.x and SpaceShip.y are the x and y co-ordinates of the (top left of the) SpaceShip

I get the error messege

Bullet.java:23: non-static variable y cannot be referenced from a static context
    public Double yVelocity = y-SpaceShip.y;

And an equivalent error for the x

 

Can anyone help me out?

Thanks for your time