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

2016/10/27

Freezing and lagging in this scenario

JonnyB7 JonnyB7

2016/10/27

#
After I fire bullets in this scenario it begins to freeze and lag. It seems to happen when I fire bullets quickly or hold down the fire button. But, sometimes it will happen from the first bullet. I've tried increasing the reload time to give the scenario more time to process but have had no success. I'm getting no error messages or anything. If I reset the scenario it will lag right from the beginning. If I close and reopen it will work until I try to fire again. Code for firing actor
public class Robo extends Actor
{
    private int smileyPoints;
    private int gunReloadTime;              // The minimum delay between firing the gun.
    private int reloadDelayCount;
    
    /**
     * Initialise this Robo.
     */
    public Robo()
    {
        gunReloadTime = 20;
        reloadDelayCount = 0;
    }
    
    /**
     * Act - do whatever the Robo wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        //move(5);
        lookForWorm();
        checkDirectionKey();
        checkKeyPress();
        reloadDelayCount++;
        
    }    
    //Action for when Robo finds Smiley
    public void lookForWorm()
    {
        if ( isTouching (Smiley.class) )
        {
            removeTouching (Smiley.class);
            Greenfoot.playSound("hooray.wav");
            smileyPoints = smileyPoints + 1;
            if ( smileyPoints == 5)
            {
                Greenfoot.stop();
                Greenfoot.playSound("fanfare.wav");
            }
        }
    }

    //Check if directional key is pressed, if so react
    public void checkDirectionKey()
    //Turn Robo left
    {
        if (Greenfoot.isKeyDown("left"))
        {
            turn(-4);
        }
        //Turn Crab Right
        if (Greenfoot.isKeyDown("Right"))
        {
            turn(4);
        }
        //Move Robo up
        if (Greenfoot.isKeyDown("up"))
        {
            move(5);
        }
        //Move Robo down
        if (Greenfoot.isKeyDown("down"))
        {
            move(-5);
        }
    }

    //Check if key is press, if so then react
    public void checkKeyPress()
    {
        //move up
        if (Greenfoot.isKeyDown("W"))
        {
            setLocation(getX(), getY()-4);
        }
        //move down
        if (Greenfoot.isKeyDown("S"))
        {
            setLocation(getX(), getY()+4);
        }
        //move left
        if (Greenfoot.isKeyDown("A"))
        {
            move(-4);
        }
        //move right
        if (Greenfoot.isKeyDown("D"))
        {
            move(4);
        }
        //Create bullet at 100, 100
        if (Greenfoot.isKeyDown("L"))
        {
           fire();  
        }
    }
    //fire bullet if gun is ready
    private void fire()
    {
        if ( reloadDelayCount >= gunReloadTime )
        {
            getWorld().addObject( new Bullet(), getX(), getY());
            reloadDelayCount = 0; //time since last shot fired
        }
    }
}
Code for bullet
/*Bullet will disappear when 0 life*/
    private int life = 30;
    
    /*Move bullet in direction it was created*/
    public int getRotation;

    
    public void act() 
    {
        move();
        hitSpider();
        //noLife();
        ifTouchingEdge();
    }   
    
    public void move()
    {
        move(2);
        
        /*if  ( getRotation(Bullet.class) = 0)
        {
            move(2);
        }**/
        
        
    }
    
    /*  public void noLife()
    {
        if (life <= 0) 
        {
            getWorld().removeObject(this);
        } 
    }  */ 

    //Action for when bullet hits spider
    public void hitSpider()
    {
        if ( isTouching (Grunt.class) )
        {
            removeTouching (Grunt.class);
            Greenfoot.playSound("Squish.wav");
        }
    }
    
    //When bullet reaches right edge, disappear
    public void ifTouchingEdge()
    {
        if ( getX() == 599)
        {
            //life = 0;
            getWorld().removeObject(this);
        }
    }
}
danpost danpost

2016/10/27

#
JonnyB7 wrote...
After I fire bullets in this scenario it begins to freeze and lag. It seems to happen when I fire bullets quickly or hold down the fire button. But, sometimes it will happen from the first bullet.
I do not see any issues in these classes. Are you sure it is not in your World subclass? Please post it for review.
JonnyB7 JonnyB7

2016/10/27

#
World Sub class
public class Spidertron extends World
{

    /**
     * Constructor for objects of class Spidertron.
     * 
     */
    public Spidertron()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 600, 1); 
        setBackground("grass.png");
        prepare();
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {

        Robo robo = new Robo();
        addObject(robo,302,301);
        Smiley smiley = new Smiley();
        addObject(smiley,405,69);
        Smiley smiley2 = new Smiley();
        addObject(smiley2,185,44);
        Smiley smiley3 = new Smiley();
        addObject(smiley3,202,204);
        Smiley smiley4 = new Smiley();
        addObject(smiley4,114,498);
        Smiley smiley5 = new Smiley();
        addObject(smiley5,466,537);
        Smiley smiley6 = new Smiley();
        addObject(smiley6,511,201);
        Smiley smiley7 = new Smiley();
        addObject(smiley7,268,567);
        Grunt grunt = new Grunt();
        addObject(grunt,484,76);
        Grunt grunt2 = new Grunt();
        addObject(grunt2,245,61);
        Grunt grunt3 = new Grunt();
        addObject(grunt3,77,317);
        Grunt grunt4 = new Grunt();
        addObject(grunt4,349,544);
        Grunt grunt5 = new Grunt();
        addObject(grunt5,533,371);
        Grunt grunt6 = new Grunt();
        addObject(grunt6,72,74);
    }
}
danpost danpost

