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

2014/10/22

Enemy glitching

A1234 A1234

2014/10/22

#
I am creating a platform game in school and I have almost everything working except for my enemy. I have watched tutorials on how to get the enemy to run back and forth on he platform but for some reason, when I place my enemy onto the platform, it runs really really slowly to one side of the platform, and then runs the other way but at the same time, it shifts from facing left to right and plays the animation really fast. It has nothing to do with the speed at the moment, something else is wrong as the images of the enemy keep changing directions. I'm not sure what is wrong with my code. Here is the enemy's code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Monster here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Monster extends Actor
{
    private int vSpeed = 0;
    private int acceleration = 2;

    private int spriteHeight = getImage().getHeight();
    private int spriteWidth = getImage().getWidth();
    private int lookForGroundDistance = (int)spriteHeight/2;
    private int lookForEdge = (int)spriteWidth/2;

    private int speed = 1;

    int frame = 0; //keeps track of image shown
    private int animationCounter = 0;

    private GreenfootImage monster = new GreenfootImage("Monster.png");
    private GreenfootImage monster2 = new GreenfootImage("Monster2.png");
    private GreenfootImage monster3 = new GreenfootImage("Monster3.png");
    private GreenfootImage monster4 = new GreenfootImage("Monster4.png");
    private GreenfootImage monster5 = new GreenfootImage("Monster5.png");
    private GreenfootImage monster6 = new GreenfootImage("Monster6.png");
    private GreenfootImage monster7 = new GreenfootImage("Monster7.png");

    private GreenfootImage monsterl = new GreenfootImage("Monsterl.png");
    private GreenfootImage monster2l = new GreenfootImage("Monster2l.png");
    private GreenfootImage monster3l = new GreenfootImage("Monster3l.png");
    private GreenfootImage monster4l = new GreenfootImage("Monster4l.png");
    private GreenfootImage monster5l = new GreenfootImage("Monster5l.png");
    private GreenfootImage monster6l = new GreenfootImage("Monster6l.png");
    private GreenfootImage monster7l = new GreenfootImage("Monster7l.png");

    /**
     * Act - do whatever the Monster wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkFall();
        move();

        if(speed<0)
        {
            if(animationCounter % 4 == 0)
            {
                animateLeft();
            }
        }
        else
        {
            if(animationCounter % 4 == 0)
            {
                animateRight();
            }
        }

        animationCounter ++; //count up by one*/
    }    

    public void move()
    {
        Actor ground = getOneObjectAtOffset(lookForEdge, lookForGroundDistance, platform.class);

        if(ground == null)
        {
            speed *= -1;
            lookForEdge *= -1;
        }
        else
        {
            move(speed);
        }
    }

    public void animateLeft()
    {
        if(frame == 0)
        {
            setImage(monsterl);
        }
        else if(frame == 1)
        {
            setImage(monster2l);
        }
        else if(frame == 2)
        {
            setImage(monster3l);
        }
        else if(frame == 3)
        {
            setImage(monster4l);
        }
        else if(frame == 4)
        {
            setImage(monster5l);
        }
        else if(frame == 5)
        {
            setImage(monster6l);
        }
        else if(frame == 6)
        {
            setImage(monster7l);
            frame = 0;
        }

        frame ++; //add one frame to itself
    }

    public void animateRight()
    {
        if(frame == 0)
        {
            setImage(monster);
        }
        else if(frame == 1)
        {
            setImage(monster2);
        }
        else if(frame == 2)
        {
            setImage(monster3);
        }
        else if(frame == 3)
        {
            setImage(monster4);
        }
        else if(frame == 4)
        {
            setImage(monster5);
        }
        else if(frame == 5)
        {
            setImage(monster6);
        }
        else if(frame == 6)
        {
            setImage(monster7);
            frame = 0;
        }

        frame ++; //add one frame to itself
    }

    public void fall()
    {
        setLocation(getX(), getY() + vSpeed); 
        if(vSpeed <=9)
        {
            vSpeed = vSpeed + acceleration; //falls down 2 pixels per cycle
        }
    }

    public boolean onGround()
    {
        int spriteHeight = getImage().getHeight();
        int yDistance = (int) (spriteHeight/2) + 5; //look for ground 5 pixels below character

        Actor ground = getOneObjectAtOffset(0, getImage().getHeight()/2, platform.class);

        if(ground == null)
        {
            return false;
        }
        else
        {
            moveToGround(ground);
            return true;
        }
    }

    public void moveToGround(Actor ground)
    {
        int groundHeight = ground.getImage().getHeight();
        int newY = ground.getY() - (groundHeight + getImage().getHeight())/2;

        setLocation(getX(), newY);
    }

    public void checkFall()
    {
        if(onGround())
        {  
            vSpeed =0; //If the player is on the ground(platform), vertical speed will stop
        }
        else
        {
            fall();
        }
    }
}
danpost danpost

2014/10/22

#
Is your enemy on a moving platform? if so, does your platform move any objects that may be on it with it? Is the speed slider for the scenario set around the middle of the scale? For the switching of left and right, try adding one to the lookForGroundDistance at line 69 above.
A1234 A1234

2014/10/22

#
Nope, the platform is stationary. There is one object on it, which is a door. However, the door does not move, nor do any characters move it. The speed slider is right in the middle. I already made sure of that because that was what I initially thought was wrong. And how would I code that? Sorry, my head isn't working too well right now. I'm overtired. Thank you
danpost danpost

2014/10/22

#
(1) true or false, the platforms do not move AND your character move faster in one direction than in the other (horizontally); (2) yes or no, your scenario is at minimum a side-scroller; (if yes, this information was not given and shame on you); (3) if a scroller, explain, how are you controlling the scrolling (mainly with determining how much to scroll and what you do with any actors during scrolling).
A1234 A1234

2014/10/22

#
No, the platforms do not move at all. The character moves at the same speed left and right. And no, the scenario is not a side scroller
danpost danpost

2014/10/23

#
Try using the following for line 69 above:
Actor ground = getOneObjectAtOffset(lookForEdge, lookForGroundDistance+1, platform.class);
Report back any changes, if any.
A1234 A1234

2014/10/23

#
It is now working fine! Thank you so much!
danpost danpost

2014/10/23

#
Was it this last suggestion that fixed it? I actually cannot believe that would be the case! I went as far as taking your Monster class code and tested it. I did not find any behavior that would seem to be unwanted.
A1234 A1234

2014/10/23

#
Yeah, that seemed to fix it and now it is working completely fine. It is strange how that fixed it as before it was glitching like crazy! But thank you again :)
You need to login to post a reply.