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

2021/2/27

Is there a way to count times the "isTouching" function is repeated?

ThiagoPorto ThiagoPorto

2021/2/27

#
I'm making a game where when the character unleashes a power and hits a rival, the rival's life decreases. But I can only shorten the life of the rival character when he is touched only once. Is there a way to count times the "isTouching" function is repeated?
/**
 * Write a description of class jiren here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class jiren extends Actor
{
    
    
    /**
     * Act - do whatever the jiren wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        apanhar ();
        limite();
        vida();
       
        
                
    }
    
    
    public void vida(){
                
    
    }
    
    
    
    
    public void importa_socos(){
        goku1 goku1 = new goku1();       
        if(isTouching(goku1.class)){
        setImage("jirenap2.png");
        Greenfoot.delay(3);
        setImage("jirenap1.png");
        Greenfoot.delay(3);
        setImage("jirenap1.png");
        Greenfoot.delay(3);
        setImage("jirenap1.png");
        Greenfoot.delay(3);
        
     
    }
        
    
        
    }
    
    
    private void limite (){        
    if (getX()>1200) setLocation(1200, getY());
    if (getX()<20) setLocation(20, getY());
    if (getY()<100) setLocation(getX(),100);
    if (getY()>520) setLocation(getX(),520);
    
    }
    
    
    
    public void apanhar(){
        if(isTouching(energia.class)){
            removeTouching(energia.class);
            
            //Greenfoot.playSound("Explosão - Golpe na Parede.wav");
            Greenfoot.delay(1);
            
            setLocation(getX(), getY() +20);
            setImage("jirenap2.png");
            Greenfoot.delay(1);
            setImage("jirenap3.png");
            Greenfoot.delay(30);
            setImage("Jirenteleporte1.png");
            Greenfoot.delay(1);
            //Greenfoot.playSound("Teletransporte - 01.wav");
            setImage("Jirenteleporte2.png");
            Greenfoot.delay(1);
            
            jiren_vida_1 jiren_vida_1 = (jiren_vida_1)getWorld().getObjects(jiren_vida_1.class).get(0);
            jiren_vida_1.apanhar_1();
           
        }
        
                
        else{
                setImage("jirenbase1.png");
                //Greenfoot.delay(10);
                //setImage("jirenbase2.png");
               // Greenfoot.delay(10);
            }
        
    }
    
    
}
Whenever the "energia" class touches the "jiren" class, the life bar (which I created in the form of an image) changes the image to an image where the life bar is weakened. However, I don't know how to do an "if" to repeat the procedure when the classes "energia" and "jiren" play for the second time...
danpost danpost

2021/2/27

#
Simply add an int field and increment it when touched by an energia object.
ThiagoPorto ThiagoPorto

2021/2/27

#
Where I add this int ?
ThiagoPorto ThiagoPorto

2021/2/27

#
danpost wrote...
Simply add an int field and increment it when touched by an energia object.
I'm new to the world of programming and don't know where to add the int
danpost danpost

2021/2/27

#
ThiagoPorto wrote...
Where I add this int ?
Anywhere in the class not in a sub-block (method or constructor).
You need to login to post a reply.