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

2025/5/6

A error on making a "counter"

Punithan35 Punithan35

2025/5/6

#
So i m making a game for my school final project. so when i was doing the counter ther is a error ,on my latar and laser. on the latar the error is"Undlared variable: Counter and the laser error says"Undeclaren method addOject(counter) so i will send my latarclass,laserclass and my other class Counterclass. import greenfoot.*; public class Latar extends World { private int jada=0; public Latar() { super(600, 400, 1,false); addObject(new rocket(),100,200); } public Counter getCounter() { return Counter; } public void act() { addObject(Counter, 100, 40); if(jada>0)jada--; else jada=20; if(jada==1){ int py=Greenfoot.getRandomNumber(getHeight()); addObject(new enemy1(-(2+Greenfoot.getRandomNumber(3))),getWidth()+200,py); } } } the laser class import greenfoot.*; import java.awt.Color.*; public class laser extends Actor { private boolean toRemove=false; private int vx=3; Counter counter = new Counter(); public void addedToWorld(World Latar) { GreenfootImage image=new GreenfootImage(50,10); image.setColor(Color.GREEN); image.drawLine(0,5,image.getWidth()-1,5); setImage(image); } public Counter getCunter() { return counter; } public void act() { Greenfoot.addObject(counter); if(!toRemove){ setLocation(getX()+vx,getY()); Actor actor=getOneIntersectingObject(enemy1.class); if(actor!=null){ ((enemy1)actor).Hancur(); } if(getX()>getWorld().getWidth()+32)toRemove=true; }else{ getWorld().removeObject(this); } } } theCounterclass import java.awt.Color.*; /** * 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.GREEN, Color.BLACK)); } public void addScore() { score++; } } so if any one to help me on my last project your free to send the right coade but i need fast i have only 4 days to send my project. TQ FOR READING THIS ALL :)
danpost danpost

4 days ago

#
Punithan35 wrote...
when i was doing the counter ther is a error ,on my latar and laser. on the latar the error is"Undlared variable: Counter and the laser error says"Undeclaren method addOject(counter) so i will send my latarclass,laserclass and my other class Counterclass. << Code Omitted >>
First off, the Counter instance should be created, maintained and controlled by the World subclass Latar, not the laser class. Any laser instance can get its World object and call a public method in it to adjust the value of that Counter instance. Note that where you have it in the laser class, each laser instance will create its own Counter instance which is lost when the laser instance is removed from the world. Also, do not import java.awt.Color. Greenfoot has its own Color class you can use directly (and uploaded scenario using it will not run on the site (just because its java.awt, for security reasons).
Punithan35 Punithan35

yesterday

#
danpost wrote...
Punithan35 wrote...
when i was doing the counter ther is a error ,on my latar and laser. on the latar the error is"Undlared variable: Counter and the laser error says"Undeclaren method addOject(counter) so i will send my latarclass,laserclass and my other class Counterclass. << Code Omitted >>
First off, the Counter instance should be created, maintained and controlled by the World subclass Latar, not the laser class. Any laser instance can get its World object and call a public method in it to adjust the value of that Counter instance. Note that where you have it in the laser class, each laser instance will create its own Counter instance which is lost when the laser instance is removed from the world. Also, do not import java.awt.Color. Greenfoot has its own Color class you can use directly (and uploaded scenario using it will not run on the site (just because its java.awt, for security reasons).
it works
You need to login to post a reply.