2016/10/27

#
JonnyB7 wrote...
World Sub class
I do not see anything there, either. Let's try the Grunt and Smiley classes. Oh, and if there is any more to the Bullet class, that too.
JonnyB7 JonnyB7

2016/10/27

#
I have no code in the Smiley class Grunt class
{
    /**
     * Act - do whatever the Grunt wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(1);
        lookForRobo();
        lookForSmiley();
        randomTurn();
        turnAtEdge();
    }    

    //Check if we have stumbled upon a Robo.
    //If we have, eat it. If not, do nothing.
    public void lookForRobo()
    {
        if ( isTouching (Robo.class) )
        {
            removeTouching (Robo.class);
            Greenfoot.stop();
            Greenfoot.playSound("Scaryscream.wav");
        }
    }

    //Check if we have stumbled upon a Smiley.
    //If we have, eat it. If not, do nothing.
    public void lookForSmiley()
    {
        if ( isTouching (Smiley.class) )
        {
            removeTouching (Smiley.class);
            Greenfoot.playSound("sad-trombone.wav");
        }
    }

    //Turns lobsters randomly at 10% chance.
    public void randomTurn()
    {
        if ( Greenfoot.getRandomNumber(100) < 10 )
        {
            turn ( Greenfoot.getRandomNumber(90)-45 );
        }
    }

    //Turn Grunt when they reach EOS
    public void turnAtEdge()
    {
        if ( isAtEdge() )
        {
            turn (17);
        }
    }
I have some additions to bullet class I'm in the process of trying to shoot different directions. Full current Bullet class
{
    /**
     * Act - do whatever the Bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */

    /*Bullet will disappear when 0 life*/
    private int life = 30;
    
    /*Move bullet in direction it was created*/
    public int getRotation;

    
    public void act() 
    {
        move();
        hitSpider();
        //noLife();
        ifTouchingEdge();
        setDirection();
    }   
    
    public void move()
    {
        move(2);
        
        /*if  ( getRotation(Bullet.class) = 0)
        {
            move(2);
        }**/
        
        
    }
    
    /*  public void noLife()
    {
        if (life <= 0) 
        {
            getWorld().removeObject(this);
        } 
    }  */ 

    //Action for when bullet hits spider
    public void hitSpider()
    {
        if ( isTouching (Grunt.class) )
        {
            removeTouching (Grunt.class);
            Greenfoot.playSound("Squish.wav");
        }
    }
    
    //When bullet reaches right edge, disappear
    public void ifTouchingEdge()
    {
        if ( getX() == 599)
        {
            //life = 0;
            getWorld().removeObject(this);
        }
    }
    
    public void setDirection()
        {
            if ( getRotation() == 0)
            {
                
            }
        }
danpost danpost

2016/10/27

#
To tell you the truth, I do not see anything within the code that might cause a significant amount of lagging or freezing of the scenario. Try commenting out different 'Greenfoot.playSound' lines in the code. Maybe one or more of them are contributing to that behavior. Also, you may try creating GreenfootSound objects of them and place them in instance fields so that they are already loaded and ready to play when needed. For example, in the Bullet class, line 49 plays the "Squish.wav" file. You could do this:
// add instance field
private GreenfootSound squish = new GreenfootSound("Squish.wav");

// and change line 49 to
squish.play();
JonnyB7 JonnyB7

2016/10/31

#
No luck commenting out the sound lines. I am on a Mac I haven't had a chance of testing the scenario on a windows machine yet, but is there any reports of issues with the new apple updates and greenfoot compatibility?
danpost danpost

2016/10/31

#
JonnyB7 wrote...
is there any reports of issues with the new apple updates and greenfoot compatibility?
That might be a question that a member of the greenfoot team could answer. For one, I am not a Mac user and do not pay much attention to Mac issues; for another, issues of that type are usually sent to support at greenfoot and they are not accessible to the general public. I copied your classes into a new scenario (on a Windows system) and commented out the sound playing lines. It seemed to run without lagging. Key control did seem a bit funky, however. This may be due to the way it was coded. You have the 'checkKeyPress' method, which has code for the movement of an actor that does not rotate; but, then you have the 'checkDirectionKey' method, which has movement code for a rotating actor. Use one or the other; but not both. Also, in the 'checkDirectionKey' method, you have "Right" as one of the keys; it should be "right", starting with a lowercase letter.
JonnyB7 JonnyB7

2016/11/1

#
Thanks for the advice on both topics. Yeah key control got messed up because I was trying to add directional shooting for the bullets then I forgot to take it out once I realized that was not the way to do it. Thanks, Let you know if I make any progress.
You need to login to post a reply.