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

2013/4/2

Life problems...

1
2
JetLennit JetLennit

2013/4/2

#
When i try to do this all the lives go to zero and it gets a game over.... i think it has to do with the fact that it is still in contact with the alien so it does it again but i dont know how to fix it...
 if(getOneIntersectingObject(Alien.class) != null)
        {
           lives -= 1;  
        }
        if(lives == 0)
        {
            JOptionPane.showMessageDialog(null, "Your score was: " + totalCount);
         Space.canStart = (0);
         startbutton.destructother = (0);
         Greenfoot.stop();
         return;   
        }
erdelf erdelf

2013/4/2

#
maybe you should a add a timer, so that the lives are only reduced after a specified time
Yah, you need to add a timer or something similar. I think you should have the alien remove itself immediately after hurting the player. What is happening is that your alien is hitting the player, and then the next frame, he hits the player again, and again, until lives is equal to 0. You either need to add a timer that makes the player "invincible" to being hurt for a few seconds, or remove the alien after right after hurting the player.
JetLennit JetLennit

2013/4/2

#
Nothing seems to be working....... i will just destroy the code and work on it later... (please still give any ideas you have)
erdelf erdelf

2013/4/2

#
replace your code with this
if(System.currentTimeMillis() - lastHurt > timer)
{ 
           if(getOneIntersectingObject(Alien.class) != null)  
           {  
              lives -= 1;    
              lastHurt=System.currentTimeMillis();
           }  
}
           if(lives == 0)  
           {  
               JOptionPane.showMessageDialog(null, "Your score was: " + totalCount);  
            Space.canStart = (0);  
            startbutton.destructother = (0);  
            Greenfoot.stop();  
            return;     
           }  
and add this as global variables:
private int lastHurt;
private static final int timer = 1000; //or whatever you want
and this in you constructor
lastHurt = System.currentTimeMillis();
do something like this for invincible code;
//Instance variables
private static final int INVI_TIME = 100; //For 100 frames, you will be invincible
private int inviTime = INVI_TIME;
private boolean invincible = false;

...

public void act()
{
    if(invinicible)
        beInvincible();
    if(!invincible && runsIntoAlien())
        getHurt();
    ...
}

public void getHurt()
{
    // subtract health
    invincible = true;
}

public void beInvincible()
{
    inviTime--;

    if (inviTime <= 0)
    {
        inviTime = INVI_TIME;
        invincible = false;
    }
}
erdelf wrote...
replace your code with this
Looks like you're using a timer system. I personally like counting frames, but Jet, you can use either of our code.
danpost danpost

2013/4/2

#
This is the basic repetition problem. Every act cycle that the Alien object is intersecting 'this', lives will be decreased until it becomes zero. The way to prevent it would depend on whether you want lives to decrease continuously (but much slower) or just decrease once per contact. To decrease lives once per new alien contact:
// make sure you have these imports
import java.util.List;
import java.util.ArrayList;
// add this instance array
private ArrayList<Alien> aliensContacted = new ArrayList<Alien>();
// then your code would be
ArrayList<Alien> currentContacts = new ArrayList<Alien>();
currentContacts = (ArrayList<Alien>)getIntersectingObjects(Alien.class);
{
    for (Alien alien : aliensContacted)
    {
        if (!currentContacts.contains(alien))
        {
            aliensContacted.remove(alien);
        }
    }
    for (Alien alien : currentContacts)
    {
        if (!aliensContacted.contains(alien))
        {
            aliensContacted.add(alien);
            lives--;
            if (lives == 0)
            {
                String text = "Your score was: " + totalCount;
                JOptionPane.showMessageDialog(null, text);
                Space.canStart = 0;
                startbutton.destructother = 0;
                Greenfoot.stop();
                return;
            }
            // etc.
This should subtract one from lives anytime an alien first comes in contact with 'this'. To decrease once per amount of time in contact with an alien:
// add instance field
private int contactTimer;
// your code would then be
if (getOneIntersectingObject(Alien.class) != null)
{
    contactTimer = (contactTimer + 1) % 20;  // adjust '20' to suit
    if (contactTimer == 0)
    {
        lives--;
        if (lives == 0)
        {
            String text = "Your score was: " + totalCount;
            JOptionPane.showMessageDialog(null, text);
            Space.canStart = 0;
            startbutton.destructother = 0;
            Greenfoot.stop();
            return;
        }
    }
    // etc.
JetLennit JetLennit

2013/4/2

#
It gives the last thing "possible loss of precision required int found long"
erdelf erdelf

2013/4/2

#
would you mind saying us, in which code and at which line?
JetLennit JetLennit

2013/4/2

#
your's and the last thing you said to do
erdelf erdelf

2013/4/2

#
oh sry, change the global variable line to
private long lastHurt;
instead of
private int lastHurt;
JetLennit JetLennit

2013/4/2

#
For @danpost's I get the error java.util.ConcurrentModificationException
JetLennit JetLennit

2013/4/2

#
I modified it to work
danpost danpost

2013/4/2

#
@JetLennit, what code did you end up using? What I mean is, please post the code you ended up with.
There are more replies on the next page.
1
2