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

2012/6/10

Actors death

1
2
Benedict Benedict

2012/6/10

#
hi everyone, i need your help again. I want an actor to die when a certain picture of another actor touches him. thanking you in anticipation :)
MatheMagician MatheMagician

2012/6/10

#
if(getOneIntersectingObject(Enemy.class) != null)
{

     if(){
         getWorld().removeObject(this);
         return;
     }
}
MatheMagician MatheMagician

2012/6/10

#
Sorry, I accidentally posted while writing the code. This is the actual code:
if(getOneIntersectingObject(Enemy.class) != null)  
{  
     Actor enemy = getOneIntersectingObject(Enemy.class);
     if(enemy.getImage() == killer){  
         getWorld().removeObject(this);  
         return;  
     }  
}  
When defining your variables in the actor's class that is being attacked, just define the image that will destroy it as killer.
Benedict Benedict

2012/6/12

#
i dont understand, should i replace "killer" by the name of the image of Enemy.class or what?
danpost danpost

2012/6/12

#
The best way to handle it, is to add an instance String variable to hold the name of the image that the actor currently displays (set the variable in the constructor; and anywhere else that the image may change at). Then use the following code:
if (getOneIntersectingObject(Enemy.class) != null)
{
    Enemy enemy = (Enemy) getOneIntersectingObject(Enemy.class);
    if ("killer.png".equals(enemy.currentImage))  getWorld().removeObject(this);
}
where 'killer.png' is the name of the image you are checking for and 'currentImage' is the name of the String instance variable that holds the name of the current image.
Benedict Benedict

2012/6/13

#
thx :)
Benedict Benedict

2012/6/13

#
i figured out an easier way, but still really need your help i made a variable named "health". public void getHurt() { health--; } public void die() { if(health <= 0) { setImage("death.PNG"); Greenfoot.delay(5); getWorld().removeObject(this); } } now the problem is that i cant figure out how to activate getHurt(); when the certain picture of the other character touches him. thanking you in anticipation :)
danpost danpost

2012/6/13

#
Use the same code as above; just replace 'getWorld().removeObject(this)' with 'getHurt()' and adjust the image name.
ArchAngel ArchAngel

2012/6/14

#
It can't find the variable for currentImage. why is that??
danpost danpost

2012/6/14

#
Did you declare the variable 'currentImage' as a String type variable, and set it to the filename of the image that the actor is currently using?
Benedict Benedict

2012/6/14

#
you guys really helped me with my school project, i just want to thank you :)
ArchAngel ArchAngel

2012/6/18

#
I don't know what you mean? I'm not used to use a string.
danpost danpost

2012/6/18

#
@ArchAngel, maybe you should check the Java trail. Start here.
Girl2012 Girl2012

2012/6/18

#
hello I have got the same problem. I have got a sheep that will die if it touches a bear for example. how will it happens? i set the code from danpost but there is a errore with the 'currentImage' it cann't find the symbol. why?
danpost danpost

2012/6/18

#
You need to set the String variable of 'currentImage' to the name of the image (i.e. "bear.png" or "sheep.png") that the object is currently showing. So, for example, let us say you have 2 images for the bear object. You could start your class this way:
public class Bear extends Actor
{
    String image1 = "bear.png";
    String image2 = "bear2.png";
    String currentImage = image1;
Then, let us say that when the bear eats a sheep, its image changes:
private void eat(Class cls)
{
    Actor actor = getOneIntersectingObject(cls);
    getWorld().removeObject(actor);
    currentImage = image2;
    setImage(currentImage);
}
This code is just an example of how to set up a String to track to current image of an actor. The actual use of it in the example is not neccessary, but the setting up and code usage of it is valid. @Girl2012, if you are just removing the sheep when it touches a bear, and the bear image will not change, then you really do not need to use a 'currentImage' variable in your code. Normally, you will see the methods 'eat(Class cls)' and 'see(Class cls)' together; called by a statement similar to:
if (see(Sheep.class)) eat(Sheep.class);
where the 'see(Class cls)' method is:
private boolean see(Class cls)
{
    return getOneIntersectingObject(cls) != null;
}
You could move the image manipulation code to a seperate method, and instead of 'if(see)eat;', you could use:
if (see(Sheep.class))
{
    eat(Sheep.class);
    alterImage(image2);
}
where 'alterImage(String img)' is:
private void alterImage(String img)
{
    currentImage = img;
    setImage(currentImage);
}
There are more replies on the next page.
1
2