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

2020/11/24

How to use a method to touch a different object

ThiagoPorto ThiagoPorto

2020/11/24

#
Hello, I'm developing a fighting game where using an "isKeyDown" method the character of the class "goku1" attacks by changing the shape. I would like that when executing the command (isKeyDown) just touching the class 2 object called "jiren" the object would change the way it is in the "import_socos" method.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

Class goku1:
/**
 * Write a description of class goku1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class goku1 extends Actor
{
    /**
     * Act - do whatever the goku1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        movimentar();
        socos ();
        limite();
       
   
    }
    
     
    private void limite(){
    if (getX()>1200) setLocation(1200, getY());
    if (getX()<20) setLocation(20, getY());
    if (getY()<100) setLocation(getX(),100);
    if (getY()>500) setLocation(getX(),500);
       
    }


    
    public void socos(){
        GreenfootImage image1 = new GreenfootImage("Goku super soco2,1.png");
        GreenfootImage image2 = new GreenfootImage("Goku super soco2,2.png");
        GreenfootImage image3 = new GreenfootImage("Goku super soco2,3.png");
        GreenfootImage image4 = new GreenfootImage("Goku 1,2.png");
        GreenfootImage soco1 = new GreenfootImage("Goku soco 1.png");
        GreenfootImage soco2 = new GreenfootImage("Goku soco 2.png");
        GreenfootImage base1 = new GreenfootImage("Goku 1.png"); 
        GreenfootImage base2 = new GreenfootImage("Goku 1,2.png");
        jiren jiren = new jiren();
        
        
        
        if(Greenfoot.isKeyDown("d")){
            
            
            setImage(soco1);
            Greenfoot.delay(5);
            setImage(soco2);
            Greenfoot.delay(5);            
            setImage(base2);
            
            Greenfoot.playSound("Ataque 02.wav");
            
            
       
        }
        if(Greenfoot.isKeyDown("f") && Greenfoot.isKeyDown("v")){
            setImage("teleporte1.png");
            Greenfoot.delay(4);
            setImage("teleporte2.png");
            Greenfoot.delay(4);
            setImage("teleporte3.png");
            Greenfoot.delay(4);
            setImage("teleporte4.png");
            Greenfoot.delay(4);
            Greenfoot.playSound("Teletransporte - 01.wav");
                        
            setLocation(200, 500);
            
            setImage("teleporte3.png");
            Greenfoot.delay(4);
            setImage("teleporte2.png");
            Greenfoot.delay(4);
            setImage("teleporte1.png");
            Greenfoot.delay(4);
            
            setImage("k1.png");
            Greenfoot.delay(4);
            setImage("k2.png");
            Greenfoot.delay(4);
            Greenfoot.playSound("Poder - Eyebeam_fire.wav");
            
            
            MyWorld cenario = (MyWorld) getWorld ();
            cenario.addObject(new energia(), getX() + 200, getY()) ;
            
                     
          
                           
        }
        
        if(Greenfoot.isKeyDown("q")){  
            jiren = new jiren();
            setImage(image1);
            Greenfoot.delay(5);
            setImage(image2);
            Greenfoot.delay(5);
            setImage(image3);
            Greenfoot.delay(5);
            setImage(base2);
            Greenfoot.playSound ("Ataque 01.wav");
            
            
           
         
                 
        }
        if(Greenfoot.isKeyDown("e")){            
            setImage("chute1,1.png");
            Greenfoot.delay(5);
            setImage("chute1,2.png");
            Greenfoot.delay(5);
            setImage("chute1,3.png");
            Greenfoot.delay(5);
            Greenfoot.playSound ("Ataque 02.wav");
                           
        }
        if(Greenfoot.isKeyDown("a")){            
            setImage("chute2,1.png");
            Greenfoot.delay(5);
            setImage("chute2,2.png");
            Greenfoot.delay(5);
            setImage("chute2,3.png");
            Greenfoot.delay(5);
            setImage("chute2,4.png");
            Greenfoot.delay(5);
            setImage("chute2,5.png");
            Greenfoot.delay(5);
            Greenfoot.playSound("Ataque 02.wav");
            
                           
        }
        if(Greenfoot.isKeyDown("c")){            
            setImage(base2);
            Greenfoot.delay(5);
        }
    
    }
    private void movimentar(){ 
        
        
        
        GreenfootImage image1 = new GreenfootImage("Goku super soco2,1.png");
        GreenfootImage image2 = new GreenfootImage("Goku super soco2,2.png");
        GreenfootImage image3 = new GreenfootImage("Goku super soco2,3.png");
        GreenfootImage image4 = new GreenfootImage("Goku 1,2.png");
        GreenfootImage soco1 = new GreenfootImage("Goku soco 1.png");
        GreenfootImage soco2 = new GreenfootImage("Goku soco 2.png");
        GreenfootImage base1 = new GreenfootImage("Goku 1.png"); 
        GreenfootImage base2 = new GreenfootImage("Goku 1,2.png");
              
       
                
        if(Greenfoot.isKeyDown("left")){
            setLocation(getX() -10, getY());
            setImage("Goku 2 left.png");
        }
        
        
        else{
        setImage(base1);
        }
        
        if(Greenfoot.isKeyDown("right")){
            setLocation(getX() +10, getY());
            setImage("Goku 2 right left.png");
        }
        if(Greenfoot.isKeyDown("up")){
            setImage("Goku cima.png");
            setLocation(getX() , getY() -20);  
                    }
        if(Greenfoot.isKeyDown("down")){
            setImage("Goku baixo.png");
            setLocation(getX() , getY() +20);            
        }
              
        
        
        
      
        
    }
    
}
CLASS jiren:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * 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();
        importa_socos();
        
                
    }
    public void importa_socos(){
        if(isTouching(goku1.class)){
        setImage("jirenap1.png");
        Greenfoot.delay(3);
        setImage("jirenbase2.png");
        Greenfoot.delay(3);
        setImage("jirenap1.png");
        Greenfoot.delay(3);
        setImage("jirenap1.png");
        Greenfoot.delay(3);               
        setImage("jirenap1.png");
        Greenfoot.delay(3);
        setImage("jirenbase2.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);
            setImage("jirenap1.png");
            Greenfoot.delay(3);
            setLocation(getX(), getY() +20);
            setImage("jirenap2.png");
            Greenfoot.delay(3);
            setImage("jirenap3.png");
            Greenfoot.delay(30);
            setImage("Jirenteleporte1.png");
            Greenfoot.delay(3);
            Greenfoot.playSound("Teletransporte - 01.wav");
            setImage("Jirenteleporte2.png");
            Greenfoot.delay(3);
            
            
            
        }
        else{
                setImage("jirenbase2.png");
            }
        
    }
    
}
danpost danpost

2020/11/24

#
By using the delay method, you are not allowing any action to take place (only the animation will run). That is, the jiren object will not act during the animation. As an abstract example, a boxer does not "freeze" when the opponent begins to take a swing. You will need to control the animations across multiple act steps. This will require a counter/timer and knowing which animation is currently being executed. Other actions this actor takes may be governed by this as well.
ThiagoPorto ThiagoPorto

2020/11/24

#
danpost wrote...
By using the delay method, you are not allowing any action to take place (only the animation will run). That is, the jiren object will not act during the animation. As an abstract example, a boxer does not "freeze" when the opponent begins to take a swing. You will need to control the animations across multiple act steps. This will require a counter/timer and knowing which animation is currently being executed. Other actions this actor takes may be governed by this as well.
So... what I want to do exactly is to animate the "import_socos" method of the "jiren" class, but only when I press the "q" key class "jiren"
public void importa_socos(){
        goku1 goku1 = new goku1();       
        if(isTouching(goku1.class)){
        setImage("jirenap1.png");
        Greenfoot.delay(3);
        setImage("jirenbase2.png");
        Greenfoot.delay(3);
        setImage("jirenap1.png");
        Greenfoot.delay(3);
        setImage("jirenap1.png");
        Greenfoot.delay(3);               
        setImage("jirenap1.png");
        Greenfoot.delay(3);
        setImage("jirenbase2.png");
        Greenfoot.delay(3);
        setImage("jirenap1.png");
        Greenfoot.delay(3);
        setImage("jirenap1.png");
        Greenfoot.delay(3);
        }
class "goku1":
if(Greenfoot.isKeyDown("q")){  
            jiren = new jiren();
            setImage(image1);
            Greenfoot.delay(5);
            setImage(image2);
            Greenfoot.delay(5);
            setImage(image3);
            Greenfoot.delay(5);
            setImage(base2);
            Greenfoot.playSound ("Ataque 01.wav");
            
            if(isTouching(jiren.class)){
            jiren.importa_socos();
            }
The problem is that the object of the class "Jiren" is animating before I hit the "q" key, it is animating only when touching the other class. I thank you for your help.
ThiagoPorto ThiagoPorto

2020/11/24

#
Full code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class goku1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class goku1 extends Actor
{
    /**
     * Act - do whatever the goku1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        movimentar();
        socos ();
        limite();
       
   
    }
    
     
    private void limite(){
    if (getX()>1200) setLocation(1200, getY());
    if (getX()<20) setLocation(20, getY());
    if (getY()<100) setLocation(getX(),100);
    if (getY()>500) setLocation(getX(),500);
       
    }


    
    public void socos(){
        GreenfootImage image1 = new GreenfootImage("Goku super soco2,1.png");
        GreenfootImage image2 = new GreenfootImage("Goku super soco2,2.png");
        GreenfootImage image3 = new GreenfootImage("Goku super soco2,3.png");
        GreenfootImage image4 = new GreenfootImage("Goku 1,2.png");
        GreenfootImage soco1 = new GreenfootImage("Goku soco 1.png");
        GreenfootImage soco2 = new GreenfootImage("Goku soco 2.png");
        GreenfootImage base1 = new GreenfootImage("Goku 1.png"); 
        GreenfootImage base2 = new GreenfootImage("Goku 1,2.png");
        jiren jiren = new jiren();
        
        
        
        if(Greenfoot.isKeyDown("d")){
            
            
            setImage(soco1);
            Greenfoot.delay(5);
            setImage(soco2);
            Greenfoot.delay(5);            
            setImage(base2);
            
            Greenfoot.playSound("Ataque 02.wav");
            
            
       
        }
        if(Greenfoot.isKeyDown("f") && Greenfoot.isKeyDown("v")){
            setImage("teleporte1.png");
            Greenfoot.delay(4);
            setImage("teleporte2.png");
            Greenfoot.delay(4);
            setImage("teleporte3.png");
            Greenfoot.delay(4);
            setImage("teleporte4.png");
            Greenfoot.delay(4);
            Greenfoot.playSound("Teletransporte - 01.wav");
                        
            setLocation(200, 500);
            
            setImage("teleporte3.png");
            Greenfoot.delay(4);
            setImage("teleporte2.png");
            Greenfoot.delay(4);
            setImage("teleporte1.png");
            Greenfoot.delay(4);
            
            setImage("k1.png");
            Greenfoot.delay(4);
            setImage("k2.png");
            Greenfoot.delay(4);
            Greenfoot.playSound("Poder - Eyebeam_fire.wav");
            
            
            MyWorld cenario = (MyWorld) getWorld ();
            cenario.addObject(new energia(), getX() + 200, getY()) ;
            
                     
          
                           
        }
        
        if(Greenfoot.isKeyDown("q")){  
            jiren = new jiren();
            setImage(image1);
            Greenfoot.delay(5);
            setImage(image2);
            Greenfoot.delay(5);
            setImage(image3);
            Greenfoot.delay(5);
            setImage(base2);
            Greenfoot.playSound ("Ataque 01.wav");
            
            if(isTouching(jiren.class)){
            jiren.importa_socos();
            }
            
            
            
           
         
                 
        }
        if(Greenfoot.isKeyDown("e")){            
            setImage("chute1,1.png");
            Greenfoot.delay(5);
            setImage("chute1,2.png");
            Greenfoot.delay(5);
            setImage("chute1,3.png");
            Greenfoot.delay(5);
            Greenfoot.playSound ("Ataque 02.wav");
                           
        }
        if(Greenfoot.isKeyDown("a")){            
            setImage("chute2,1.png");
            Greenfoot.delay(5);
            setImage("chute2,2.png");
            Greenfoot.delay(5);
            setImage("chute2,3.png");
            Greenfoot.delay(5);
            setImage("chute2,4.png");
            Greenfoot.delay(5);
            setImage("chute2,5.png");
            Greenfoot.delay(5);
            Greenfoot.playSound("Ataque 02.wav");
            
                           
        }
        if(Greenfoot.isKeyDown("c")){            
            setImage(base2);
            Greenfoot.delay(5);
        }
    
    }
    private void movimentar(){ 
        
        
        
        GreenfootImage image1 = new GreenfootImage("Goku super soco2,1.png");
        GreenfootImage image2 = new GreenfootImage("Goku super soco2,2.png");
        GreenfootImage image3 = new GreenfootImage("Goku super soco2,3.png");
        GreenfootImage image4 = new GreenfootImage("Goku 1,2.png");
        GreenfootImage soco1 = new GreenfootImage("Goku soco 1.png");
        GreenfootImage soco2 = new GreenfootImage("Goku soco 2.png");
        GreenfootImage base1 = new GreenfootImage("Goku 1.png"); 
        GreenfootImage base2 = new GreenfootImage("Goku 1,2.png");
              
       
                
        if(Greenfoot.isKeyDown("left")){
            setLocation(getX() -10, getY());
            setImage("Goku 2 left.png");
        }
        
        
        else{
        setImage(base1);
        }
        
        if(Greenfoot.isKeyDown("right")){
            setLocation(getX() +10, getY());
            setImage("Goku 2 right left.png");
        }
        if(Greenfoot.isKeyDown("up")){
            setImage("Goku cima.png");
            setLocation(getX() , getY() -20);  
                    }
        if(Greenfoot.isKeyDown("down")){
            setImage("Goku baixo.png");
            setLocation(getX() , getY() +20);            
        }
              
        
        
        
      
        
    }
    
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * 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();
        importa_socos();
        
                
    }
    public void importa_socos(){
        goku1 goku1 = new goku1();       
        if(isTouching(goku1.class)){
        setImage("jirenap1.png");
        Greenfoot.delay(3);
        setImage("jirenbase2.png");
        Greenfoot.delay(3);
        setImage("jirenap1.png");
        Greenfoot.delay(3);
        setImage("jirenap1.png");
        Greenfoot.delay(3);               
        setImage("jirenap1.png");
        Greenfoot.delay(3);
        setImage("jirenbase2.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);
            setImage("jirenap1.png");
            Greenfoot.delay(3);
            setLocation(getX(), getY() +20);
            setImage("jirenap2.png");
            Greenfoot.delay(3);
            setImage("jirenap3.png");
            Greenfoot.delay(30);
            setImage("Jirenteleporte1.png");
            Greenfoot.delay(3);
            Greenfoot.playSound("Teletransporte - 01.wav");
            setImage("Jirenteleporte2.png");
            Greenfoot.delay(3);
            
            
            
        }
        else{
                setImage("jirenbase2.png");
            }
        
    }
    
}
danpost danpost

2020/11/24

#
ThiagoPorto wrote...
The problem is that the object of the class "Jiren" is animating before I hit the "q" key, it is animating only when touching the other class.
Remove line 19 from jiren class.
ThiagoPorto ThiagoPorto

2020/11/24

#
danpost wrote...
ThiagoPorto wrote...
The problem is that the object of the class "Jiren" is animating before I hit the "q" key, it is animating only when touching the other class.
Remove line 19 from jiren class.
It turns out that now (after deleting line 19), when I press "q" when touching the "jiren" class, the following error message appears: " java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed at jiren.importa_socos(jiren.java:25) at goku1.socos(goku1.java:109) at goku1.act(goku1.java:18) java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed at jiren.importa_socos(jiren.java:25) at goku1.socos(goku1.java:109) at goku1.act(goku1.java:18) java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed at jiren.importa_socos(jiren.java:25) at goku1.socos(goku1.java:109) at goku1.act(goku1.java:18)"
danpost danpost

2020/11/24

#
Remove line 98 in goku1 class. Obtain a reference to the jiren actor being touched at line 108 and call importa_socos on it.
ThiagoPorto ThiagoPorto

2020/11/25

#
danpost wrote...
Remove line 98 in goku1 class. Obtain a reference to the jiren actor being touched at line 108 and call importa_socos on it.
Thank you very much, it worked!
You need to login to post a reply.