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

2021/4/1

Play an animation for a short time after touching another Actor

hernry2812 hernry2812

2021/4/1

#
Currently I have this animation:
public void hitAnimation()
    {
        if(count % 40 == 0)    
        {
            if(animationImage > 1)    
            {
                animationImage = 0;
            }
            setImage("polizeiautohit_" + animationImage + ".png");   
            animationImage++;   
        }
    }
I would like to play the animation for a specific time everytime it hits a specific actor. Hope someone has an idea :)
danpost danpost

2021/4/2

#
About how long should animation run? Is hit actor being removed? If not, will intersecting with hit actor be maintained longer than animation? Should animation repeat if still intersecting actor? Should animation complete for full run time even if intersecting is not continued? Need codes of calling method, as well.
hernry2812 hernry2812

2021/4/2

#
About how long should animation run? About 3 seconds, doesn't have to be exact. Is the car being removed after it hits the mole? (new) No it is not being removed. But the World should be set to EndScreen after it got hit 3 times (Every hit -> lifes--; ) Is hit actor being removed? The mole is getting automatically removed after it got hit. This takes about 2 seconds. If not, will intersecting with hit actor be maintained longer than animation? No, the hit actor (the actor my main actor Car hits) will explode if touched by my car and afterwards be removed (whole animation is about 2 seconds) Should animation repeat if still intersecting actor? No, but its fine if it does. Should animation complete for full run time even if intersecting is not continued? Yes, thats important. Should the hit animation override the boost animation? (new question) Yes Calling method in RaceWorld:
public class RaceWorld extends SWorld 
{  
    public RaceWorld()
    {    
        super(800, 800, 1, 1920, 1440); 
        setBackground(new GreenfootImage("World.png"));
        GreenfootImage bg = new GreenfootImage("World.png");
        setScrollingBackground(bg);

        setMainActor(new Car(), 1, 1);
        mainActor.setLocation(280, -80);

        setPaintOrder(Car.class, Mole.class);
        
    }
    public void act()
    {
        createMoles();
        super.act();
    }
There are already existing animations for the car, so heres some of the car class code:
public class Car extends SmoothMover
{
    boolean touchingMole;
    int animationImage = 1;
    int lifes = 3;
    int boost = 0;

    public void act() 
    {
        //The touching method for Mole is missing
       //The checkIfTouched method exists, but I don't know if its also suitable for this case
        setAnimation();

        lifes();

        count++;
    }

public void setSpeed()
    {
        if(checkIfSpaceIsPressed() == false)    
        {
            if (checkOnTrack() == true)     
            {
                setNormalSpeed();   
            }
            else
            {
                setNormalSpeedOnGrass();    
            }
        }
        else
        {
            boost();    
        }
    }

public void setAnimation()
    {
        if(checkIfSpaceIsPressed() == true && boost > 0)    
        {
            boostAnimation();   
        }
        else
        {
            carAnimation();     
        }
    }

public void carAnimation()
    {
        if(count % 40 == 0) 
        {
            if(animationImage > 1)  
            {
                animationImage = 0;
            }
            setImage("polizeiauto_" + animationImage + ".png"); 
            animationImage++; 
        }
    }

public void boostAnimation()
    {
        if(count % 40 == 0)     
        {
            if(animationImage > 1)     
            {
                animationImage = 0;
            }
            setImage("polizeiautoboost_" + animationImage + ".png");    
            animationImage++;   
        }
    }

public void hitAnimation()
    {
        if(count % 40 == 0)     
        {
            if(animationImage > 1)    
            {
                animationImage = 0;
            }
            setImage("polizeiautohit_" + animationImage + ".png");    
            animationImage++;   
        }
    }

public void lifes()
    {
        checkIfTouched();
   
        if (touchedMole == 1)
        {
            lifes = 2;      
        }
        if (touchedMole == 2)
        {
            lifes = 1;      /
        }
        if (touchedMole == 3)
        {
            //Play complete hit animation
            EndScreen endscreen = new EndScreen(); 
            Greenfoot.setWorld(endscreen);
        }
    }
    
    public void checkIfTouched()
    {
        if (touchingMole != isTouching(Mole.class))     
        {
            touchingMole = !touchingMole; 
            if (touchingMole)
            {
                touchedMole++;      
            }
        }
    }
Hope this spam of code isn't too confusing, I tried to only paste the "necessary" code. If you need to see any other methods, hit me up.
danpost danpost

2021/4/2

#
Okay. It will probably be best to have a String field to indicate what animation is currently active. It only needs to be one of three values -- "", "boost" or "hit". A method to set the animation might help as well. Two benefits of having the field are (1) you only need one method to run any of the animations; and (2) you can easily determine which animation is active. So, this is the gist of it (animation codes only)
String animationSet;

public Car()
{
    setAnimation("");
}

public void act()
{
    // the following must be placed first in act
    if ("hit".equals(animationSet))
    {
        runAmination();
        if (count == 0 && animationImage == 0) Greenfoot.setWorld(new EndScreen());
        return;
    }
    ...
    // last in act
    runAnimation();
}

private void setAnimation(String type)
{
    if (type.equals(animationSet)) return;
    animationSet = type;
    count = -1;
    animationImage = -1;
    runAnimation();
}

private void runAnimation()
{
    count = (count+1)%40;
    if (count == 0)
    {
        animationImage = (animationImage+1)%2;
        setImage(new GreenfootImage("polizeiauto"+animationSet+"_"+animationImage+".png"));
    }
}

// when hit 3 times (similar for "boost")
setAnimation("hit");
hernry2812 hernry2812

2021/4/2

#
Okay, thanks^^
You need to login to post a reply.