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

2021/2/4

Code is freezing while an actor is doing its job

XApple15 XApple15

2021/2/4

#
I have a multiplayer game , both players are playing from the same keyboard. The problem is, whenever a player reaches a Task and is working on it, both players are Freezing ( like they dont move on key press, or any moving object in the world , but the task is doing it`s job )
import greenfoot.*;  
import java.util.List;
/**
 * Write a description of class Job1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Job1 extends Jobs
{
    boolean FinishedCutting = false;
    long LastTime, CurrentTime;
    private int i, j, bar_number;
    private int CutDelay = 50;
    
    
    public void act() 
    {
        
        if( Greenfoot.isKeyDown("c")) 
          {  
              Cutting();
          }

    }    

    public  void Cutting() 
    {
        for ( bar_number = 1 ; bar_number <=4 ; bar_number++ ) 
        {
            for( i=1 ; i<=10;i++) 
            {
                setImage("cuts/cut" + bar_number + "_"  + i + ".png" );
                if( Greenfoot.isKeyDown("c") == false ) 
                { getWorld().removeObject(this); return;}
                LastTime = System.currentTimeMillis();
                while ( LastTime + CutDelay >= System.currentTimeMillis() ) {} 
                getWorld().repaint();
            }
            for(i=9 ; i>=1 ;i-- ) 
            {
                setImage("cuts/cut" + bar_number + "_"  + i + ".png" );
                if( Greenfoot.isKeyDown("c") == false )
                {  getWorld().removeObject(this); return;}
                LastTime = System.currentTimeMillis();
                while ( LastTime + CutDelay >= System.currentTimeMillis() ) {}
                getWorld().repaint();
            }
        }
        setImage("cuts/cut_final.png"); 
        getWorld().repaint();
        
        goToNextPoint();   
    }

    private void goToNextPoint() 
    {
        getWorld().removeObjects( getWorld().getObjects(Vent1.class));
        getWorld().removeObjects( getWorld().getObjects(Players.class));
        getWorld().removeObjects( getWorld().getObjects(Vents.class));
        getWorld().addObject( new Vent2(), 595,612);
        getWorld().addObject( new Vent3(), 400,400);
        getWorld().addObject( new Player1(), 1150,90);
        getWorld().addObject( new Player2(), 1150, 135);
        getWorld().addObject( new WalkieTalkie(), 612, 499);
        getWorld().removeObject(this);
    }

}
RcCookie RcCookie

2021/2/4

#
This is actually exactly what we talked about in the other thread. Take a look at my alternative solution
danpost danpost

2021/2/5

#
RcCookie wrote...
This is actually exactly what we talked about in the other thread. Take a look at my alternative solution
I had mentioned the use of while in the other (or another) thread. Avoid it if other actions are to be taking place at that time.
RcCookie RcCookie

2021/2/5

#
So basically what is happening is that it is running your code all at once. Nothing else can run in between, so it seems to lag. But all that lag just comes from your while loop repeating until the desired delay is achived.
RcCookie RcCookie

2021/2/5

#
Look at this
XApple15 XApple15

2021/2/5

#
RcCookie wrote...
Look at this
Thanks ! That worked !
You need to login to post a reply.