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

2012/11/1

Can't get score to count

ryanr4788 ryanr4788

2012/11/1

#
So I've tried making the Bullet a subclass of Counter and vise versa. When I do what I think would work I get some message similiar to: non static method cannot be referenced from a static context. Pretty much when the bullet hits the asteroid and the checkCollision method called upon I need the counter to go up. Thanks in advance.....and yes the counter came precoded, as you can probably tell since the code actually looks clean =\. import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) import java.awt.Font; /** * Counter that displays a number. * * @author Michael Kolling * @version 1.0.1 */ public class Counter extends Actor { private int value = 0; private int target = 0; private String text; private int stringLength; public Counter() { this(""); } public Counter(String prefix) { text = prefix; stringLength = (text.length() + 2) * 16; setImage(new GreenfootImage(stringLength, 24)); GreenfootImage image = getImage(); Font font = image.getFont(); image.setFont(font.deriveFont(24.0F)); // use larger font updateImage(); } public void act() { if(value < target) { value++; updateImage(); } else if(value > target) { value--; updateImage(); } } public void add(int score) { target += score; } public void subtract(int score) { target -= score; } public int getValue() { return value; } /** * Make the image */ private void updateImage() { GreenfootImage image = getImage(); image.clear(); image.drawString(text + value, 1, 18); } } import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Bullet here. * * @author (your name) * @version (a version number or a date) */ public class Bullet extends Actor { /** * Act - do whatever the Bullet wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { setLocation(getX(), getY()-5); if (!checkHeight()) { checkCollision(); } } public void checkCollision() { Actor a = getOneIntersectingObject(Asteroid.class); Actor b = getOneIntersectingObject(Bullet.class); if (a != null) { World world = getWorld(); world.removeObject(a); world.removeObject(this); Greenfoot.playSound("explosion.wav"); }} private boolean checkHeight() { if(getY()<=37) { World world= getWorld(); world.removeObject(this); return true; } return false; } }
Gevater_Tod4711 Gevater_Tod4711

2012/11/1

#
In this code you get the compilererror that a non static method cannot be referenced from a static context? Thats a little strange because I don't see any static methods or variables here. But however: To Fix you problem you first need a static variable for in the counter class (just write private static int instead of private int). And then you need a method to count up this variable:
public static void setCounter(int counter) {//this method will count up the counter value for as much points as you write as parameter;
    this.counter += counter;
}
public static int getCounter() {//this method just alowes you to see the value of the counter in all other classes;
    return counter;
}
If you then want to count up the variable from the bullet class you just need to do that:
Counter.setCounter(1);//or as many points you want;
You need to login to post a reply.