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

2012/6/22

Well this lagged quite a bit

darkmist255 darkmist255

2012/6/22

#
I'm working on writing a simple particle generator, starting with one that just dims as it goes outward. This is my first simple attempt at it but a single act() cycle takes a full second. I'm assuming it doesn't like a for() within a for()?
        for(int xp = 0; xp < canvasImg.getWidth(); xp++)
        {
            for(int yp = 0; yp < canvasImg.getHeight(); yp++)
            {
                int alphaRef = (int)abs(Math.hypot(centerX - xp, centerY - yp));
                int alpha = 255 - alphaRef;
                canvasImg.setColorAt(0, 0, new Color(0, 0, 0, alpha));
            }
        }
The only way I can think of right now to do this would be using a for() within a for(), any suggestions on what direction I should take to continue? I seem to be unable to find any articles online for how to go about coding a particle generator, but I'll keep looking.
kiarocks kiarocks

2012/6/22

#
you could do a loop every act cycle
//Put with other vars
boolean onX, onY;
int yp;
int xp;

//in act cycle (as the last thing
if(onX)
{
        if(xp < canvasImg.getWidth())
{
onY = true;
onX = false;
return;
}
        xp++;
        int alphaRef = (int)abs(Math.hypot(centerX - xp, centerY - yp));  
        int alpha = 255 - alphaRef;  
        canvasImg.setColorAt(0, 0, new Color(0, 0, 0, alpha));  
}
else if(onY)
{
if(yp < canvasImg.getHeight())
{
onY = false;
return;
}
yp++;
int alphaRef = (int)abs(Math.hypot(centerX - xp, centerY - yp));  
        int alpha = 255 - alphaRef;  
        canvasImg.setColorAt(0, 0, new Color(0, 0, 0, alpha));  
}
nccb nccb

2012/6/23

#
darkmist255: the code looks fine -- I don't know why it would take so long, unless X or Y are huge. Maybe you can post more of your code, in case the slowness is elsewhere?
darkmist255 darkmist255

2012/6/24

#
Well then... I tried the code again today and the lag is significantly less than it was yesterday even though I haven't changed anything o.O. The image is just 40x40 so it shouldn't be overwhelming, and the CPU usage only jumps maybe an extra 5-10% now. It should be fine for now :D. Maybe my antivirus freaked out? Shouldn't have lasted all day, but who knows.
You need to login to post a reply.