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

2013/12/8

How to create an enemy that can take multiple hits

1
2
MontclairState MontclairState

2013/12/8

#
Hello I was wondering how you can create an enemy that can take more than one hit from a gun or something like that.
MontclairState MontclairState

2013/12/8

#
This is the code i have so far and it doesn't do anything
private Rocket myShip;
    private int hitcounter = 3;
    public Shot(Rocket myShip)
    {
        setImage("beeper.png");
        getImage().scale(25, 25);
        this.myShip = myShip;
        setRotation(myShip.getRotation());
    }
    public void act() 
    {
        move(5);
        if((this.getX() < 5 || this.getX() > this.getWorld().getWidth() - 5) && (this != null))  
        {  
            this.getWorld().removeObject(this);  
            return;  
        }  
        if((this.getY() < 5 || this.getY() > this.getWorld().getHeight() - 5)  && (this != null))  
        {  
            this.getWorld().removeObject(this);  
            return;  
        }
        
           
        Actor ball = getOneIntersectingObject(Target.class);
        if (hitcounter == 0) {
            getWorld().removeObject(ball);
        }
        if (ball != null) {
                hitcounter = hitcounter - 1;
                getWorld().removeObject(this);
                
                return;
        }
        
        
        
    }
danpost danpost

2013/12/8

#
The code you have shown is the 'Shot' class. It is not the shot that is taking the damage (or that will be hit several times). The 'hitcounter' field should be in the 'Target' class (I guess that is your enemy class). Then, you can replace everything from line 25 and on with:
Target target = (Target)getOneIntersectingObject(Target.class);
if (target != null)
{
    target.takeDamage();
    getWorld().removeObject(this);
    return;
}
Then you can add a 'public void takeDamage()' to the Target class to control the value of the hitcounter field and check if its value is zero for removal.
MontclairState MontclairState

2013/12/8

#
Okay tried
private int hitcounter = 3;
    public void act() 
    {
        if (hitcounter == 0)
        {
            getWorld().removeObject(this);
        }
    }  
    public void takeDamage()
    {
        hitcounter = hitcounter - 1;
    }
and changed the code like you said
public void act() 
    {
        move(5);
        if((this.getX() < 5 || this.getX() > this.getWorld().getWidth() - 5) && (this != null))  
        {  
            this.getWorld().removeObject(this);  
            return;  
        }  
        if((this.getY() < 5 || this.getY() > this.getWorld().getHeight() - 5)  && (this != null))  
        {  
            this.getWorld().removeObject(this);  
            return;  
        }
        
           
        Target target = (Target)getOneIntersectingObject(Target.class);  
        if (target != null)  
        {  
            target.takeDamage();  
            getWorld().removeObject(this);  
            return;  
        }
        
        
        
    }
but still it doesn't get removed after three hits
Super_Hippo Super_Hippo

2013/12/8

#
In Greenfoot, you can try if the int hitcounter actually decreases if the target got hit. Then it is easier to see which part of your code is the problem. By the way, what is the reason for those "return;" in your methods? I thought you do not have to return anything in the "void" type. And what do they actually return there? Oh and what is the "this" for? It looks like you want to get the coordinates of the rocket and not of the "this" (ship?) to remove the rocket when it reaches the end of the world.
danpost danpost

2013/12/8

#
Super_Hippo wrote...
By the way, what is the reason for those "return;" in your methods? I thought you do not have to return anything in the "void" type. And what do they actually return there?
You can use 'return' in 'void' methods; you just do not return a value. The use of it will exit the method at that point. This can avoid unwanted exceptions later in the method or prevent the rest of the method from being executed. In this case, it is almost like a combination of both in that by preventing the rest of the method from executing after the object has been removed from the world, an exception is avoided. Granted, the last use of 'return' is not necessary as no code is being avoided there; but the others are necessary, or 'getOneIntersectingObject' will cause an IllegalStateException because you cannot get intersectors of an object that has already been removed from the world.
Super_Hippo wrote...
Oh and what is the "this" for? It looks like you want to get the coordinates of the rocket and not of the "this" (ship?) to remove the rocket when it reaches the end of the world.
I believe the code using 'this' is in the Shot class (not the Ship class). @MontclairState, move lines 4 through 7 out of your 'act' method and into your 'takeDamage' method after line 11 .
Super_Hippo Super_Hippo

2013/12/8

