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

2022/4/26

Actor disappearing for seemingly no reason

Harrypotamus Harrypotamus

2022/4/26

#
I have a token actor that's meant to appear whenever a drone is destroyed, and it does, but it instantly gets removed for some reason. I tried just adding a new one manually, and the same thing happened to it every time I run the program. I know the token is gone, because when I right click at the position where it was, it doesn't show up as an option. I posted all of its code here:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * A green collectible token
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class token extends Actor
{
    private int frame = 0;
    private GreenfootImage t1;
    private GreenfootImage t2;
    private GreenfootImage t3;
    private GreenfootImage t4;
    private GreenfootImage t5;
    private GreenfootImage t6;
    private GreenfootImage t7;
    private GreenfootImage t8;
    private GreenfootImage t9;
    private GreenfootImage t10;
    public void token()
    {
    t1 = new GreenfootImage ("token0.png");
    t2 = new GreenfootImage ("token1.png");
    t3 = new GreenfootImage ("token2.png");
    t4 = new GreenfootImage ("token3.png");
    t5 = new GreenfootImage ("token4.png");
    t6 = new GreenfootImage ("token5.png");
    t7 = new GreenfootImage ("token6.png");
    t8 = new GreenfootImage ("token7.png");
    t9 = new GreenfootImage ("token8.png");
    t10 = new GreenfootImage ("token9.png");
    }
    /**
     * Act - do whatever the token wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        animate();
        frame = frame + 1;
        if (frame == 31)
        {
        frame = 0;
        }
    }    
    private void animate()
    {
    if (frame == 3)
        {
        setImage(t1);
        }
        if (frame == 6)
        {
        setImage(t2);
        }
        if (frame == 9)
        {
        setImage(t3);
        }
        if (frame == 12)
        {
        setImage(t4);
        }
        if (frame == 15)
        {
        setImage(t5);
        }
        if (frame == 18)
        {
        setImage(t6);
        }
        if (frame == 21)
        {
        setImage(t7);
        }
        if (frame == 24)
        {
        setImage(t8);
        }
        if (frame == 27)
        {
        setImage(t9);
        }
        if (frame == 30)
        {
        setImage(t10);
        }
    }
}
Harrypotamus Harrypotamus

2022/4/26

#
Whenever I remove animate();, it doesn't disappear. But I can't see anything there that would just straight up remove my actor.
Harrypotamus Harrypotamus

2022/4/26

#
Also, I have tried replacing the images, and it still removes itself.
Harrypotamus Harrypotamus

2022/4/27

#
I even renamed the images, both in the code and also the file names themselves, still nothing changes.
danpost danpost

2022/4/27

#
Harrypotamus wrote...
I even renamed the images, both in the code and also the file names themselves, still nothing changes.
If the actor itself is being removed, then the problem would not be in the codes given. Show codes of any and all classes the either deal with or interact with a token.
Harrypotamus Harrypotamus

2022/4/27

#
There's multiple actors doing this now. Another one is my slide actor.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class slide here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class slide extends Actor
{
    private int frame;
    private GreenfootImage slide0;
    private GreenfootImage slide1;
    public void slide()
    {
    slide0 = new GreenfootImage("slide0.png");
    slide1 = new GreenfootImage("slide1.png");
    }
    /**
     * Act - do whatever the slide wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       frame = frame+1;
        if (frame == 5)
        {
        frame = 0;
        } 
        animate();
    }
    public void animate()
    {
    if (frame > 2)
    {
    setImage(slide0);
    }
    if (frame > 4)
    {
    setImage(slide1);
    }
    }
}
Harrypotamus Harrypotamus

2022/4/27

#
danpost wrote...
Harrypotamus wrote...
I even renamed the images, both in the code and also the file names themselves, still nothing changes.
If the actor itself is being removed, then the problem would not be in the codes given. Show codes of any and all classes the either deal with or interact with a token.
This one is completely self contained, and not referred to by any other actor, and it has the same issue.
Harrypotamus Harrypotamus

2022/4/27

#
It's not a problem with the images themselves, I've gone to the point of completely replacing them with new images, and the problem persists.
danpost danpost

2022/4/27

#
Harrypotamus wrote...
This one is completely self contained, and not referred to by any other actor, and it has the same issue.
Maybe not by any other actor, but an instance must be created and added to the world, plus there may be codes that removes Actor objects or some grouping subclass of Actor objects. And you may have fields that hold some of these objects, as well.
Harrypotamus Harrypotamus

2022/4/27

#
I didn't put any subclasses for anything, and there's nothing I can see that targets Actor specifically. I can post everything that removes anything, if that would help. In bullet.class under Actor
 public void edge()
    {
    if (isAtEdge())
    {
    getWorld().removeObject(this);
    }
    }
In boom.class under Actor
public void act() 
    {
        animate(); 
        frame = frame + 1; //used to animate
        if (frame == 19)
        {
        getWorld().removeObject(this);
        }
        
    }    
in block.class under Actor
public void act() 
    {
        fakeroad fakeroad19 = new fakeroad();
        getWorld().addObject(fakeroad19, getX(), getY());
        setLocation(getX()+30, getY());
        
        if (isAtEdge()){
        getWorld().removeObject(this);}
    } 
Those are just the ones that remove themselves. I can post the ones that target other items specifically, if that would help.
Harrypotamus Harrypotamus

2022/4/27

#
danpost wrote...
Harrypotamus wrote...
This one is completely self contained, and not referred to by any other actor, and it has the same issue.
Maybe not by any other actor, but an instance must be created and added to the world, plus there may be codes that removes Actor objects or some grouping subclass of Actor objects. And you may have fields that hold some of these objects, as well.
I forgot to quote you, I'm not sure if you get a notification if I don't. Sorry.
danpost danpost

2022/4/27

#
Harrypotamus wrote...
I forgot to quote you, I'm not sure if you get a notification if I don't. Sorry.
Quoting is not necessary in regards to notifications. But it helps to give context to the answers given. A notification is always given to anyone already involved within a discussion when a new post is added to it.
danpost danpost

2022/4/27

#
Harrypotamus wrote...
Those are just the ones that remove themselves. I can post the ones that target other items specifically, if that would help.
It might.
Harrypotamus Harrypotamus

2022/4/27

#
danpost wrote...
Harrypotamus wrote...
Those are just the ones that remove themselves. I can post the ones that target other items specifically, if that would help.
It might.
From pinwheel.class in Actor.
public void act() 
    {
        animate();
        setLocation(getX()+6, getY());
        frame = frame + 1; //used to animate
        if (frame == 21)
        {
        frame = 0;
        }
        if (isTouching(pinwheel.class))
        {
        removeTouching(pinwheel.class);
        }
        checkedge();
    }  
from flyer.class in Actor
private void hurtcheck()
    {
    if (isTouching(pinwheel.class))
   {
   boom boom = new boom();
   getWorld().addObject(boom, getX(), getY());
   removeTouching(pinwheel.class);
   token token = new token();
   getWorld().addObject(token, getX(), getY());
   getWorld().removeObject(this);
   }
    }
That's all I could find. I'm not sure of any of this is the culprit, and if it was, I don't know why it would be targetting these two self-contained objects specifically.
Harrypotamus Harrypotamus

2022/4/27

#
I found the issue, I had the initialization as "public void token()" instead of "public token()"
You need to login to post a reply.