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

2020/12/25

Animate a png

Roshan123 Roshan123

2020/12/25

#
I want to animate a png but i m not able to do that. I want to check how close the class is for every fps i.e. the crate animation should increase vigorously if any class is getting closer to it....
//globally
byte count;
//act method
count++
if(count<11 && count>0)
setImage(crate1.png);
else if(!getObjectsInRange(80,Actor.class).isEmpty() && count >11 && count<22)
setImage(crate2.png);
else(count>22)
count=0;
danpost danpost

2020/12/25

#
Roshan123 wrote...
I want to animate a png but i m not able to do that. I want to check how close the class is for every fps i.e. the crate animation should increase vigorously if any class is getting closer to it... << Code Omitted >>
// global
GreenfootImage[] images =
{
    new GreenfootImage("crate1.png"),
    new GreenfootImage("crate2,png")
};
int count;
int imgNum;

// act
int dist = 400;
for (Object obj : getObjectsInRange(400, Actor.class))
{
    Actor actor = (Actor)obj;
    dist = Math.min(dist, (int)Math.hypot(actor.getY()-getY(), actor.getX()-getX()));
}
count++;
if (count >= dist/16)
{
    imgNum = (++imgNum)%2;
    setImage(images[imgNum]);
    count = 0;
}
Roshan123 Roshan123

2020/12/25

#
Does it take time for you to think about the syntax or its only about the concept....wether its clear or not
Roshan123 Roshan123

2020/12/25

#
Thanks for helping me out..... _/\_
danpost danpost

2020/12/25

#
Roshan123 wrote...
Does it take time for you to think about the syntax or its only about the concept....wether its clear or not
"syntax" is strictly writing what you want in such a way as the compiler accepts it. It is the algorithm that creates the concept. It took me a minute to realize that an imgNum field would be used here, realizing that the count would need to vary in its max value (meaning it could not be dual-purposed in controlling which image was to show).
You need to login to post a reply.