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

2017/1/4

Lose a life when the actor gets eaten?

eeriksson eeriksson

2017/1/4

#
Hi! I am having a little problem with this code: public void loseLife() { if (????????????()) { setLocation (300,200); lives = lives -1; // need line here to remove one of the blue dots that represent lives removeObject(Heart.class); if (lives == 0) { Greenfoot.stop(); } } } Where the following question mark is, is where I need help.. There are 4 various other actors trying to eat my actor (Mouse2) those following are: Cactus, Rock, Catspawner, and Bullet. Can any of you help me find a code to remove a life once my actor gets eaten by one of these objects? Maybe this is the wrong code? Thanks!
Super_Hippo Super_Hippo

2017/1/4

#
if (isTouching(Cactus.class) || isTouching(Rock.class) || isTouching(CatSpawer.class) || isTouching(Bullet))
{
    //...you probably have to remove the cactus, rock, catspawner or bullet
    lives--;
    Actor heart = (Actor) getWorld().getObjects(Heart.class).get(lives); //should work if you add the hearts to the world in the right order
    getWorld().removeObject(Heart.class);
    if (lives == 0)
    {
        //...
    }
}
eeriksson eeriksson

2017/1/4

#
I just have a little problem... In my game now my actor is veeery sensitive and even when it is just like a centimeter away it is like it removes the lifes..
danpost danpost

2017/1/4

#
eeriksson wrote...
I just have a little problem... In my game now my actor is veeery sensitive and even when it is just like a centimeter away it is like it removes the lifes..
Do you have excess transparencies around your image(s)?
eeriksson eeriksson

2017/1/4

#
transparencies?
danpost danpost

2017/1/4

#
eeriksson wrote...
transparencies?
Yes. To explain in simplistic terms, let us say we had an image, whose frame is 100 pixels wide by 100 pixels high. Now, the image itself is a diamond, going from the top center to the center of each side and down to the center of the bottom. The areas near the corner of the image frame are not part of the diamond; but, they are still part of the image. Usually those areas around the edges are transparent (sometimes they are white; and those areas should be made transparent -- annoying as they are). Because the transparent areas are part of the image of the actors, they can cause early collisions as you suggest you are having trouble with (some transparent part of one image is touching another image -- though the actors appear to be some short distance apart from each other). Try this as an experiment:
// in actor class constructors
GreenfootImage image = new GreenfootImage(image);
image.drawRect(0, 0, image.getWidth()-1, image.getHeight()-1);
setImage(image);
You can do this with all the classes for actors that will interact (collide) with each other. Then run the program and see if the lives drop before the frames touch each other.
eeriksson eeriksson

2017/1/4

#
Wait, where do I put this code? Do you mind showing a screenshot and an arrow where? Still a bit new with the Greenfoot and Java vocabulary.
danpost danpost

2017/1/4

#
The Actor class templates usually do not provide a constructor. You may have to add them. For example, for an Actor subclass named Cactus, you would add:
public Cactus()
{
    // code provided above here
}
eeriksson eeriksson

2017/1/5

#
Says: "variable image might not have been initialized" (highlighting the word IMAGE in the brackets GreenfootImage (image);
Super_Hippo Super_Hippo

2017/1/5

#
Try to use '100, 100' instead of 'image'.
GreenfootImage image = new GreenfootImage(100, 100);
Well, probably some smaller numbers.
danpost danpost

2017/1/5

#
eeriksson wrote...
Says: "variable image might not have been initialized" (highlighting the word IMAGE in the brackets GreenfootImage (image);
Sorry -- line 2 should be this:
GreenfootImage image = new GreenfootImage(getImage());
eeriksson eeriksson

2017/1/6

#
Still does not work.. With the code you mentioned danpost, it creates a black box around it but the same thing still happens and the code you suggested Super_Hippo: The actor disappears.
eeriksson eeriksson

2017/1/6

#
It muse be something with the code in the Mouse2. The code isTouching.. It might be very sensitive.. Isn't there anyone for isEaten or something?
danpost danpost

2017/1/6

#
You will have to show the entire Mouse2 class and at least one of the other classes that have actor involved -- maybe the Cactus class. That way we may be able to determine better what is going on. Use code tag (there is a 'code' link bellow the reply box and a help page link below that.
You need to login to post a reply.