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

2019/3/15

How do I take damage from collision without Animal class??

KennyBoy KennyBoy

2019/3/15

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Goku here. * * @author (your name) * @version (a version number or a date) */ public class Goku extends Actor { public int gokuRotation; boolean touchingMinion = false; public static int gokuX, gokuY; /** * Act - do whatever the Goku wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(4); shoot(); gokuRotation = getRotation(); checkKeys(); gokuX = getX(); gokuY = getY(); } public void checkKeys() { if (Greenfoot.isKeyDown("a")) { turn(-5); } if (Greenfoot.isKeyDown("d")) { turn(5); } } public void shoot() { if(Greenfoot.getMouseInfo() != null) { if(Greenfoot.getMouseInfo().getButton() == 1) getWorld() .addObject(new Bullet(gokuRotation), getX(), getY()); } } public boolean isTouching() { return getOneIntersectingObject(Minion.class) != null; } }
danpost danpost

2019/3/15

#
The Animal class is pretty much obsolete as the Actor class methods isTouching, removeTouching and isAtEdge are valid replacements for the Animal class methods canSee, eat and atWorldEdge. Also, the Actor class already has fields for rotation, x and y. You do not need to write any code for any of the aforementioned methods or fields. In your Goku class, remove the isTouching method and the fields named gokuRotation, gokuX and gokuY along with the three lines in the act method dealing with those fields.
KennyBoy KennyBoy

3 days ago

#
danpost do you know how to take damage though because ive looked on the discussions and youtube but all of them dont work.
danpost danpost

3 days ago

#
KennyBoy wrote...
danpost do you know how to take damage though because ive looked on the discussions and youtube but all of them dont work.
You would use something like this:
if (isTouching(Minion.class))
{
    // possible follow up
    removeTouching(Minion.class);
    // take damaage here
}
Since I do not know what kind of "health" or "lives" system you want, I cannot be more specific.
You need to login to post a reply.