This site requires JavaScript, please enable it in your browser!
Greenfoot back
Dillybar
Dillybar wrote ...

2013/10/16

Help with a command please.

Dillybar Dillybar

2013/10/16

#
Hi, I'm having problems with making my scoreboard update every time someone misses in my game. All help is appreciated :). PS... the goal of the game is to shoot cannonballs over walls to hit the target. Here is my cannonball code and scoreboard code. Again, thanks :). Cannonball public class CannonBall extends Actor { protected int angle=0; protected int power=100; protected int misses=0; protected StatusBoard statusBoard; protected int xSpeed= 0; protected int ySpeed= 0; public static final int GRAVITY=2; public CannonBall (int angle, int power, StatusBoard board) { power=(power*10)/25; xSpeed=(int)(power*Math.cos(Math.toRadians(angle))); ySpeed=(int)(power*Math.sin(Math.toRadians(angle))); } /** * Act - do whatever the CannonBall wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // Add your action code here. setLocation(getX()+xSpeed,getY()-ySpeed); ySpeed=ySpeed-GRAVITY; World cannonWorld = getWorld(); statusBoard.setValues (angle, power, misses); if ( getY() > cannonWorld.getHeight() ) { cannonWorld.removeObject(this); misses=misses+1; } else { checkCollision(); } } public void checkCollision() { Target t = (Target) getOneIntersectingObject(Target.class); if(t != null) { getWorld().removeObject(t); getWorld().removeObject(this); Greenfoot.playSound("Ding.mp3"); } } } StatusBoard public class StatusBoard extends Actor { private static final String angleLabel = "angle: "; private static final String powerLabel = "power: "; private static final String missesLabel = "misses: "; protected GreenfootImage background = null; private GreenfootImage box; public StatusBoard () { background = new GreenfootImage (100, 180); background.setColor(new java.awt.Color(255, 255, 255, 200)); background.fillRect (0, 0, 150, 60); setImage(background); } public void setValues(int angle, int power, int misses) { GreenfootImage img = new GreenfootImage (background); img.setColor (java.awt.Color.BLUE); img.drawString("Angle: " + angle, 10, 20); img.drawString("Power: " + power, 10, 35); img.drawString("Misses: " + misses, 10, 50); setImage(img); } } Thank you very much. All help is appreciated
Kartoffelbrot Kartoffelbrot

2013/10/16

#
1.) In your act method you first update the statusboard and afterwards you increase the misses. 2.) You forgot to set the value of statusboard in the constructor in cannonball. 3.) You reset the value of the scoreboard everytime you call setValues because ypu dont add the misses. So at the moment it can be only 0. If I had a failure in my mind, say it please.
danpost danpost

2013/10/16

#
If you are creating a 'new CannonBall' object for each attempt, then the 'misses' field will zero out each time you create one. By making the 'misses' field 'static ('private static int misses = 0;'), all cannonballs created will use the same 'misses' field instead of each working with their own individual 'misses' field. The angle and power will always be 0 and 100 respectively. The angle argument in the constructor is not saved ('this.angle = angle;') and the power argument is never set to the instance field because both 'power's in 'power = (power*10)/25;' refer to the field that is local to the constructor method; instead, it needs to be 'this.power = (power*10)/25;'. The statusboard object also needs to be saved ('statusBoard = board;'), as per (2) of Kartoffelbrot's post. I do not think that (1) in that post is anything to worry about; although I would 'setValues' once in the constructor (after saving the values of the arguments) and once immediately after 'misses = misses+1;'. That way, it does not bog down the system by updating the board when no change has occurred.
Dillybar Dillybar

2013/10/17

#
Sorry, I still can't get it to work. Here is my cannonball code. To clarify, my angle and power update fine but as soon as I shoot a cannonball the game stops. Thanks for the help. Cannonball public class CannonBall extends Actor { protected int angle=0; protected int power=100; protected int misses; protected int xSpeed= 0; protected int ySpeed= 0; public static final int GRAVITY=2; public CannonBall (int angle, int power, StatusBoard board) { power=(power*10)/25; xSpeed=(int)(power*Math.cos(Math.toRadians(angle))); ySpeed=(int)(power*Math.sin(Math.toRadians(angle))); } /** * Act - do whatever the CannonBall wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // Add your action code here. setLocation(getX()+xSpeed,getY()-ySpeed); ySpeed=ySpeed-GRAVITY; World cannonWorld = getWorld(); statusBoard.setValues (angle, power, misses); if ( getY() > cannonWorld.getHeight() ) { cannonWorld.removeObject(this); misses=misses+1; } else { checkCollision(); } } public void checkCollision() { Target t = (Target) getOneIntersectingObject(Target.class); if(t != null) { getWorld().removeObject(t); getWorld().removeObject(this); Greenfoot.playSound("Ding.mp3"); } } Turret import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.List; /** * Write a description of class Turret here. * * @author (your name) * @version (a version number or a date) */ public class Turret extends Actor { protected int angle=0; protected int power=100; protected StatusBoard statusBoard; public Turret (StatusBoard board) { statusBoard = board; } /** * Act - do whatever the Turret wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { controlTurret(); setRotation(-angle); statusBoard.setValues (angle, power, misses); } public void controlTurret() { if(Greenfoot.isKeyDown("left")&&angle<90) { angle=angle+1; } if(Greenfoot.isKeyDown("right")&&angle>0) { angle=angle-1; } if(Greenfoot.isKeyDown("up")&&power<100) { power=power+1; } if(Greenfoot.isKeyDown("down")&&power>0) { power=power-1; } if(Greenfoot.isKeyDown("space")&&noCannonBalls()) { addCannonBall(); } } public Boolean noCannonBalls() { World cannonWorld = getWorld(); java.util.List balls = cannonWorld.getObjects(CannonBall.class); return balls.isEmpty(); } public void addCannonBall() { getWorld(). addObject(new CannonBall(angle, power, StatusBoard), getX(), getY()); Greenfoot.playSound("Cannon.wav"); } } The statusboard is the same as above. And again, thank you very much for all your help.
danpost danpost

2013/10/17

#
If you are not going to make the 'misses' field 'static', then you must retain and use the same cannonball object for all shots (so you can retain the value of the 'misses' field and not replace it with a new field initialized to zero each time you shoot a new cannonball). This could not be the code you used that ran until you shot a cannonball. This code should not even compile. You are using the 'misses' field in your turret act method, but do not have that field declared anywhere in the class. Once you make the 'misses' field 'static' in the cannonball class, you can change the call to set the statusboard to this:
statusBoard.setValues (angle, power, CannonBall.misses);
Dillybar Dillybar

2013/10/17

#
Thank you so much! I really appreciate all of your help. It works now :). thanks
You need to login to post a reply.