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

2018/4/23

Losing a life once on actor is touching another

LobsterL LobsterL

2018/4/23

#
I'm having difficulty in making my frog lose a life if it touches any of the cars. Greenfoot says that a non-static method cannot be referenced from a static method.
public class Cars extends Actor
{

    /**
     * Act - do whatever the Cars wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
    }  
    public void removeWhenTouching()
    {
                if(isTouching(Frog.class))
        {
            removeTouching(Frog.class);
            if(Frog.getLives() == 1)
            {
                Greenfoot.stop();
            }else{
                Frog.loseLife();
            }
            
        }
    }
}
The remove when touching is placed in each individuals cars act method. This is also the code of the frog class.
public class Frog extends Actor
{
    private String[] move = {"frogup1.png","frogup2.png","frogup3.png","frogup4.png","frogup5.png","frogup6.png"};
    private int frameCounterMove = 0;
    private int reload;
    int lives = 3;
    /**
     * Act - do whatever the Frog wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move();
        if (isTouching(Car3.class))
        { 
            loseLife();
        }
        if (isTouching(Car4.class))
        { 
            loseLife();
        }
        if (isTouching(Car2.class))
        { 
            loseLife();
        }
        if (isTouching(Car1.class))
        { 
            loseLife();
        }
    }  
    public void move()
    {
        if (Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY()-5);
            setRotation(360);
            animateOnMove();
        }
        
        else if (Greenfoot.isKeyDown("left"))
        {
            setLocation(getX()-5, getY());
            setRotation(270);
            animateOnMove();
        }
        
        else if (Greenfoot.isKeyDown("right"))
        {
            setLocation(getX()+5, getY());
            setRotation(90);
            animateOnMove();
        }
        
        else if (Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(), getY()+5);
            setRotation(180);
            animateOnMove();
        }
    }
    public void animateOnMove()
    {
        reload++;
        if(reload==3)
       {
        setImage(move[frameCounterMove]);
        frameCounterMove++;
        if(frameCounterMove >= move.length)
          frameCounterMove=0; 
        reload = 0;
      }
    }
    public int getLives()
    {
       return lives;
    }
    public void loseLife()
    {
        lives = lives - 1;
    }
}

danpost danpost

2018/4/23

#
"Frog" is a class name -- it is not an object created from the class. Your Frog class does not have lives -- the Frog objects that you create from the class has lives. Only do collision between two objects from one of the classes -- not both. Best would be to stay in the Frog class here.
LobsterL LobsterL

2018/4/26

#
Thanks
LobsterL LobsterL

2018/4/26

#
Greenfoot now stops when my frog touches a car and i haven't called it to do that at all. here is the code for my frog
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Frog here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Frog extends Actor
{
    private String[] move = {"frogup1.png","frogup2.png","frogup3.png","frogup4.png","frogup5.png","frogup6.png"};
    private int frameCounterMove = 0;
    private int reload;
    int lives = 3;
    // Lives life = new Lives();
    /**
     * Act - do whatever the Frog wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move();
        removeWhenTouching();
        if (isTouching(Car3.class))
        { 
            loseLife();
        }
        if (isTouching(Car4.class))
        { 
            loseLife();
        }
        if (isTouching(Car2.class))
        { 
            loseLife();
        }
        if (isTouching(Car1.class))
        { 
            loseLife();
        }
    }  
    public void move()
    {
        if (Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY()-5);
            setRotation(360);
            animateOnMove();
        }
        
        else if (Greenfoot.isKeyDown("left"))
        {
            setLocation(getX()-5, getY());
            setRotation(270);
            animateOnMove();
        }
        
        else if (Greenfoot.isKeyDown("right"))
        {
            setLocation(getX()+5, getY());
            setRotation(90);
            animateOnMove();
        }
        
        else if (Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(), getY()+5);
            setRotation(180);
            animateOnMove();
        }
    }
    public void animateOnMove()
    {
        reload++;
        if(reload==3)
       {
        setImage(move[frameCounterMove]);
        frameCounterMove++;
        if(frameCounterMove >= move.length)
          frameCounterMove=0; 
        reload = 0;
      }
    }
    public int getLives()
    {
       return lives;
    }
    public void loseLife()
    {
        lives = lives - 1;
        // getWorld().removeObject(life);
    }
    public void removeWhenTouching()
    {
                if(isTouching(Car1.class))
        {
            removeTouching(Frog.class);
            if(getLives() == 1)
            {
                Greenfoot.stop();
            }else{
                loseLife();
            }
            
        }
               if(isTouching(Car2.class))
        {
            removeTouching(Frog.class);
            if(getLives() == 1)
            {
                Greenfoot.stop();
            }else{
                loseLife();
            }
            
        }
               if(isTouching(Car3.class))
        {
            removeTouching(Frog.class);
            if(getLives() == 1)
            {
                Greenfoot.stop();
            }else{
                loseLife();
            }
            
        }
               if(isTouching(Car4.class))
        {
            removeTouching(Frog.class);
            if(getLives() == 1)
            {
                
            }else{
                loseLife();
            }
            
        }
    }
    // public boolean OutOfBounds(){
    
        // int x = getX();
        // int y = getY();
        
    // }
}


danpost danpost

2018/4/26

#
And what, now, is the code for a car?
LobsterL LobsterL

2018/4/26

#
i have 4 cars here are each of there codes
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Cars here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Cars extends Actor
{
    
    /**
     * Act - do whatever the Cars wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {

    }
    public void atTheEdge()
    {
                if (isAtEdge())
        {
            getWorld().removeObject(this);
        }
    }
    }


import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Car1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Car1 extends Cars
{
    /**
     * Act - do whatever the Car1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(6);
        // removeAtWorldEdge();
        atTheEdge();
    }    
            // public void removeAtWorldEdge()
    // {
        // if (isAtEdge())
        // {
            // // getWorld().removeObject(this);
            // // spawnTimer();
            // setLocation(10, 630);
        // }
    // }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Car2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Car2 extends Cars
{
    /**
     * Act - do whatever the Car2 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(-3);
        // removeAtWorldEdge();
        atTheEdge();
    }    
                // public void removeAtWorldEdge()
    // {
        // if (isAtEdge())
        // {
            // // getWorld().removeObject(this);
            // // spawnTimer();
            // setLocation(995, 736);
        // }
    // }
}

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Car3 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Car3 extends Cars
{
    /**
     * Act - do whatever the Car3 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(-3);
        // removeAtWorldEdge();
        atTheEdge();
    }    
                // public void removeAtWorldEdge()
    // {
        // if (isAtEdge())
        // {
            // // getWorld().removeObject(this);
            // // spawnTimer();
            // setLocation(995, 736);
        // }
    // }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Car4
 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Car4 extends Cars
{
    /**
     * Act - do whatever the Car4 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(-3);
        // removeAtWorldEdge();
        atTheEdge();
    }    
                // public void removeAtWorldEdge()
    // {
        // if (isAtEdge())
        // {
            // // getWorld().removeObject(this);
            // // spawnTimer();
            // setLocation(995, 736);
        // }
    // }
}
danpost danpost

2018/4/26

#
LobsterL wrote...
i have 4 cars here are each of there codes
Just making sure -- because you previously had Greenfoot.stop calls in them. Unless I am mistaken, I do not think that removeTouching will remove this Frog actor. It should only remove a Frog object that touches this Frog actor. However, it would only take touching one Car1, Car2 or Car3 object for the game to stop with lines 99, 110 and 121 in the Frog class. If neither is removed from the world, which I suspect, then it would take 2 act steps to decrease lives to 1 and stop the game (2 act steps is about 0.03 seconds).
LobsterL LobsterL

2018/4/26

#
Yes it doesn't remove the frog but what it does do it stop the game when i have only coded it to stop when the lives are equal to 1 not when 1 life is removed. so i'm wondering why it does that and not do what i want it to do?
danpost danpost

2018/4/26

#
LobsterL wrote...
Yes it doesn't remove the frog but what it does do it stop the game when i have only coded it to stop when the lives are equal to 1 not when 1 life is removed. so i'm wondering why it does that and not do what i want it to do?
Yes. And I explained why in my last post.
LobsterL LobsterL

2018/4/27

#
Thanks Danpost, i have realized that without creating a reset method the actor will continuously lose lives as it touches the cars.
You need to login to post a reply.