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

2020/6/5

adding png

1
2
Roshan123 Roshan123

2020/6/5

#
i made a game and in that the enemy class is not in the world its showing me the error that the actor is not in the world and and i dont want to insert my enemy.class to the world
if(health==0)
{
     setImage(blast.png);
     remove(this);     \\removes enemy from the world
}
and also i want to remove the actor after its health is equals to 0 but the problem is that the blast png is not showing after the enemy dies is their any way to show the blast png
Kostya20052011 Kostya20052011

2020/6/5

#
You need the command with the string removeObject() to be at the very end of the object program. So that this "if" is at the very end of the code.
Roshan123 Roshan123

2020/6/5

#
welcome
RcCookie RcCookie

2020/6/5

#
To reply to the problem that the image is not shown, it actually is, but as soon as the image is set, the object gets removed. To solve this there are two options: 1.:
setImage(„blast.png“);
Greenfoot.delay(20);//this value may be tweaked
getWorld().removeObject(this);
This is pretty simple, but the problem is, that Greenfoot.delay() pauses everything in the scenario. So if you have any other objects moving simultaneously while the death, they will stop and pause, too. 2.:
//globally
private int deathAnimationTime = -1;

//in the death method
setImage(„blast.png“);
deathAnimationTime = 0;

//in the act method
public void act(){
    if(deathAnimationTime >=0){
        if(++deathAnimationTime > 20){// value may be tweaked
            getWorld().removeObject(this);
        }
    }
    else{
        //Anything that would normally be in your act method
    }
}
What this does is starting a counter when the death starts. In every following frame it is going to count up instead of doing the stuff it would do alive until it reaches a certain value and finally removes itself. This may be more work but won’t effect any other objects.
danpost danpost

2020/6/6

#
Better, and more appropriate, is to use a different Actor subclass to display the blast. For one thing, a blast is not an enemy type object. The two have totally different behaviors.
Roshan123 Roshan123

2020/6/6

#
danpost wrote...
Better, and more appropriate, is to use a different Actor subclass to display the blast. For one thing, a blast is not an enemy type object. The two have totally different behaviors.
do you mean this or i misunderstand
// insterted Blast blast;
if (health ==0)
{
    getWorld().addObject(blast,this.getX(),this.getY());
removeObject(this);
}
RcCookie RcCookie

2020/6/6

#
Roshan123 wrote...
danpost wrote...
Better, and more appropriate, is to use a different Actor subclass to display the blast. For one thing, a blast is not an enemy type object. The two have totally different behaviors.
do you mean this or i misunderstand
// insterted Blast blast;
if (health ==0)
{
    getWorld().addObject(blast,this.getX(),this.getY());
removeObject(this);
}
Yes and then put the counter stuff into that class
Roshan123 Roshan123

2020/6/6

#
what do you mean by counter stuff will you try to write the code plz
danpost danpost

2020/6/6

#
Roshan123 wrote...
what do you mean by counter stuff will you try to write the code plz
The "counter stuff" is shown above by RcCookie in his (2:) code as deathAnimationTime.
danpost danpost

2020/6/6

#
Roshan123 wrote...
do you mean this or i misunderstand << Code Omitted >>
Well, maybe with "new Blast()" instead of "blast" on line 4.
Kostya20052011 Kostya20052011

2020/6/6

#
I found a small error in the code - you forgot to create a new blast: Blast blast=new Blast (); rather, you should substitute the name of the widget instead of Blast.
Kostya20052011 Kostya20052011

2020/6/6

#
Rather you have already found this error*
Roshan123 Roshan123

2020/6/6

#
in the world i have already written Blast blast=new Blast(); and then i added the blast .class in enemy.class by writing-- Blast blast;// to call the class from the world
Kostya20052011 Kostya20052011

2020/6/6

#
Ok
danpost danpost

2020/6/6

#
Why would you think that "Blast blast;" will call anything from the world? "Blast blast;" , in your enemy class, describes a field that is given to any enemy created and it is given to each enemy with a "null" value. Also, because it is given to the enemy at the time the enemy is created, the created enemy would have no immediate link to your active world.
There are more replies on the next page.
1
2