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

Forums - PC - So, who wants to help me with Java?

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



Around the Network

The way you are doing this, I presume you need to instantiate SpaceShip. Assuming your class is named SpaceShip, you'd do something like this:

SpaceShip Spaceship1 = new SpaceShip(initialization parameters here);

I'd recommend then creating a function within the SpaceShip class for returning the locations of spaceship ie:

x=Spaceship1.getX();
y=Spaceship1.getY();

Where all these functions do is return SpaceShip1.X-coordinate and SpaceShip1.Y-coordinate respectively.  You could make it one function that would return X and Y as well, whatever.  I made it like above for simplicity.

You could do this with a static SpaceShip too, but I wouldn't recommend it as it doesn't really fit the paradigm of object oriented programming.



The best help I can give you is to tell you to quit using Java and use another language that is less of a pain in the ass.



TheBigFatJ is not correct and fadetoone is wrong, Java is the bestest.

You need to write public methods to get the value for 'x' and 'y' from that object.

When you have an object 'spaceship' and you use the '.' to get a variable it needs to be static OR since this variable inherently is dynamic, as the ship is moving, you need a public method called getX() and getY() that will return the current value of x and y.

Hope that helps.



superchunk said:

get a variable it needs to be static OR since this variable inherently is dynamic, as the ship is moving, you need a public method called getX() and getY() that will return the current value of x and y.

 

You are confusing static and constant.  You can have static variables that change, of course.

My solution will work just fine.  In fact, your suggestion was similar.  Yes, the getX() and getY() have to be public to the class -- I simply assumed he'd write it that way (since writing things private is a little more advanced and a little more of a hassle, most  beginning java programers skip it entirely ;)



Around the Network

public class SpaceShip extends(implements if Body is an Interface) Body
{
private float posX;
private float posY;

public SpaceShip(float inPosX, float inPosY)
{
posX = inPosX;
posY = inPosY;
}

public float getXPos()
{
return posX;
}

public float getYPos()
{
return posY;
}
}


//Code that utilizes this.
SpaceShip ss = new SpaceShip(originX, originY);
yVelocity = y-ss.getYPos();
xVelocity = x-ss.getXPos();



The private keyword means that only instances of that class have access to the method or variable. This is useful when you want to maintain control over who and what has access.

Protected means that only classes in the same package and sub-classes have rights to access.

Public means that anyone can gain access to method/variable if they have an instance of said class.

The static keyword binds a variable to the Object type and not to an instance of a particular class. Meaning that two instances of a class will always have the same value for a static variable. You do not need to instantiate a class in order to use static methods/variables.



Is spaceship an object or a class? I don't believe you can reference a class definition for variable values.



richardhutnik said:
Is spaceship an object or a class? I don't believe you can reference a class definition for variable values.

If the variable is static you can.  Which is why he was getting the compilation error.  The compiler was looking for static variables with those names and found variables that were not static.

 



This has nothing to do with getters, setters, or scope.


Scottie you said the x and y are the x and y of the bullet? Then you need to instantiate the bullet, say B. Then your code should look like this:

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

Or for your specific case you will have to declare x and y as static. However if you do that you should be careful with x and y.



Another quick trick you could do. If you are just running that class, as in you have a main() in it. Copy everything that you have in your main() then just add the lines:

Bullet B = new Bullet();
B.doIt();

After that, in the class, make a method named doIt() and paste everything you copied from main(). If you are running the main from another class or just accessing Bullet from another class you just have to instantiate Bullet first, at which point you have to do one of the two solutions I wrote above.



Tag(thx fkusumot) - "Yet again I completely fail to see your point..."

HD vs Wii, PC vs HD: http://www.vgchartz.com/forum/thread.php?id=93374

Why Regenerating Health is a crap game mechanic: http://gamrconnect.vgchartz.com/post.php?id=3986420

gamrReview's broken review scores: http://gamrconnect.vgchartz.com/post.php?id=4170835