#
Okay, so the "return" is something similar like the "break;" in a switch construction, right? About this "this": For me, it would only make sense to put the method, which removes an object that leaves the world, in the code of the class of this object. If this "this" is in the Target class and is referring to the shot, (which is, if I got it right, the bullet from the rocket which is on the ship,) does not every target checks whether or not the last shot is outside the world? And what happens if there is another shot before the other shot is removed?
danpost danpost

2013/12/8

#
Super_Hippo wrote...
Okay, so the "return" is something similar like the "break;" in a switch construction, right? About this "this": For me, it would only make sense to put the method, which removes an object that leaves the world, in the code of the class of this object. If this "this" is in the Target class and is referring to the shot, (which is, if I got it right, the bullet from the rocket which is on the ship,) does not every target checks whether or not the last shot is outside the world? And what happens if there is another shot before the other shot is removed?
Yes, the 'return' statement is to a method as the 'break' statement is to a block of code (whether in a 'for', 'switch' or other similar block). Like I said, I believe the code using 'this' is in the Shot class. However, if it was in the Target class, which is not the bullet from the ship (refer to the initial code provided by MontclaireState), it would be checking to see if the Target object is outside the world and all Shot objects would be irrelevant.
Super_Hippo Super_Hippo

2013/12/8

#
I think the code was in the Shot class in the beginning, but after your first post, he transferred it into the Target Class as you said. (I am not saying, that you made a wrong suggestion, just in case you think so. This sentence sounds a bit like it.) Of what exactly do you mean with "using" 'this'? I interpreted it, that it is the class which has the code. I have to say that it is not easy for me to guess what his plan is.
danpost danpost

2013/12/8

#
The post said nothing moving the method; just the 'hitCounter' field; and I thought I was pretty clear when stating something had to go in the Target class ('replacing' code does not infer moving it also). Maybe that was not clear to you. The keyword 'this' refers to the object that is either being constructed or acted on. All non-static methods can use the 'this' keyword to reference the object currently being worked on. When a constructor is being executed, it refers to the object being created. When a non-static method is being executed, it refers to an object that was already created from that class. The 'act' method is run on the active World object and all Actor objects within that world; and 'this' would refer to that World or Actor object that the 'act' method is being run on. Other non-static methods can be explicitly called on objects using 'actorReference.methodName' and 'this' would refer to the referenced actor.
Super_Hippo Super_Hippo

2013/12/8

#
danpost wrote...
The post said nothing moving the method; just the 'hitCounter' field; and I thought I was pretty clear when stating something had to go in the Target class ('replacing' code does not infer moving it also). Maybe that was not clear to you.
Oh yes, you are right. Looks like I misread that. I think I will never understand the use of "this". I would not call me dumb, but that is quite complicated.
danpost danpost

2013/12/8

#
'this' refers to the object of the class (whether being created or already created) that the constructor or method (within that class) is being called on. It will always refer to an object of the class from which you find it (if you find it in the code of a subclass of World, it refers to a specific world object; if you find it in the code of a subclass of Actor, it refers to a specific actor object).
Super_Hippo Super_Hippo

2013/12/8

#
danpost wrote...
'this' refers to the object of the class (whether being created or already created) that the constructor or method (within that class) is being called on.
That part is what confuses me. What do you mean "is being called on"? If I use "this.method()" in a constructor, what exactly does it do? Does it run the method of the object of the class which is about to be added to the world? Or to what else? Because if I would use "method()" without 'this' in the same constructor, it would actually do the same. Same thing in a method. If I call a method in a class, then this method is only for the object of the class. I do not need a 'this' for this because it does it automatically. At least that is how I understand it. In my game where you helped me with the child object, I understood the meaning and reason of 'this'. There, the 'this' really meant the actual object because it was important for later references. But here, in this case, I do not see any reason.
danpost danpost

2013/12/8

#
I did not say it was necessary to use 'this' to call a method of that class. It is optional. Some believe it is good programming technique to prefix all method calls with an object reference. In the game where I helped you with the child object, I believe we were dealing with two variable that had the same name; one was local to the method (or constructor) and the other was an instance field name belong to an object created from that class (or being created). One could actually call a method on a different object of the same class from the constructor of that class (while creating an object of that class). I will double-check this and upload a demo scenario (or post that I was wrong about this).
danpost danpost

2013/12/9

#
Check out my demo on the uses of 'this'. I believe that all method calls are prefixed with an object reference in the whole project. It also demos creating a portal system.
There are more replies on the next page.
1
2