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

2023/3/27

Demon class killing villagers that are being grabbed

1
2
Spock47 Spock47

2023/3/28

#
KCee wrote...
im not really sure what you mean by stacktrace but this is what it says:
What you posted is the stacktrace. Very good. For simplicity (since asking whether villager is grabbed is very common), please add the method to Villager:
    public boolean isGrabbed() {
        final VehicleWorld world = (VehicleWorld)getWorld();
        if (world == null) {
            return false;
        }
        return world.isGrabbed(this);
    }
KCee wrote...
java.lang.ClassCastException: class GoodWitch cannot be cast to class Villager (GoodWitch and Villager are in unnamed module of loader java.net.URLClassLoader @786b6cc9) at PoisonCloud.act(PoisonCloud.java:29) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183)
Ok, the class cast exception is the problem mentioned at the end of thread https://greenfoot.org/topics/65329/0#post_148583 So, you have to decide: Option A: If PoisonCloud can poison any Pedestrian (including Witches), change PoisonCloud lines 29 through 33 to
            if (touched instanceof Villager && ((Villager)touched).isGrabbed()) {
                continue;
            }
Option B: (code by danpost) If PoisonCloud can poison only villagers, change PoisonCloud lines 27 through 35 to:
for (Object obj : getIntersectingObjects(Villager.class)) {
    Villager villager = (Villager)obj;
    if ( ! villager.isGrabbed()) {
        getWorld().removeObject(villager);
        break;
    }
}
Spock47 Spock47

2023/3/28

#
KCee wrote...
After doing this when i try to call the method here:
public void act()
    {
        if (timer == 0) 
        {
            drive();
        } 
        actionWithGrabbed();
    }
You don't have to call the method there, keep "checkHitPedestrian();" at this position. The actionWithGrabbed method will automatically be called by it.
Spock47 Spock47

2023/3/28

#
Ok, please let me know if there is any other problem. (Currently I assume that I added answers to all questions. :) )
KCee KCee

2023/3/28

#
another question regarding lane changes in the lanechange discussion post
danpost danpost

2023/3/28

#
KCee wrote...
poison class code: << Code Omitted >>
All the cloud should do is dissapate and get removed:
import greenfoot.*; 

public class PoisonCloud extends Actor
{
    private int trans = 250;

    public PoisonCloud ()
    {
        getImage().scale(image.getWidth()-150, image.getHeight()-120);
    }
     
    public void act()
    {
        getImage().setTransparency(--trans);
        if (trans < 5) getWorld().removeObject(this);
    }
}
Spock47 Spock47

2023/3/28

#
KCee wrote...
another question regarding lane changes in the lanechange discussion post
I think @danpost is more qualified than me to answer the lane change question (he had already looked in detail into the lane change thread): https://greenfoot.org/topics/65331 (Only thing I can add at this moment: Is the changeLanes method called from somewhere, e.g from the act method?)
KCee KCee

2023/3/28

#
I just wanna thank Spock and Danpost for helping me out these last few days youve been a great help and i wouldnt have been able to finish this project without you guys thanks!
Spock47 Spock47

2023/3/28

#
You're welcome. I am glad to help. I hope you found some fun with programming and keep coding.
You need to login to post a reply.
1
2