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

2014/4/26

Having some problems with removeObject.

Than4tos Than4tos

2014/4/26

#
    public void atacaDown()
    {
        findInimigos findDown = new findInimigos();   
    
        if(Greenfoot.isKeyDown("space") && direction == 1 && !isOnD)
        {
            isOnD = true;
            indice = 0;
            setImage(atacaDown[indice]);
            getWorld().addObject(findDown,getX(),getY()+20);
        }
        if(isOnD)
        {
            indice++;
            if(indice>= atacaDown.length)
            {
                getWorld().removeObject(findDown);      //<------Right here.
                isOnD = false;
                indice = 0;
            }
            setImage(atacaDown[indice]);
        }
    }
So this piece of code is basicly for when i press space it does the attackDown animation all in one go with out interrupting it half way if im not pressing space. So im adding the object but when i need to remove it just doesn't remove and i don't know why and can't figure out why, need some help here. Sorry for any bad english.
danpost danpost

2014/4/26

#
Line 17 is trying to remove the findInimigos object you created at the beginning of the method (which is not the one that IS in the world -- as 'indice' is zero when 'findDown' is the one just put in the world). I think if you move line 3 to outside the method, but still within the class, it might work better:
findInimigos findDown = new findInimigos();   

public void atacaDown()
{
    if(Greenfoot.isKeyDown("space") && direction == 1 && !isOnD)
    {
        isOnD = true;
        indice = 0;
        setImage(atacaDown[indice]);
        getWorld().addObject(findDown,getX(),getY()+20);
    }
    if(isOnD)
    {
        indice++;
        if(indice>= atacaDown.length)
        {
            getWorld().removeObject(findDown);
            isOnD = false;
            indice = 0;
        }
        setImage(atacaDown[indice]);
    }
}
Than4tos Than4tos

2014/4/26

#
Tried that getting java.lang.StackOverflowError.
danpost danpost

2014/4/26

#
Than4tos wrote...
Tried that getting java.lang.StackOverflowError.
Please post (copy/paste) complete error message.
Than4tos Than4tos

2014/4/26

#
java.lang.StackOverflowError at greenfoot.Actor.getClassImage(Actor.java:616) at greenfoot.Actor.<init>(Actor.java:124) at Player.<init>(Player.java:38) at findInimigos.<init>(findInimigos.java:9) and it goes on repeating those 4.
danpost danpost

2014/4/26

#
Ok, let me ask you this: how many findInimigos objects can you possibly have in your world at one time (if the project was working properly)?
Than4tos Than4tos

2014/4/26

#
One at a time in front of the player's char(be it Up Down Left or Right) to see if the attack is connecting with the enemy. That is why i want to remove it after each time the animation of the attack ends.
danpost danpost

2014/4/26

#
Then, try this:
// remove   ****** findInimigos findDown = new findInimigos();  **********

public void atacaDown()
{
    if(Greenfoot.isKeyDown("space") && direction == 1 && !isOnD)
    {
        isOnD = true;
        indice = 0;
        setImage(atacaDown[indice]);
        getWorld().addObject(new findInimigos(),getX(),getY()+20); // changed
    }
    if(isOnD)
    {
        indice++;
        if(indice>= atacaDown.length)
        {
            getWorld().removeObjects(getWorld().getObjects(findInimigos.class)); // changed
            isOnD = false;
            indice = 0;
        }
        setImage(atacaDown[indice]);
    }
}
Than4tos Than4tos

2014/4/26

#
Thank's alot m8 seems to be working as i wanted it.
You need to login to post a reply.