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?

Also, don't use Double unless it is necessary, instead use double. double is a primitive, and Double is a wrapper object for a double primitive.

And vlad321, it has everything to do with scope, he was trying to access the non-static x and y variables from a static context.  Probably referring to the SpaceShip.x and SpaceShip.y.  Unless he named the SpaceShip object variable SpaceShip, which is just bad form.

Scottie, it would be helpful if you would just post all of your code.  And just for clarification, what does Body have that you are extending?  Bounding box, helper methods, etc?

Also, is SpaceShip the name of the variable in that code?  If so, you should use camel case.



Around the Network
JaggedSac said:

Also, don't use Double unless it is necessary, instead use double. double is a primitive, and Double is a wrapper object for a double primitive.

And vlad321, it has everything to do with scope, he was trying to access the non-static x and y variables from a static context.

Scottie, it would be helpful if you would just post all of your code.  And just for clarification, what does Body have that you are extending?  Bounding box, helper methods, etc?

 

Static is not scope. It has nothign to do with restrictions from where a variable is visible.



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

 

vlad321 said:
JaggedSac said:

Also, don't use Double unless it is necessary, instead use double. double is a primitive, and Double is a wrapper object for a double primitive.

And vlad321, it has everything to do with scope, he was trying to access the non-static x and y variables from a static context.

Scottie, it would be helpful if you would just post all of your code.  And just for clarification, what does Body have that you are extending?  Bounding box, helper methods, etc?

 

Static is not scope. It has nothign to do with restrictions from where a variable is visible.

Static variables are bound to a class and not an object instance.  In this compilation error, it is most likely referring to the SpaceShip.x and SpaceShip.y.  SpaceShip is the name of the class and it is expecting there to be x and y static variables in the class.  But without more code to look at, it is hard to tell.  Here is most likely what he did.

 

