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

2026/5/27

issues with score and intersecting using "child classes"

bonyay bonyay

2026/5/27

#
I'M EXTREMELY NEW to coding, and I need help with a game for a school project. I'm sure my code is poorly written, sorry! I have this code in the "parent" target, and 3 "child" classes under target, being TargetMid, TargetFar, and TargetClose. The problem is that the code works for TargetClose, but not the other two. If it helps, I have code for unrelated things in TargetMid and Far, and nothing in TargetClose
public class Target extends Actor
{
    public int targetSpeed=-8;
    public static int scoreVar=0;
    /**
     * Act - do whatever the Target wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        if(MyWorld.isPaused == false)
        {
            setLocation(getX()+targetSpeed,getY());
            if(getX()<5)
            {
                getWorld().removeObject(this);
            }
        }
        pointup();
    }
    public void pointup()
    {
        Actor water = getOneIntersectingObject(Water.class);
        if (water != null)
        {
         getWorld().removeObject(water);
         getWorld().removeObject(this);
         scoreVar++;
        }
    }
}
danpost danpost

2026/5/27

#
bonyay wrote...
I have this code in the "parent" target, and 3 "child" classes under target, being TargetMid, TargetFar, and TargetClose. The problem is that the code works for TargetClose, but not the other two. If it helps, I have code for unrelated things in TargetMid and Far, and nothing in TargetClose << Code Omitted >>
If you have act methods in the TargetMid and Far classes, then the act method in the Target class will not run unless you add the following line to those act methods:
bonyay bonyay

2026/5/31

#
I eventually figured it out myself (obviously, first post had that upload time delay), but thank you for the reply anyways!
You need to login to post a reply.