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

2018/3/7

Move another actor when another mouse is pressed on actor!

LaurentiuRohan LaurentiuRohan

2018/3/7

#
Helo everyone, I have three actors in my world. Circle MoveLeft, Circle MoveRight, Puppy Player. I want that when mouse is pressed on the Cirlc MoveLeft Puppy player moves to the left when CircleMoveRight puppy moves to the right. How could I do that?This is the code for my Puppy Player.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import java.util.ArrayList;
/**
 * Write a description of class Puppy_Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Puppy_Player extends Actor
{
    private int vSpeed = 7;
    private int acceleration = 2;
    private int jumpStrenght = 20;
    public boolean jumping;
    private int speed = 4;
    private boolean onGrounds; 
    private int frame = 1;
    private boolean fell = false;
     private boolean gotHit;
    static GreenfootSound jump = new GreenfootSound("Jump.wav");
    static GreenfootSound die = new GreenfootSound("Die.mp3");
    
    
    private int lives;

    /**
     * Act - do whatever the Doors_Level_Select wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    { 
       checkKey();
       checkFall();
   
       GreenfootImage[] inAnimation = currentAnimation;
       int atFrame = frame;
       int dx = 0, dy = 0;
        if (Greenfoot.isKeyDown("left")) dx--;
        if (Greenfoot.isKeyDown("right")) dx++;
        if (dx  == 0) // idle
         {
        if (idleLeftAnimation != currentAnimation && idleRightAnimation != currentAnimation)
            {
          if (runningRightAnimation == currentAnimation || jumpingRightAnimation == currentAnimation)
             currentAnimation = idleRightAnimation;
          else
             currentAnimation = idleLeftAnimation;
             frame = 0;
            }
        }
        else if (dx == 1 && currentAnimation != runningRightAnimation)
         {
            currentAnimation = runningRightAnimation;
            frame = 0;
         }
        else if (dx == -1 && currentAnimation != runningLeftAnimation)
         {
            currentAnimation = runningLeftAnimation;
            frame = 0;
         }

         setImage(currentAnimation[frame]);
         frame = (frame+1)%currentAnimation.length;
         
       if(getY() >= 540)
        {
            Scrooling_Worlds Scrooling_Worlds = (Scrooling_Worlds)getWorld();
            Scrooling_Worlds.resetCoins();
            die.play();
            if (getWorld() instanceof Mace_Level)
            {
                Greenfoot.setWorld(new GameOver_World());
                
            }
           
            if (getWorld() instanceof Saw_Level)
            {
                Greenfoot.setWorld(new GameOver_World());
            }
            if (getWorld() instanceof Orc_Level)
            {
                Greenfoot.setWorld(new GameOver_World());
            }
       }    
       
        if(getOneObjectAtOffset (0, getImage().getHeight() , Spike_Up.class)!= null)
       {
           Scrooling_Worlds Scrooling_Worlds = (Scrooling_Worlds)getWorld();
            Scrooling_Worlds.resetCoins();
            die.play();
            if (getWorld() instanceof Mace_Level)
            {
                Greenfoot.setWorld(new GameOver_World());    
            }
           
            if (getWorld() instanceof Saw_Level)
            {
                Greenfoot.setWorld(new GameOver_World());
            }
            if (getWorld() instanceof Orc_Level)
            {
                Greenfoot.setWorld(new GameOver_World());
            }
       }   
        
         
 
         if(getOneObjectAtOffset (0, getImage().getHeight() , Water.class)!= null)
       {
           Scrooling_Worlds Scrooling_Worlds = (Scrooling_Worlds)getWorld();
            Scrooling_Worlds.resetCoins();
            die.play();
            if (getWorld() instanceof Mace_Level)
            {
                Greenfoot.setWorld(new GameOver_World());
                
            }
           
            if (getWorld() instanceof Saw_Level)
            {
                Greenfoot.setWorld(new GameOver_World());
            }
            if (getWorld() instanceof Orc_Level)
            {
                Greenfoot.setWorld(new GameOver_World());
            }
       }    
       
        if(getOneObjectAtOffset (0, getImage().getHeight() , Spike_Up.class)!= null)
       {
           Scrooling_Worlds Scrooling_Worlds = (Scrooling_Worlds)getWorld();
            Scrooling_Worlds.resetCoins();
            die.play();
            if (getWorld() instanceof Mace_Level)
            {
                Greenfoot.setWorld(new GameOver_World());    
            }
           
            if (getWorld() instanceof Saw_Level)
            {
                Greenfoot.setWorld(new GameOver_World());
            }
            if (getWorld() instanceof Orc_Level)
            {
                Greenfoot.setWorld(new GameOver_World());
            }
       }   
       
        if(getOneObjectAtOffset (0, getImage().getHeight() , Spike_Down.class)!= null)
       {
           Scrooling_Worlds Scrooling_Worlds = (Scrooling_Worlds)getWorld();
            Scrooling_Worlds.resetCoins();
            die.play();
            if (getWorld() instanceof Mace_Level)
            {
                Greenfoot.setWorld(new GameOver_World()); 
            }
           
            if (getWorld() instanceof Saw_Level)
            {
                Greenfoot.setWorld(new GameOver_World());
            }
            if (getWorld() instanceof Orc_Level)
            {
                Greenfoot.setWorld(new GameOver_World());
            }
       }    
       
       
       
       
         if(getOneObjectAtOffset (0, getImage().getHeight() , Mace.class)!= null)
       {
           Scrooling_Worlds Scrooling_Worlds = (Scrooling_Worlds)getWorld();
           Scrooling_Worlds.resetCoins();
           die.play();
            if (getWorld() instanceof Mace_Level)
            {
                Greenfoot.setWorld(new GameOver_World());   
            }
        }
        
          if(getOneObjectAtOffset (0, getImage().getHeight() , Saw.class)!= null)
       {
           Scrooling_Worlds Scrooling_Worlds = (Scrooling_Worlds)getWorld();
           Scrooling_Worlds.resetCoins();
           die.play();
           if (getWorld() instanceof Saw_Level)
           {
                Greenfoot.setWorld(new GameOver_World());  
           }
        }
        
           if(getOneObjectAtOffset (0, getImage().getHeight() , Orc.class)!= null)
       {
           Scrooling_Worlds Scrooling_Worlds = (Scrooling_Worlds)getWorld();
            Scrooling_Worlds.resetCoins();
            die.play();
            
            if (getWorld() instanceof Orc_Level)
            {
                Greenfoot.setWorld(new GameOver_World());   
            }
        }
        
           
         if(getOneObjectAtOffset (0, getImage().getHeight() , Chest.class)!= null)
       {
           Scrooling_Worlds Scrooling_Worlds = (Scrooling_Worlds)getWorld();
            Scrooling_Worlds.resetCoins();
            die.play();
            if (getWorld() instanceof Orc_Level)
            {
                Greenfoot.setWorld(new YouWin_World());   
            }
        }
        
        
   }   

  public void checkFall()
   {
    if(onGround()) 
     {
        vSpeed = 0;
     } 
    else 
     {
        fall();
     } 
   }
    
   public boolean onGround()
   {
       Actor under = getOneObjectAtOffset(0,getImage().getHeight() /2  ,Grounds.class);
       return under != null;
   }
   
   public void fall()
   {
       setLocation(getX(), getY() + vSpeed);
       vSpeed = vSpeed + acceleration;
   }
   
   public void jump()
   {
       vSpeed = -jumpStrenght;
       speed = 6;
       jump.play();
       fall();
   }    
   
   public void moveRight()
   {
       setLocation(getX()+speed, getY());
   }   
   
   public void moveLeft()
   {
       setLocation(getX()-speed, getY());
   } 
   
   public void checkKey()
   {
       if(Greenfoot.isKeyDown("right"))
        {
            moveRight();
        }
       if(Greenfoot.isKeyDown("left"))
        {
            moveLeft();
        }
       if (Greenfoot.isKeyDown("up") && onGround())
        {
            jump();     
        }
       if (Greenfoot.isKeyDown("down") && (isTouching(Door_Saw.class)))
       {
               Greenfoot.setWorld(new Saw_Level());
       }
       if (Greenfoot.isKeyDown("down") && (isTouching(Door_Mace.class)))
       {
               Greenfoot.setWorld(new Mace_Level());
       } 
       if (Greenfoot.isKeyDown("down") && (isTouching(Door_Orc.class)))
       {
               Greenfoot.setWorld(new Orc_Level());
       }

    }
        
        /** Animatie Alergare Dreapta*/
