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

2016/11/29

Program slowing down from object

DukeFutzwillus DukeFutzwillus

2016/11/29

#
Whenever I have this object in the world it gradually slows the program until it is cleared and then it returns to normal. This is the code. Anyone idea
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class CavernTank here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class TankBoss extends SmoothMovement
{
    private GreenfootImage image1;
    private GreenfootImage image2;
    private GreenfootImage image3;
    int imgFrame = 0;   //This keeps track of which image frame it is currently showing
    private boolean swordeInRange = false;
    private int dmgDone;
    private boolean dashOn = false;
    private int dashCoolDown;
    private int tankDash = 50;
    private int dashCount;
    private int dashLength = 10;
    private int dashSound = 40;
    
     public TankBoss()
     {
         
         image1 = new GreenfootImage("Cavern Tank0.png");
         image2 = new GreenfootImage("Cavern Tank1.png");
         image3 = new GreenfootImage("Cavern Tank2.png");
     }
    public void act()  
    {  
        
        checkCollision();
        move();
        moveImage();
        findSworde();
        dashCoolDown++;
        death();
    }  
      
    
    public void death()
    {
        if (dmgDone == 200)
        {
            Greenfoot.playSound("explosion_x.wav");
            getWorld().removeObject(TankBoss.this);
        }
    }
    
    public void checkCollision()
    {
        Sworde sworde = (Sworde) getOneIntersectingObject(Sworde.class);
        if (sworde != null)
        {
            dmgDone += 10;
        }
    }
    
    public void moveImage() //This would be a loop going constantly to animate it  
    {  
        imgFrame++;
        
        if (imgFrame == 4)
        {
            imgFrame = 1;
            
        }
        if (imgFrame == 1)
        {
          setImage(image2);
          
        }
        
        if (imgFrame == 2)
        {
         setImage(image3);
          
        }
        
        if (imgFrame == 3)
        {
         setImage(image1);
         
        }
       
        
        }
        
        public void findSworde()
        {
            move(1);
        int checkTurn = Greenfoot.getRandomNumber(10);
        
        if (!getObjectsInRange(300, Sworde.class).isEmpty())
        {
            
            swordeInRange = true;
            dash();
        }
        else
        {
            swordeInRange = false;
        }
        
        if (swordeInRange)
        {
            Actor sworde = (Actor)getWorld().getObjects(Sworde.class).get(0);
            turnTowards(sworde.getX(), sworde.getY());
        }
        else
        if (checkTurn > 8)
        {
            turn(5);
        }
        if (checkTurn < 2)
        {
            turn(-5);
        }
    }
        
    public void dashLength()
    {
        if (dashCount <= dashLength)
        {
            move(10);
            dashCount++;
        }
        
        if (dashCount >= dashLength)
        {
            dashOn = false;
            
            dashCount = 0;
        }
    }
    
    public void dash()
    {
        
        if (dashOn)
        {
            dashLength();
            dashSound += 10;
        }      
        if (dashSound >= 40)
        {
            Greenfoot.playSound("tank.wav");
            dashSound = 0;
        }
        if (dashCoolDown >= tankDash)
        {
           
           dashCoolDown = 0; 
           DashTrail2 dashtrail = new DashTrail2 (getVelocity(), getRotation());
           getWorld().addObject (dashtrail, getX(), getY());
           dashOn = true;
           
        
    }
       
}
    }
why this is?
danpost danpost

2016/11/29

#
I see two possible issues here; but, I do not think they could be the cause of the slow down. The first is the use of 'TankBoss.this' at line 48; it should simply be 'this' (what you had there is how an inner class object would refer to it; I cannot say how the compiler interprets that from the object itself). The other is the changing of images every act cycle. Unless you have the speed slider down low, the images would be flickering by. Still, neither of these (I do not think) should cause a drastic slow down of the scenario. Out of curiosity, what code do you have in the Sworde and DashTrail2 classes?
DukeFutzwillus DukeFutzwillus

2016/11/29

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

