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

2017/3/7

Creating an Ammo Counter

ThanksGreenfoot ThanksGreenfoot

2017/3/7

#
Hi, I'm making a tank game where each tank only has 20 missiles to shoot. I have gotten the tanks to stop shooting after 20 shots, but I would like a counter to display this. I guess I'll paste all the code so you can review it. Tank: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Player2 here. * * @author (your name) * @version (a version number or a date) */ public class Blue extends DoubleMove { public Blue() { GreenfootImage image = getImage(); image.scale(image.getWidth() - 65, image.getHeight() - 50); setImage(image); ammo=20; } public Blue(AmmoBlue BlueShots) { } private int ammo; public void act() { //Controls if (Greenfoot.isKeyDown("left")) turn(-2); if (Greenfoot.isKeyDown("right")) turn(2); if (Greenfoot.isKeyDown("up")) move(2.5); if (Greenfoot.isKeyDown("down")) move(-1.5); if (ammo > 0 && (Greenfoot.isKeyDown("enter") && System.currentTimeMillis()>FireDelay+250)) { BlueMissile bluemissile = new BlueMissile(); getWorld().addObject(bluemissile, getX(), getY()); bluemissile.setRotation(getRotation()); ammo--; FireDelay = System.currentTimeMillis(); } // Slow on Touching the Sponge if(isTouching(JellyWall.class)) move(-1); } private double FireDelay; } Counter: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; public class AmmoBlue extends Actor { private static final Color transparent = new Color(0,0,0,0); private GreenfootImage background; private int value; private int target; /** * Create a new counter, initialised to 0. */ public AmmoBlue() { background = getImage(); // get image from class value = 0; target = 0; updateImage(); } /** * Animate the display to count up (or down) to the current target value. */ public void act() { { } { if (value < target) { value++; updateImage(); } else if (value > target) { value--; updateImage(); } } } private double ValueDelay; /** * Add a new score to the current counter value. */ public void add(int score) { target += score; } /** * Return the current counter value. */ public int getValue() { return value; } /** * Set a new counter value. */ public void setValue(int newValue) { target = newValue; value = newValue; updateImage(); } /** * Update the image on screen to show the current value. */ private void updateImage() { GreenfootImage image = new GreenfootImage(background); GreenfootImage text = new GreenfootImage("" + value, 22, Color.BLACK, transparent); image.drawImage(text, (image.getWidth()-text.getWidth())/2, (image.getHeight()-text.getHeight())/2); setImage(image); } } World: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class MyWorld here. * * @author (your name) * @version (a version number or a date) */ public class Level1 extends World { public Level1() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(1000, 500, 1); prepare(); } public void act() { setPaintOrder(Blue.class, BlueMissile.class); } private void prepare() { Clock clock = new Clock(); Red player = new Red(); addObject(player,178,74); JellyWall jellywall = new JellyWall(); addObject(jellywall, 300,71); JellyWall jellywall2 = new JellyWall(); addObject(jellywall2,300,384); JellyWall jellywall4 = new JellyWall(); addObject(jellywall4,700,71); JellyWall jellywall3 = new JellyWall(); addObject(jellywall3,700,378); ScoreRed scorered = new ScoreRed(); addObject(scorered,199,19); ScoreBlue scoreblue = new ScoreBlue(); addObject(scoreblue,917,18); Clock timer = new Clock(); addObject(timer,491,18); AmmoBlue ammoblue = new AmmoBlue(); addObject(new AmmoBlue(),809,18); AmmoRed ammored = new AmmoRed(); addObject(ammored,96,20); Blue blue = new Blue(); addObject(blue,803,390); } } So I just need a counter that displays the current ammo for Blue, and I should be able to use this for Red later as well. I've looked at the tutorial and Michael did something in the world prepare such as this: Blue blue = new Blue(AmmoBlue); addObject(blue,803,390); and then in Blue: public Blue(AmmoBlue BlueShots) { } but this says in the world prepare method 'cannot find variable AmmoBlue' Please help!
danpost danpost

2017/3/7

#
You need to pass an AmmoBlue object to the new Blue object:
Blue blue = new Blue(new AmmoBlue());
Then you will need to put something in the constructor in the Blue class that except the AmmoBlue object (currently there is no code in that constructor; but the AmmoBlue object will need to be saved -- which means you need a field for it; the image will need to be scaled and the ammo count will need set, as well).
ThanksGreenfoot ThanksGreenfoot

2017/3/8

#
Thanks, that got me headed in the right direction. The Blue actor receives AmmoBlue now, but I don't know how to resize it again. As well, when I do ammoblue.minus(1); it makes the number count down really fast, even when not pressing any buttons. Here is the code: public Blue() { GreenfootImage image = getImage(); image.scale(image.getWidth() - 65, image.getHeight() - 50); setImage(image); ammo=20; } public Blue(AmmoBlue BlueShots) { ammoblue = BlueShots; } if (ammo > 0 && (Greenfoot.isKeyDown("enter") && System.currentTimeMillis()>FireDelay+250)) { BlueMissile bluemissile = new BlueMissile(); getWorld().addObject(bluemissile, getX(), getY()); bluemissile.setRotation(getRotation()); ammo--; ammoblue.minus(1); FireDelay = System.currentTimeMillis(); } In BlueAmmo: public void minus(int score) value=20; ... { target -= score; }
ThanksGreenfoot ThanksGreenfoot

2017/3/8

#
I'll be honest, if you can supply code that will simply just allow me to count the current ammo down from 20 every time my tank fires via a counter, it will suffice. I don't understand how this method works. Now my tank doesn't shoot at all.
Super_Hippo Super_Hippo

2017/3/8

#
You have to add the code starting with 'if (ammo ...' into the act method. You should also only have one field holding the ammo amount and not two for the same value.
ThanksGreenfoot ThanksGreenfoot

2017/3/9

#
How would I do this? If I do 'if (ammo=...); then it says cannot convert boolean to int.
Super_Hippo Super_Hippo

2017/3/9

#
Show the whole code again (and use code tags please). Did you create an act method?
You need to login to post a reply.