private final GreenfootImage[] runningRightAnimation =
{
    new GreenfootImage("Puupy_RR1.png" ),
    new GreenfootImage("Puupy_RR2.png"),
    new GreenfootImage("Puupy_RR3.png"),
    new GreenfootImage("Puupy_RR4.png"),
    new GreenfootImage("Puupy_RR5.png"),
    new GreenfootImage("Puupy_RR6.png"),
    new GreenfootImage("Puupy_RR7.png"),
    new GreenfootImage("Puupy_RR8.png"),
    new GreenfootImage("Puupy_RR9.png"),
    new GreenfootImage("Puupy_RR10.png"),
    new GreenfootImage("Puupy_RR11.png"),
    new GreenfootImage("Puupy_RR12.png"),
    new GreenfootImage("Puupy_RR13.png"),
    new GreenfootImage("Puupy_RR14.png"),
    new GreenfootImage("Puupy_RR15.png"),
    new GreenfootImage("Puupy_RR16.png"),
    new GreenfootImage("Puupy_RR17.png"),
    new GreenfootImage("Puupy_RR18.png")
};
 
/** Animatie Alergare Stanga */
private final GreenfootImage[] runningLeftAnimation =
{
    new GreenfootImage("Puupy_RL1.png"),
    new GreenfootImage("Puupy_RL2.png"),
    new GreenfootImage("Puupy_RL3.png"),
    new GreenfootImage("Puupy_RL4.png"),
    new GreenfootImage("Puupy_RL5.png"),
    new GreenfootImage("Puupy_RL6.png"),
    new GreenfootImage("Puupy_RL7.png"),
    new GreenfootImage("Puupy_RL8.png"),
    new GreenfootImage("Puupy_RL9.png"),
    new GreenfootImage("Puupy_RL10.png"),
    new GreenfootImage("Puupy_RL11.png"),
    new GreenfootImage("Puupy_RL12.png"),
    new GreenfootImage("Puupy_RL13.png"),
    new GreenfootImage("Puupy_RL14.png"),
    new GreenfootImage("Puupy_RL15.png"),
    new GreenfootImage("Puupy_RL16.png"),
    new GreenfootImage("Puupy_RL17.png"),
    new GreenfootImage("Puupy_RL18.png")
};
 
