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

2019/6/22

enemy movement animation

AniSusi AniSusi

2019/6/22

#
I really need help. My enemy can move from left to right, however the animation and the direction of view doesn't work.
private void move()
    {
        if(timer < 75)
        {
            blick=0;
            setLocation(getX() + speed,getY());
        }
        else if(timer > 75)
        {
            blick=1;
            speed = -speed;
            getImage().mirrorHorizontally();
            timer = 0;
        }
        if(blick == 0)
        {
            animateLeft();
        }
        if(blick == 1)
        {
            animateRight();
        }
    }
AniSusi AniSusi

2019/6/22

#
I also tried this method
if (getObjectsInRange(100,player.class).isEmpty())  
        {
            if (steps == 0)
            {
                blick=0;
                speed = -speed;
            }
            blick=1;
            move(speed);
            if(blick == 0)
            {
                animateLeft();
            }
            if(blick == 1)
            {
                animateRight();
            }
        }
Super_Hippo Super_Hippo

2019/6/22

#
If the animation doesn't work, you should probably show the "animateRight" and "...Left" methods. In the second code, "blick" is set to 1 in line 8, so it will never be 0 in line 10. You also never increase the timer anywhere.
AniSusi AniSusi

2019/6/22

#
blick isnt supposed to be a timer, but the direction the enemy is looking at. I still changed it so the " blick 1" is called in first. heres the code for the animation
public void animateLeft()
    {
        if(frame == 10)
        {
            setImage(enemy11);
        }
        else if(frame == 20)
        {
            setImage(enemy12);
            frame = 1;
            return;
        }
        frame++;
    }
Super_Hippo Super_Hippo

2019/6/22

#
Yeah, I meant the "timer" in the first code. And you use "steps" in the second one and never change it either.
AniSusi AniSusi

2019/6/22

#
...sorry for being a morron but im not sure if i can follow you
Super_Hippo Super_Hippo

2019/6/22

#
Show the act method of the class.
AniSusi AniSusi

2019/6/22

#
private int blick;
public int steps;
private int frame=1;
int timer = 0;
    public enemy1()
    {
        speed1=0;
    }
    public void act() 
    {
        fall();
        counter++;
        timer++;
        steps = (steps + 1) % 75;
        if(Greenfoot.isKeyDown("d"))
        {
            move(-5);
        }
        if(Greenfoot.isKeyDown("a"))
        {
            move(5);
        }
        if(health == 0)
        {
            if (getWorld().getObjects(explosion.class).isEmpty())
            {
                getWorld().addObject(new explosion(),getX(),getY());
            }
            World world = getWorld();
            level1 myWorld = (level1)world;
            counter counter = myWorld.getCounter();
            counter.addScore();
            animateDeath();
            return;
        }
        move();
        shoot();
        hit();
    }
Super_Hippo Super_Hippo

2019/6/22

#
Hm, I am a bit lost I guess. Try moving all the "timer++" and similar code into the method in which they are used. This shouldn't change anything, but it makes it easier to understand what you are doing. It is a bit messy because there are so many variables.
danpost danpost

2019/6/23

#
I suggest using just one act counter for the movement/animation. The following is a close example:
public void move()
{
    timer = (timer+1)%160;
    setLocation(getX()+1-2*(timer/80), getY());
    if (timer%10 == 0) setImage(timer/80 == 1 ? (timer%20 == 0 ? enemy11 : enemy12) : (timer%20 == 0 ? enemy01 : enemy02));
}
AniSusi AniSusi

2019/6/23

#
i probably did something wrong but this isnt really changing much. except that the enemies start gliding away from the player
danpost danpost

2019/6/23

#
AniSusi wrote...
i probably did something wrong but this isnt really changing much. except that the enemies start gliding away from the player
Wait ... it is quite strange to see key controls in the class of an enemy. Is this really want you are wanting?
AniSusi AniSusi

2019/6/23

#
mhh... the keys are supposed to be for scrolling world. it does seem to not make sense tho. I'll remove them
You need to login to post a reply.