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

2013/10/30

Spawning according to lifecounter

Baenefin Baenefin

2013/10/30

#
Hello again, In my scenario my character dies when she reaches the bottom of the world. WHat I would like to do it when she reaches the bottom of the world AND has 3,2 or 1 life, spawn het at the starting point. This is what I have got. My mover
public class Mover extends Actor
{
public int vSpeed = 0;

public void setVSpeed(int speed)
    {
        vSpeed = speed;
    }

    public void fall()
    {
        setLocation ( getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
        if ( atBottom())
        gameEnd();
    }

    public boolean atBottom()
    {
        return getY() >= getWorld().getHeight() - 2;
    }       

    public void gameEnd()
    {
        Greenfoot.setWorld(new gameOver());
    }
}

My character
public class Alice extends Mover
{
boolean onGround;

    private void jump()
    {
        setVSpeed(-jumpStrength);
        fall();        
    }

    private void checkFall()
    {
        if (onGround()||onMover() || onUBlock()) {
            setVSpeed(0);          
        }
        else {
            fall();
        }
    }
}



My score
public class Score extends Counters
{
public Score()
    {
        updateImage();
    }

    /**
     * Creates (or re-creates) the image of the object.
     */
    private void updateImage()
    {
        setImage(new GreenfootImage("Life: ", 18, Color.black, new Color(0, 0, 0, 0)));
    }

    /**
     * Adusts the score by <i>addAmt</i>.
     *
     * @param addAmt: the change in the score (negatives decrease the score)
     */
    public void add(int addAmt)
    {
        Life+=addAmt;
        updateImage();
}
}
Any ideas?
ksksks ksksks

2013/10/30

#
try an if(atWorldEnd). any ideas for me? If that doesn't work, somthing similar might. need @world end code?
Baenefin Baenefin

2013/10/30

#
Since I use atBottom to see if my actor is off the world (actor can only fall off worl at bottom) I try to get the Life from my score class. But it won't work this way
   
public class Alice extends Mover  
{  
boolean onGround;  
  
    private void jump()  
    {  
        setVSpeed(-jumpStrength);  
        fall();          
    }  
  
    private void checkFall()  
    {  
        if (onGround()||onMover() || onUBlock()) {  
            setVSpeed(0);            
        }  
        else {  
            fall();  
        }  
    }  

 public void spawner()
    {
        if (atBottom() && (Score)getWorld().getObjects(ShroomScore.class))Life=3;
        {    
            setLocation(20,340);    
        }    
    }
ksksks ksksks

2013/10/30

#
create a seperate class for lives? That might help, because then you would be able to call on the lives class and have scores for other things, not relative to lives! :)
danpost danpost

2013/10/30

#
You will need to work on your 'fall' method. Right now you have:
if (atBottom()) gameEnd();
You need to expand on that to something like the following
if (atBottom())
{
    // if lifes remain restart level else end game
}
ksksks ksksks

2013/10/30

#
his idea is better.
Baenefin Baenefin

2013/10/30

#
Can i get the Life score by
((Score) getWorld().getObjects(Score.class).get(Life));
Baenefin Baenefin

2013/10/30

#
In other words, how can I get the "Life" value from my Score class?
danpost danpost

2013/10/30

#
I think you need to review some information given in the Java tutorials, especially the pages on methods and maybe the section that contains them.
Baenefin Baenefin

2013/10/30

#
Hi again, I feel like such a moron. I got it to work. It was as simple as this. (i am so braindead) Addint a Life int and
    public void fall()
    {
        setLocation ( getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
        if ( atBottom())

        {
            if (Life==3)
            {
                ((Score) getWorld().getObjects(Score.class).get(0)).add(-1);
                //suffer damage
                setLocation(20,340);
            }

            if (Life==2)
            {
                ((Score) getWorld().getObjects(Score.class).get(0)).add(-1);
                //suffer damage
                setLocation(20,340);
            }

            if (Life==1)
            {
                ((Score) getWorld().getObjects(Score.class).get(0)).add(-1);
                //suffer damage
                gameEnd();
            }

        }
    }
Baenefin Baenefin

2013/10/30

#
I have trouble getting the world right. If i am in Level1 (=Level1 world) I would like to spawn at 20,340. If i am in Level2 (=Level2 world) I would like to spawn at 10,55. I should keep track of the world but I have no clue as to how at the moment. Any help would be appreciated. I have:
Private int Level
public void fall()
    {
        setLocation ( getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
        if ( atBottom())
        {
            if (Level ==1)
            {
                ((Score) getWorld().getObjects(Score.class).get(0)).add(-1);
                Greenfoot.playSound("death.wav");
                setLocation(20,340);
            }

            if (Level ==2)
            {
                ((Score) getWorld().getObjects(Score.class).get(0)).add(-1);
                Greenfoot.playSound("death.wav");
                setLocation(10,55);
            }
        }
    }
danpost danpost

2013/10/30

#
Try:
if (getWorld() instanceof Level1) ...; else ...;
You need to login to post a reply.