public class Bullet extends Body{

public Double x;

public Double y;

public Double xVelocity = x - SpaceShip.x;

public Double yVelocity = y - SpaceShip.y;

...

...

In which case SpaceShip does not have static variables with this name.

Java does not have a global scope but static variables are as close as it gets.  There is only one instance of each static variable per ClassLoader(A JVM can have more than one class loader).  But you are correct in that static does not define visibility.

 



Alright, Ill post all (or at least the relevant parts) of my code

the SpaceShip class


import java.util.Random;
import javax.swing.ImageIcon;

public class SpaceShip extends Body {

static final ImageIcon ship = new ImageIcon("ship.png");

static final Integer width = ship.getIconWidth();

static final Integer height = ship.getIconHeight();

public SpaceShip(Double xp, Double yp) {
x = xp;
y = yp;

}

public Integer clip = 7;

public Integer ammo = 70;

public void draw(Game h, GameComponent canvas) {

canvas.drawImage(ship.getImage(), mapx(h, canvas, x),
mapy(h, canvas, y));

}

public void step(Game bw, GameComponent canvas) {
if (canvas.leftPressed) {
x += -2;
if (x x = 0.0;
}
if (canvas.rightPressed) {
x += 2;
if (x > canvas.xdim - width)
x = 1.0 * canvas.xdim - width;
}
if (canvas.upPressed) {
y += -2;
if (y y = 0.0;
}
if (canvas.downPressed) {
y += 2;
if (y > canvas.ydim - height)
y = 1.0 * canvas.ydim - height;

}

if (canvas.rPressed) {
canvas.rPressed = false;
ammo = (ammo -7 + clip);
clip += 7;
System.out.println(ammo);
}


if (canvas.spacePressed) {
canvas.spacePressed = false;
if (clip >= 1) {
bw.missiles.add(new Missile(x + width / 2 - Missile.width / 2, y));
clip += -1;
System.out.println(clip);
}
}
}

public Double width() {
return (double) width;
}

public Double height() {
return (double) height;
}
}



Bluuet Class

import java.util.Random;
import javax.swing.ImageIcon;
public class Bullet extends Body {

static final ImageIcon bullet = new ImageIcon("bullet.png");

static final Integer width = bullet.getIconWidth();

static final Integer height = bullet.getIconHeight();

// public Double yVelocity = y-SpaceShip.y;
// public Double xVelocity = x-SpaceShip.x;
// public Double yVelocity = SpaceShip.top();
public double yvelocity = Game.ship.getY();

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

public Bullet(Double xp, Double yp) {
x = xp;
y = yp;
}

public void draw(Game h, GameComponent canvas) {
canvas.drawImage(bullet.getImage(), mapx(h, canvas, x), mapy(h, canvas,
y));
}

public Boolean offscreen() {
return y > SpaceInvaders.ycanvas;
}

public void step(Game bw, GameComponent canvas) { // All they do is drop.
y += yVelocity;
x += xVelocity;
}

public Double width() {
return (double) width;
}

public Double height() {
return (double) height;
}
}



Around the Network

Body Class

public abstract class Body {

public Double x, y;

public Double distance(Body b) { // This calculates the distance between
// centers.
Double xd = (b.x + b.width() / 2) - (x + width() / 2);
Double yd = (b.y + b.height() / 2) - (y + height() / 2);
return Math.sqrt(xd * xd + yd * yd);
}

public Boolean collide(Body b) { // This determines if two bodies have
// collided.
return pointIn(b.x, b.y) || pointIn(b.x + b.width(), b.y)
|| pointIn(b.x, b.y + b.height())
|| pointIn(b.x + b.width(), b.y + b.height())
|| b.pointIn(x, y);
}

public Boolean pointIn(Double xp, Double yp) { // This determines if a
// point is within this
// body.
return x }

public static int mapx(Game h, GameComponent canvas, double x) {
return (int) Math.round(x);
}

public static int mapy(Game h, GameComponent canvas, double y) {
return (int) Math.round;
}

public abstract void draw(Game h, GameComponent canvas);

public abstract void step(Game bw, GameComponent canvas);

public abstract Double width();

public abstract Double height();

}



Game Class (this is the last I'll upload, the rest deal with things like receiving keyboard input, displaying the bullets and other things that shouldn't be relevant)

import java.util.Iterator;
import java.util.Random;
public class Game {

static final Integer numberAliensCol = 10;

static final Integer numberAliensRows = 3;

static final Double alienGapCol = (SpaceInvaders.xcanvas - 2.0 * Alien.width)
/ numberAliensCol;

static final Double alienGapRow = (SpaceInvaders.ycanvas - 3.0 * Alien.height)
/ numberAliensRows;

static final Double shipStartx = SpaceInvaders.xcanvas / 2.0;

static final Double shipStarty = 1.0 * SpaceInvaders.ycanvas
- SpaceShip.height;

static Random random = new Random();

Double xsize, ysize;

SpaceShip ship;

Bodies missiles, aliens, bullets;

Boolean movingLeft;

Boolean lost;

public Game(Double xs, Double ys) {

lost = false;
xsize = xs;
ysize = ys;
ship = new SpaceShip(shipStartx, shipStarty);

aliens = new Bodies();
missiles = new Bodies();
bullets = new Bodies();
for (Integer i = 0; i for (Integer j = 0; j aliens.add(new Alien((i + 1) * alienGapCol, j * alienGapRow));
}
}
movingLeft = true;
}

public void draw(GameComponent canvas) {
// Drawing the current games state involves drawing all of
// the components that make up the game.
for (Body b : aliens) {
b.draw(this, canvas);
}
for (Body b : missiles) {
b.draw(this, canvas);
}
for (Body b : bullets) {
b.draw(this, canvas);
}
ship.draw(this, canvas);
}

public Boolean noAliens() {
return aliens.size() == 0;
}

public void step(GameComponent canvas) {

// Each of the components are stepped forward one step in time.
for (Body b : aliens) {
b.step(this, canvas);
}
for (Body b : missiles) {
b.step(this, canvas);
}
for (Body b : bullets) {
b.step(this, canvas);
}
ship.step(this, canvas);

// When a missile hits an alien both are removed.
Iterator alienIterator = aliens.iterator();
while (alienIterator.hasNext()) {
Alien a = (Alien) alienIterator.next();
Iterator missileIterator = missiles.iterator();
Boolean removed = false;
while (missileIterator.hasNext() && !removed) {
Missile m = (Missile) missileIterator.next();
if (m.collide(a)) {
missileIterator.remove();
alienIterator.remove();
removed = true;
}
}
}

// When a bullet hits the ship the games is lost. Also bullets that drop off the screen
// are removed from the simulation.
Iterator bulletsIterator = bullets.iterator();
while (bulletsIterator.hasNext()) {
Bullet b = (Bullet) bulletsIterator.next();
if (b.collide(ship)) {
lost = true;
}
if (b.offscreen()) {
bulletsIterator.remove();
}
}


// Missile that go off the screen are also removed.
Iterator missileIterator = missiles.iterator();
Boolean removed = false;
while (missileIterator.hasNext() && !removed) {
Missile m = (Missile) missileIterator.next();
if (m.offscreen())
missileIterator.remove();
}



}
}



scottie said:
Bluuet Class

import java.util.Random;
import javax.swing.ImageIcon;
public class Bullet extends Body {

static final ImageIcon bullet = new ImageIcon("bullet.png");

static final Integer width = bullet.getIconWidth();

static final Integer height = bullet.getIconHeight();

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

// public Double yVelocity = SpaceShip.top();
public double yvelocity = Game.ship.getY();

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

public Bullet(Double xp, Double yp) {
x = xp;
y = yp;
}

public void draw(Game h, GameComponent canvas) {
canvas.drawImage(bullet.getImage(), mapx(h, canvas, x), mapy(h, canvas,
y));
}

public Boolean offscreen() {
return y > SpaceInvaders.ycanvas;
}

public void step(Game bw, GameComponent canvas) { // All they do is drop.
y += yVelocity;
x += xVelocity;
}

public Double width() {
return (double) width;
}

public Double height() {
return (double) height;
}

}

 

That is exactly what I thought you did.  You are gonna want to either pass the x and y coordinates or the SpaceShip object through a method.  Maybe the constructor of the Bullet class.

public Bullet(Double xp, Double yp, SpaceShip ss) {
x = xp;
y = yp;

yVelocity = y-ss.y;
xVelocity = x-ss.x;

}

 

Also, why are you using Double instead of double?



Double changed to double now, thanks

Alright, so I added a ship to the bullet constructor and defined the x and y velocities as you said

Then I went to the Alien class, where bullets are constructed and added a method to determine the closest SpaceShip (which as there's only 1 I have just told to return the SpaceShip from game called ship

SpaceShip closestShip() {
return Game.ship;
}

which gave me the same 'non-static variable ship cannot be referenced from a static context' error

I also tried

SpaceShip closestShip = Game.ship;

But received the same error messege

and altered the bullet generating line to read

bw.bullets.add(new Bullet(x, y,closestShip));
But it can't find closestShip because the above didn't work



Since there is only ever going to be 1 ship in the game, you can make that variable static. This is normally not a good approach but it seems alright in this case.


in Game -

public static SpaceShip ship;