Hi there,
I spent two months developing my game as an AS assignment. The game is fully executable, but now the counter will not count the points when the ship gets the objects.
Will anyone be able to help!
Thanks
Printable View
Hi there,
I spent two months developing my game as an AS assignment. The game is fully executable, but now the counter will not count the points when the ship gets the objects.
Will anyone be able to help!
Thanks
Can you post the code here in the forum so all can see ?
Code java:public void Ship extends Actor { private int score = 0; private Counter counter; public Ship(Counter pointCounter) { counter = pointCounter; } /** * Act - do whatever the Ship wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(10); if (Greenfoot.isKeyDown("up")) { move(10); } if (Greenfoot.isKeyDown("down")) { move(-10); } if (Greenfoot.isKeyDown("right")) { turn(10); } if (Greenfoot.isKeyDown("left")) { turn(-10); } if (Greenfoot.isKeyDown("q")) { turn(180); } Actor binary1 = getOneIntersectingObject(Binary1.class); if (binary1 !=null) { ShipGameWorld shipWorld = (ShipGameWorld)getWorld(); int xCoord = Greenfoot.getRandomNumber(600); int yCoord = Greenfoot.getRandomNumber(400); binary1.setLocation(xCoord, yCoord); shipWorld.increaseScore(); Greenfoot.playSound("kaboom.wav"); } Actor binary0 = getOneIntersectingObject(Binary0.class); if (binary0 !=null) { ShipGameWorld shipWorld = (ShipGameWorld)getWorld(); int xCoord = Greenfoot.getRandomNumber(600); int yCoord = Greenfoot.getRandomNumber(400); binary0.setLocation(xCoord, yCoord); shipWorld.increaseScore(); Greenfoot.playSound("kaboom.wav"); } Actor submarine = getOneIntersectingObject(Submarine.class); if (submarine != null) { // we have hit a sub so we get access to the // world using the getWorld() method and then // use the removeObject method to remove the // ship from the game. World shipWorld = getWorld(); shipWorld.removeObject(this); Greenfoot.playSound("kaboom.wav"); } } }
Code java:public class Binary0 extends Actor { private int score = 0; private Counter counter; public Binary0(Counter pointCounter) { counter = pointCounter; } public void act() { binary0(); } public void binary0() { Actor binary0 = getOneIntersectingObject(Binary0.class); if (binary0 !=null) { ShipGameWorld shipWorld = (ShipGameWorld)getWorld(); int xCoord = Greenfoot.getRandomNumber(600); int yCoord = Greenfoot.getRandomNumber(400); binary0.setLocation(xCoord, yCoord); shipWorld.increaseScore(); Greenfoot.playSound("kaboom.wav"); } } }
Code java:public class Binary1 extends Actor { public int score = 0; /** * Act - do whatever the Binary1 wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { Actor ship = getOneIntersectingObject(Ship.class); if (ship !=null) { ShipGameWorld shipWorld = (ShipGameWorld)getWorld(); int xCoord = Greenfoot.getRandomNumber(600); int yCoord = Greenfoot.getRandomNumber(400); ship.setLocation(xCoord, yCoord); shipWorld.increaseScore(); Greenfoot.playSound("kaboom.wav"); } Actor submarine = getOneIntersectingObject(Submarine.class); if (submarine !=null) { ShipGameWorld shipWorld = (ShipGameWorld)getWorld(); int xCoord = Greenfoot.getRandomNumber(600); int yCoord = Greenfoot.getRandomNumber(400); submarine.setLocation(xCoord, yCoord); shipWorld.decreaseScore(); Greenfoot.playSound("kaboom.wav"); } } }
Code java:public class Counter extends Actor { private int score = 0; private Counter counter; public void Score() { setImage(new GreenfootImage(200, 30)); subtract(); add(); update(); } public void add() { score++; update(); } public void subtract() { score--; update(); } public void update() { GreenfootImage img = getImage(); img.clear(); img.setColor(Color.WHITE); img.drawString("Score: " + score, 4, 20); } }
Code java:public class Submarine extends Actor { private int score = 0; /** * Act - do whatever the Sub wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(10); binary0(); binary1(); int xCoord = getX(); if(xCoord <=0 || xCoord >=599) { int random = Greenfoot.getRandomNumber(41); turn(35 + random); } int yCoord = getY(); if(yCoord <=0 || yCoord >=399) { int random = Greenfoot.getRandomNumber(41); turn(35 + random); } } public void binary0() { Actor binary0 = getOneIntersectingObject(Binary0.class); if (binary0 !=null) { ShipGameWorld shipWorld = (ShipGameWorld)getWorld(); int xCoord = Greenfoot.getRandomNumber(600); int yCoord = Greenfoot.getRandomNumber(400); binary0.setLocation(xCoord, yCoord); shipWorld.decreaseScore(); Greenfoot.playSound("kaboom.wav"); } }
Code java:public void binary1() { Actor binary1 = getOneIntersectingObject(Binary1.class); if (binary1 !=null) { ShipGameWorld shipWorld = (ShipGameWorld)getWorld(); int xCoord = Greenfoot.getRandomNumber(600); int yCoord = Greenfoot.getRandomNumber(400); binary1.setLocation(xCoord, yCoord); shipWorld.decreaseScore(); Greenfoot.playSound("kaboom.wav"); } } }
Code java:public class ShipGameWorld extends World { private int score = 0; private Counter counter; public ShipGameWorld() { super(600, 400, 1); prepare(); } public void increaseScore() { score = score + 50; } public void decreaseScore() { score = score - 1; } public int getScore() { return score; } /** * Prepare the world for the start of the program. That is: create the initial * objects and add them to the world. */ private void prepare() { Ship ship = new Ship(counter); addObject(ship, 283, 264); ship.setLocation(228, 165); ship.setLocation(326, 175); Binary0 binary0 = new Binary0(counter); addObject(binary0, 99, 274); Binary0 binary02 = new Binary0(counter); addObject(binary02, 464, 303); Binary1 binary1 = new Binary1(); addObject(binary1, 368, 286); Binary0 binary03 = new Binary0(counter); addObject(binary03, 481, 83); Binary1 binary12 = new Binary1(); addObject(binary12, 182, 79); Binary0 binary04 = new Binary0(counter); addObject(binary04, 194, 335); Submarine submarine = new Submarine(); addObject(submarine, 199, 282); Counter counter = new Counter(); addObject(counter, 41, 373); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); Counter counter2 = new Counter(); addObject(counter2, 25, 383); removeObject(counter2); increaseScore(); decreaseScore(); getScore(); Counter counter3 = new Counter(); addObject(counter3, 515, 364); counter3.setLocation(114, 375); Counter counter4 = new Counter(); addObject(counter4, 66, 338); counter4.Score(); counter4.setLocation(120, 334); removeObject(counter); removeObject(counter3); counter4.setLocation(112, 376); counter4.setLocation(114, 371); getScore(); decreaseScore(); } }
Sorry, I forgot to ask you to use code tags:
Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
One thing I see missing if anyone wanted to copy, compile, execute and test the code, is there are no import statements.
I wonder what happened with your code tags. I expected to see this:
Code java:<YOUR CODE HERE>
You must have used invisible ink.Quote:
I did put up the top import
[QUOTE=Norm;62889]I wonder what happened with your code tags. I expected to see this:
Code java:<import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class ShipGameWorld here. * * @author (your name) * @version (a version number or a date) */ public class ShipGameWorld extends World { private int score = 0; private Counter counter; public ShipGameWorld() { super(600, 400, 1); prepare(); } public void increaseScore() { score = score + 50; } public void decreaseScore() { score = score - 1; } public int getScore() { return score; } /** * Prepare the world for the start of the program. That is: create the initial * objects and add them to the world. */ private void prepare() { Ship ship = new Ship(counter); addObject(ship, 283, 264); ship.setLocation(228, 165); ship.setLocation(326, 175); Binary0 binary0 = new Binary0(counter); addObject(binary0, 99, 274); Binary0 binary02 = new Binary0(counter); addObject(binary02, 464, 303); Binary1 binary1 = new Binary1(); addObject(binary1, 368, 286); Binary0 binary03 = new Binary0(counter); addObject(binary03, 481, 83); Binary1 binary12 = new Binary1(); addObject(binary12, 182, 79); Binary0 binary04 = new Binary0(counter); addObject(binary04, 194, 335); Submarine submarine = new Submarine(); addObject(submarine, 199, 282); Counter counter = new Counter(); addObject(counter, 41, 373); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); counter.setLocation(31, 376); Counter counter2 = new Counter(); addObject(counter2, 25, 383); removeObject(counter2); increaseScore(); decreaseScore(); getScore(); Counter counter3 = new Counter(); addObject(counter3, 515, 364); counter3.setLocation(114, 375); Counter counter4 = new Counter(); addObject(counter4, 66, 338); counter4.Score(); counter4.setLocation(120, 334); removeObject(counter); removeObject(counter3); counter4.setLocation(112, 376); counter4.setLocation(114, 371); getScore(); decreaseScore(); } } import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Ship here. * * @author (your name) * @version (a version number or a date) */ public class Ship extends Actor { private int score = 0; private Counter counter; public Ship(Counter pointCounter) { counter = pointCounter; } /** * Act - do whatever the Ship wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(10); if (Greenfoot.isKeyDown("up")) { move(10); } if (Greenfoot.isKeyDown("down")) { move(-10); } if (Greenfoot.isKeyDown("right")) { turn(10); } if (Greenfoot.isKeyDown("left")) { turn(-10); } if (Greenfoot.isKeyDown("q")) { turn(180); } Actor binary1 = getOneIntersectingObject(Binary1.class); if (binary1 !=null) { ShipGameWorld shipWorld = (ShipGameWorld)getWorld(); int xCoord = Greenfoot.getRandomNumber(600); int yCoord = Greenfoot.getRandomNumber(400); binary1.setLocation(xCoord, yCoord); shipWorld.increaseScore(); Greenfoot.playSound("kaboom.wav"); } Actor binary0 = getOneIntersectingObject(Binary0.class); if (binary0 !=null) { ShipGameWorld shipWorld = (ShipGameWorld)getWorld(); int xCoord = Greenfoot.getRandomNumber(600); int yCoord = Greenfoot.getRandomNumber(400); binary0.setLocation(xCoord, yCoord); shipWorld.increaseScore(); Greenfoot.playSound("kaboom.wav"); } Actor submarine = getOneIntersectingObject(Submarine.class); if (submarine != null) { // we have hit a sub so we get access to the // world using the getWorld() method and then // use the removeObject method to remove the // ship from the game. World shipWorld = getWorld(); shipWorld.removeObject(this); Greenfoot.playSound("kaboom.wav"); } } } import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Binary0 here. * * @author (your name) * @version (a version number or a date) */ public class Binary0 extends Actor { private int score = 0; private Counter counter; public Binary0(Counter pointCounter) { counter = pointCounter; } public void act() { binary0(); } public void binary0() { Actor binary0 = getOneIntersectingObject(Binary0.class); if (binary0 !=null) { ShipGameWorld shipWorld = (ShipGameWorld)getWorld(); int xCoord = Greenfoot.getRandomNumber(600); int yCoord = Greenfoot.getRandomNumber(400); binary0.setLocation(xCoord, yCoord); shipWorld.increaseScore(); Greenfoot.playSound("kaboom.wav"); } } } import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Submarine here. * * @author (your name) * @version (a version number or a date) */ public class Submarine extends Actor { private int score = 0; /** * Act - do whatever the Sub wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(10); binary0(); binary1(); int xCoord = getX(); if(xCoord <=0 || xCoord >=599) { int random = Greenfoot.getRandomNumber(41); turn(35 + random); } int yCoord = getY(); if(yCoord <=0 || yCoord >=399) { int random = Greenfoot.getRandomNumber(41); turn(35 + random); } } public void binary0() { Actor binary0 = getOneIntersectingObject(Binary0.class); if (binary0 !=null) { ShipGameWorld shipWorld = (ShipGameWorld)getWorld(); int xCoord = Greenfoot.getRandomNumber(600); int yCoord = Greenfoot.getRandomNumber(400); binary0.setLocation(xCoord, yCoord); shipWorld.decreaseScore(); Greenfoot.playSound("kaboom.wav"); } } public void binary1() { Actor binary1 = getOneIntersectingObject(Binary1.class); if (binary1 !=null) { ShipGameWorld shipWorld = (ShipGameWorld)getWorld(); int xCoord = Greenfoot.getRandomNumber(600); int yCoord = Greenfoot.getRandomNumber(400); binary1.setLocation(xCoord, yCoord); shipWorld.decreaseScore(); Greenfoot.playSound("kaboom.wav"); } } } import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; /** * This counter class adds or subtracts the value of points * from the current points value. * @author (Ryan Fagan) * @version (1.0) */ public class Counter extends Actor { private int score = 0; private Counter counter; public void Score() { setImage(new GreenfootImage(200, 30)); subtract(); add(); update(); } public void add() { score++; update(); } public void subtract() { score--; update(); } public void update() { GreenfootImage img = getImage(); img.clear(); img.setColor(Color.WHITE); img.drawString("Score: " + score, 4, 20); } } import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Binary1 here. * * @author (your name) * @version (a version number or a date) */ public class Binary1 extends Actor { public int score = 0; /** * Act - do whatever the Binary1 wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { Actor ship = getOneIntersectingObject(Ship.class); if (ship !=null) { ShipGameWorld shipWorld = (ShipGameWorld)getWorld(); int xCoord = Greenfoot.getRandomNumber(600); int yCoord = Greenfoot.getRandomNumber(400); ship.setLocation(xCoord, yCoord); shipWorld.increaseScore(); Greenfoot.playSound("kaboom.wav"); } Actor submarine = getOneIntersectingObject(Submarine.class); if (submarine !=null) { ShipGameWorld shipWorld = (ShipGameWorld)getWorld(); int xCoord = Greenfoot.getRandomNumber(600); int yCoord = Greenfoot.getRandomNumber(400); submarine.setLocation(xCoord, yCoord); shipWorld.decreaseScore(); Greenfoot.playSound("kaboom.wav"); } } }>