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

2024/2/26

.get counter is not working for my shooter game

Pls_help Pls_help

2024/2/26

#
For some reason, it is saying .getCounter is undeclared I'm new and don't know how to fix this problem please help me Code for my laser import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Laser here. * * @author (your name) * @version (a version number or a date) */ public class Laser extends Actor { /** * this code checks if the laser is touching the alien */ public void checkHit(){ if(isTouching(Alien.class)){ getWorld().removeObject((getOneIntersectingObject(Alien.class))); } } public void addedToWorld(World world){ setRotation(getWorld().getObjects(Ship.class).get(0).checkRotation()); } public void act() /** * this code runs all of the laser methods */ { move(5); if(isAtEdge()){ getWorld().removeObject(this); World myWorld = getWorld(); Counter counter = myWorld.getCounter(); counter.addScore(); } else if(isTouching(Alien.class)){ getWorld().removeObject(getOneIntersectingObject(Alien.class)); ((MyWorld) getWorld()).newAlien(); } } public Laser () /** * this code resises the laser image */ { GreenfootImage myImage = getImage(); int myNewHeight = (int)myImage.getHeight()/10; int myNewWidth = (int)myImage.getWidth()/10; myImage.scale(myNewWidth, myNewHeight); } } Code for MyWorld 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 MyWorld extends World { SimpleTimer time = new SimpleTimer(); Timer con = new Timer(); Counter counter = new Counter(); /** * Prepare the world for the start of the program. * That is: create the initial objects and add them to the world. */ public MyWorld() { super(600, 400, 1); addObject(counter, 550, 30); addObject(con,40,30); Ship ship = new Ship(); addObject(ship,309,200); Alien alien = new Alien(); addObject(alien,562,42); Alien alien2 = new Alien(); addObject(alien2,556,208); Alien alien3 = new Alien(); addObject(alien3,553,370); Alien alien4 = new Alien(); addObject(alien4,18,370); Alien alien5 = new Alien(); addObject(alien5,24,194); Alien alien6 = new Alien(); addObject(alien6,27,27); removeObject(alien2); removeObject(alien5); time.mark(); } public void newAlien() /** * This code makes the ship spawn in the middle and the 6 aliens spawn around the edge */{ addObject(new Alien(),Greenfoot.getRandomNumber(300),Greenfoot.getRandomNumber(300)); } public void act() { con.setValue(time.millisElapsed() / 1000); } /** * Prepare the world for the start of the program. * That is: create the initial objects and add them to the world. */ private Counter getCounter() { return counter; } } Code for Counter import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Counter here. * * @author (your name) * @version (a version number or a date) */ public class Counter extends Actor { int score = 0; /** * Act - do whatever the Counter wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { setImage(new GreenfootImage("Score : " + score, 24, Color.WHITE, Color.BLACK)); } public void addScore() { score++; } } Thanks
danpost danpost

2024/2/26

#
Pls_help wrote...
For some reason, it is saying .getCounter is undeclared
You have the following in your Laser class:
public void act()
{
    move(5);
    if(isAtEdge()){
        getWorld().removeObject(this);
        World myWorld = getWorld();
        Counter counter = myWorld.getCounter();
        counter.addScore();
    } else if(isTouching(Alien.class)){
        getWorld().removeObject(getOneIntersectingObject(Alien.class));
        ((MyWorld) getWorld()).newAlien();
    }
}
The getCounter method is not in the World class or any class above it (namely, also not in the Object class). It is in your MyWorld class. As such, line 6 should be:
MyWorld myWorld = (MyWorld)getWorld();
With this line, the method should be found declared if the method was declared public, which it is not. So the following change: would be required:
private Counter getCounter()
to
public Counter getCounter();
However, there is another issue. Line 5 removes the Laser object from the world, so line 6 (regardless of casting) will result in a null value being assigned to myWorld. As a result, line 7 fails (cannot call a method on a null value). Here is revised code:
public void act()
{
    move(5);
    if (isTouching(Alien.class)) {
        removeTouching(Alien.class);
        ((MyWorld)getWorld()).newAlien();
    }
    if (isAtEdge()) {
        ((MyWorld)getWorld()).getCounter().addScore();
        getWorld().removeObject(this);
    }
}
Seems weird to add to score when laser reaches edge. I would think score would increase when killing an alien. Just saying.
Pls_help Pls_help

2024/2/26

#
I found out the first problem like 10 minutes after so I changed it to public. And i added the remove laser when it gets to the end of the world so my game dosent get any lag. I need it to add a score when alien gets destroyed
Pls_help Pls_help

2024/2/27

#
Never Mind my problem was solved Thanks
You need to login to post a reply.