/**
 * Write a description of class Sword here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Sworde extends SmoothMovement
{
    
    private int dashStop; // Used to make a 3 switch for stopping and starting the dash action
    private int dashCoolDown = 10; // USed to create a cool down period for using the dash action
    public int damageDone;
    public boolean dashVuln = false;
    public int dashLength = 10;
    private int dashCount = 0;
    private boolean dashOn = false;
    private boolean movedForward = false;
    private boolean movedReverse = false;
    private boolean moving = false; 
    private int moveCount; 
    private int swordeHealth = 100;
    public boolean onPlanet;
    private int dashEngine = 30;
    
    /**
     * Act - do whatever the Sword wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        onPlanet = MyWorld.onPlanet; 
        move();
        moveSword();
        dash();
        
        showHealth();
        checkCollision();
        
        warpTurn();
        dashCoolDown++;
        checkHealth();
    }    
    
    public void moveSword()
    {
       
        
        if (Greenfoot.isKeyDown("up"))
        {
            move(5);            
        }
        
        
        if (Greenfoot.isKeyDown("left"))
        {
            turn(-5);
            
        }
        
        if (Greenfoot.isKeyDown("right"))
        {
            turn(5);
        }
        
        if (Greenfoot.isKeyDown("down"))
        
        {
            move(-2);   
        }
        
        
    }
    
    public void death()
    {
        if (damageDone >= swordeHealth)
        {
            getWorld().showText("Hull Integrity:" + (swordeHealth - damageDone), 100, 25);
            Greenfoot.playSound("blowup.wav");
            getWorld().removeObject(Sworde.this);
            
        }
    }
    
    public void dashLength()
    {
        if (dashCount <= dashLength)
        {
            move(10);
            dashCount++;
        }
        
        if (dashCount >= dashLength)
        {
            dashOn = false;
            dashVuln = false; 
            dashCount = 0;
        }
    }
        
    public void showHealth()
    {
        
        
            getWorld().showText("Hull Integrity:" + (swordeHealth - damageDone), 100, 25);
        
       
    }
    
    
    public void dash()
    {
        
        if (dashOn)
        {
            dashLength();
        }      
        
        if (dashCoolDown >= dashEngine)
        {
            setImage("The Sword, energized.png");
            
        if (Greenfoot.isKeyDown("z"))
        {
           
           dashVuln = true;
           dashCoolDown = 0; 
           setImage("The Sword.png");
           
           DashTrail2 dashtrail = new DashTrail2 (getVelocity(), getRotation());
           getWorld().addObject (dashtrail, getX(), getY());
           dashOn = true;
           
          
           
           
           
        }
        
    }
       
   
        
        
    }
    
    public void checkHealth()
    {
        
            if (damageDone >= 100)
                {
                    death();
                    Greenfoot.playSound("game_over.wav");
                }
        
    }
    
     public void checkCollision()
    {
        Bug1 bug1 = (Bug1) getOneIntersectingObject(Bug1.class);
        
        if (bug1 != null)
        {
            if (! dashVuln)
                {
                    damageDone += 25;
                }
        }
        
        TankBoss tank = (TankBoss) getOneIntersectingObject(TankBoss.class);
        
        if (tank != null)
        {
            if (! dashVuln)
            {
                damageDone += 50;
            }
        }
        
        
        BodyUpgrade body = (BodyUpgrade) getOneIntersectingObject(BodyUpgrade.class);
        
        if (body != null)
        {
            swordeHealth += 50;
            
        }
        
        PropUpgrade prop = (PropUpgrade) getOneIntersectingObject(PropUpgrade.class);
        
        if (prop != null)
        {
            
            dashLength += 4 ;
            
        }
        
        CoreUpgrade core = (CoreUpgrade) getOneIntersectingObject(CoreUpgrade.class);
        
        if(core != null)
        {
            
            dashEngine -= 5;
        }
    }
    
    
    public void warpTurn()
    {
        PortaltoPlanet port = (PortaltoPlanet) getOneIntersectingObject(PortaltoPlanet.class);
        
        if (port != null)
        {
            turn(180);
        }
    }
   
    
    
   
        
    
    
    
}
DukeFutzwillus DukeFutzwillus

2016/11/29

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

/**
 * Write a description of class DashTrail2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class DashTrail2 extends SmoothMovement
{
    /**
     * Act - do whatever the DashTrail2 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.
    }    
    
    public DashTrail2(Vector speed, int Rotation)
    {
       setRotation(Rotation); 
    }
}
DukeFutzwillus DukeFutzwillus

2016/11/29

#
I think it is the image cycling. Is there a better way to do that, so that it won't slow the program?
danpost danpost

2016/11/29

#
DukeFutzwillus wrote...
I think it is the image cycling. Is there a better way to do that, so that it won't slow the program?
Before having the images change with less frequency, comment out line 36 to see if it is actually the animation which is slowing it down.
You need to login to post a reply.