/** Animatie Saritura Dreapta*/
private final GreenfootImage[] jumpingRightAnimation =
{
    new GreenfootImage("Puppy_JumpRight1.png"),
    new GreenfootImage("Puppy_JumpRight2.png")
};
 
/** Animatie Saritura Stanga */
private final GreenfootImage[] jumpingLeftAnimation =
{
    new GreenfootImage("Puppy_JumpLeft1.png"),
    new GreenfootImage("Puppy_JumpLeft2.png")
};
 
/** Animatie Inactiv dreapta */
private final GreenfootImage[] idleRightAnimation =
{
    new GreenfootImage("Puupy_Inactive_Right1.png"),
    new GreenfootImage("Puupy_Inactive_Right2.png"),
    new GreenfootImage("Puupy_Inactive_Right3.png"),
    new GreenfootImage("Puupy_Inactive_Right4.png"),
    new GreenfootImage("Puupy_Inactive_Right5.png"),
    new GreenfootImage("Puupy_Inactive_Right6.png"),
    new GreenfootImage("Puupy_Inactive_Right7.png"),
    new GreenfootImage("Puupy_Inactive_Right8.png"),
    new GreenfootImage("Puupy_Inactive_Right9.png"),
    new GreenfootImage("Puupy_Inactive_Right10.png"),
    new GreenfootImage("Puupy_Inactive_Right11.png")
};
 
/** Animatie Inactiv Stamga */
private final GreenfootImage[] idleLeftAnimation =
{
    new GreenfootImage("Puupy_Inactive_Left1.png"),
    new GreenfootImage("Puupy_Inactive_Left2.png"),
    new GreenfootImage("Puupy_Inactive_Left3.png"),
    new GreenfootImage("Puupy_Inactive_Left4.png"),
    new GreenfootImage("Puupy_Inactive_Left5.png"),
    new GreenfootImage("Puupy_Inactive_Left6.png"),
    new GreenfootImage("Puupy_Inactive_Left7.png"),
    new GreenfootImage("Puupy_Inactive_Left8.png"),
    new GreenfootImage("Puupy_Inactive_Left9.png"),
    new GreenfootImage("Puupy_Inactive_Left10.png"),
    new GreenfootImage("Puupy_Inactive_Left10.png")
};
 
/** Animatia curenta */
private GreenfootImage[] currentAnimation = idleRightAnimation;
    }
You need to login to post a reply.