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

2018/4/19

Two Player Game Movement Stuck

zainsou zainsou

2018/4/19

#
I'm making a two player game and the movement isn't working. When there is only one actor on the screen it works fine but when both are on at the same time some freeze or only one can move at a time. The code for the movement is
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Font;
/**
 * Write a description of class Fighter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public abstract class Fighter  extends Actor 
{
    public boolean isUp = true; 
    public long time=0;
    private int fallSpeed;
    private int fallSpeed2;
   
    public void movement()
    {  
    if(Greenfoot.isKeyDown("right") && getX()<1230 && !contact(Fighter.class))
    {setLocation(getX()+5,getY());
         }
    if(Greenfoot.isKeyDown("left") && getX()>50)
        {setLocation(getX()-5,getY());
      } 
      boolean onGround = (getY() == 388);
        if(!onGround) // in middle of jump
        {
           
            fallSpeed++; // adds gravity effect
            setLocation(getX(), getY()+fallSpeed); // fall (rising slower or falling faster)
            if (getY()>=388 ) // has landed (reached ground level)
            {
                setLocation(getX(), 388); // set on ground
                Greenfoot.getKey(); // clears any key pressed during jump
           }
        }
        else // on ground
        {
            if ("up".equals(Greenfoot.getKey()))// jump key detected
            
                {fallSpeed = -22; // add jump speed
                setLocation(getX(), getY()+fallSpeed); // leave ground
            
        }
    }
    
 }
  public void movement1()
    {  
    if(Greenfoot.isKeyDown("d") && getX()<1230 && !contact(Fighter.class))
    {setLocation(getX()+5,getY());
         }
    if(Greenfoot.isKeyDown("a") && getX()>50)
        {setLocation(getX()-5,getY());
      } 
      boolean onGround2 = (getY() == 388);
        if(!onGround2) // in middle of jump
        {
           
            fallSpeed2++; // adds gravity effect
            setLocation(getX(), getY()+fallSpeed2); // fall (rising slower or falling faster)
            if (getY()>=388 ) // has landed (reached ground level)
            {
                setLocation(getX(), 388); // set on ground
                Greenfoot.getKey(); // clears any key pressed during jump
           }
        }
        else // on ground
        {
            if ("w".equals(Greenfoot.getKey()))// jump key detected
            
                {fallSpeed2 = -22; // add jump speed
                setLocation(getX(), getY()+fallSpeed2); // leave ground
            
        }
    }
    
 }
  /**
     * Return true if we can see an object of class 'clss' right where we are. 
     * False if there is no such object here.
     */
    public boolean contact(Class clss)
    {
        Actor actor = getOneObjectAtOffset(5, 0, clss);
        return actor != null;        
    }
    
    
  public abstract void positions();
 
 
    /**
     * Act - do whatever the Fighter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
    }    
}
The code for one actor is
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Famouster here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class FamousDex extends Fighter
{
    /**
     * Act - do whatever the FamousDex wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        movement();
        //Bar("Famous Dex", "Health", 100, 100);
        positions();
        if(Greenfoot.mouseClicked(this))
        {Selector.charachterchoice(this);
        }
    }    
     public void positions()
     { 
        
         if(Greenfoot.isKeyDown("down"))
        {if ( contact(Fighter.class))
            {setImage("DexterExplode.png");     
           time = System.currentTimeMillis();
           isUp = true;
        }
            else{
            setImage("DexterAttack.png");     
           time = System.currentTimeMillis();
           isUp = true;
        }  }
      if(Greenfoot.isKeyDown("shift"))
        {setImage("DexterDefend.png");     
           time = System.currentTimeMillis();
           isUp = true;
        }  
        if(isUp){
        if((System.currentTimeMillis()-time) > 50)
            {setImage("DexterUntouched.png");
          
      }
     }
     
 }
}
How do I make it so both actors can move smoothly
danpost danpost

2018/4/19

#
zainsou wrote...
I'm making a two player game and the movement isn't working. When there is only one actor on the screen it works fine but when both are on at the same time some freeze or only one can move at a time. The code for the movement is << Code Omitted >> How do I make it so both actors can move smoothly
Only thing I see giving it a quick once over is the excessive use of getKey that will prevent one fighter from being able to jump.
You need to login to post a reply.