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

2013/5/26

How can I make an enemy "lose health" when an arrow hits it?

1
2
Yongjoon Yongjoon

2013/5/28

#
no, when i press the "/" key to fire, a ton of arrows come out
Yongjoon Yongjoon

2013/5/28

#
but there is only one purpMin
danpost danpost

2013/5/28

#
Is the 'getLeft' method declared static, or no?
Yongjoon Yongjoon

2013/5/28

#
the little red line comes after: if(VayneArrow.getLeft().equals(PurpMins.getLeft())(here)
Yongjoon Yongjoon

2013/5/28

#
...how do i make it static?...
danpost danpost

2013/5/28

#
Please show your 'getLeft' method code.
Yongjoon Yongjoon

2013/5/28

#
ah ok, so the error says: "cannot find symbol - method getLeft()"
Yongjoon Yongjoon

2013/5/28

#
i don't have one lol. I thought it was a premade method that got the location of the image's left border
Yongjoon Yongjoon

2013/5/28

#
Is there any such thing that i can use?
danpost danpost

2013/5/28

#
Alright. First, classes do not have images. You can, however, set a default image (in Greenfoot) for objects created from that class. It is the actor objects (and world objects) created from the classes that have images. Therefore, you cannot, even if 'getLeft' was a defined method, use it on a class. You need to use it on a specific object created from that class. Now, if you did have a specific object that you needed to know the location of the left edge of its image, you would use something like to following:
Actor actor = /* expression or method name returning an actor object */
int leftEdgeLocationOfActor = actor.getX()-actor.getImage().getWidth()/2;
I am presuming that the world cellsize is one (or you probably would not need this code). 'getX' returns how many pixels away the center of the image of the actor is from the left edge of the world. 'getImage().getWidth()' returns how many pixels wide the image of the actor is. So, the x-location of the actor minus half the width of the image of the actor should be the x-location of the left edge of the image of the actor. If there is only one instance of a class, you can get a reference to it with the following:
Actor purpmin = (Actor)getWorld().getObjects(PurpMin.class).get(0);
This 'get's the one, and only, instance of a PurpMin object that is in the world and assigns it to a new local field called 'purpmin'. Then, you can use 'purpmin.getX()' and any other Actor class method.
Yongjoon Yongjoon

2013/5/28

#
Thanks a bunch. The arrow hits, vanishes, and damages the minion. Thanks! ^^
You need to login to post a reply.
1
2