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

2020/9/14

Lag

1
2
danpost danpost

2020/9/15

#
Roshan123 wrote...
<< Code Omitted >> do u mean this
Depends where each line is in your class. Line 1 should be a global variable (not within a method or constructor). Lines 2 and 3 should be in a constructor. The rest should be in the act method.
Roshan123 Roshan123

2020/9/15

#
now is it ok or not???
import greenfoot.*; 
public class BSheild extends Actor
{
    B b;
    int timer,frame,r,g,b1;
    public  BSheild(B mainB)
    {
      b= mainB;
      setImage(new GreenfootImage(127,127));
      getImage().drawOval(0,0,128,128);
      
    }
    public void act() 
    {
    if(++frame % 120==0)
    {
      Color mainColor=new Color( r, g, b1);
      r=Greenfoot.getRandomNumber(255)+1;
      g=Greenfoot.getRandomNumber(255)+1;
      b1=Greenfoot.getRandomNumber(255)+1;
      getImage().setColor(mainColor);
      getImage().fillOval(1,1,100-50,100-50); 
      getWorld().showText("frame"+frame+"\n maincolor"+mainColor, 400, 300);
      frame=0;
    }
     
     move(10);
     moveAround();
     }
    public void moveAround()
    {
     if(b!=null)
     {
       turnTowards(b.getX(),b.getY());
     }
     if(isTouching(B.class) && ++timer>610 ||isTouching(Sheild.class))
        {
         removeTouching(BSheild.class);   
         removeTouching(Sheild.class); 
         getWorld().removeObject(this);  
           
        }
    }
}
danpost danpost

2020/9/15

#
Roshan123 wrote...
now is it ok or not??? << Code Omitted >>
Looks good. However, that would be a bit slow on the changing. The 120 would make it like 2 seconds per change. Its a far cry from what you originally had. I would think something between 4 and 10 would suffice.
Roshan123 Roshan123

2020/9/15

#
i did what u suggested me i totally removed smoke.class and i wrote frame as 10 but it still lags when shield is to be added to the world
Roshan123 Roshan123

2020/9/15

#
i think u have the code and u should try it if u dont understand my words
Roshan123 Roshan123

2020/9/16

#
hey danpost, thanks a lot for the helping and suggesting me now it's not lagging anymore i think the problem was with the draw Oval, fill oval, setColor(rand color) when i changed it to a static image of the shield then it was not lagging anymore
You need to login to post a reply.
1
2