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

2011/12/12

Strange behavior, what am I missing?

darkmist255 darkmist255

2011/12/12

#
I'm working on an enemy called a "Charger". The idea is that once the player gets within 250 pixels of it, it will charge in a straight direction for 240 pixels, not altering its path until it is finished charging. HOWEVER. I have noted the following behaviour: Either a. The charger does absolutely nothing. or b. The charger moves in a vertical line for a second, then stops. It never moves again. The idea is a little bit ahead of my coding abilities, so I'm not entirely sure of what I'm doing while I write this. Here is the class (it's a fair bit of code, bear with me):
public class Charger extends Enemy
{
    Room room;
    Stats stats;
    Character character;
    
    String collisionSide;
    
    double realXloc, realYloc;
    double health = 10;
    
    int chargeRemaining = 0;
    int recharge = 0;
    
    double moveSpeed = 4;
    double yMomentum, xMomentum, xDistance, yDistance = 0;
    
    private String chargePlayer = "false";

    public void act() 
    {
        damageTimer();
        checkCollision();
        chargeAtPlayer();
        moveMe();
        //findPlayer();
    }
    
    public void damageTimer()
    {
        if(recharge > 0)
        {
            recharge = (recharge - 1);
        }
    }
    
    public void addedToWorld(World world)
    {
        room = (Room)getWorld();
        stats = room.stats;
        character = room.character;
        realXloc = getX();
        realYloc = getY();
    }
    
    public void checkCollision()
    {
        collisionSide = detectCollisionReturnSide((int)moveSpeed, (int)xMomentum, (int)yMomentum);
        
        if("Right".equals(collisionSide) || "Left".equals(collisionSide))
        {
            //determine up or down
            if(yDistance > 0) {
            yMomentum = moveSpeed; }
            else {
            yMomentum = -moveSpeed; }
            xMomentum = 0;
        }
        
        if("Up".equals(collisionSide) || "Down".equals(collisionSide))
        {
            //determine right or left
            if(xDistance > 0) {
            xMomentum = moveSpeed; }
            else {
            xMomentum = -moveSpeed; }
            yMomentum = 0;
        }
    }
    
    public void findPlayer()
    {
        xDistance = (character.getX() - getX());
        yDistance = (character.getY() - getY());

        if(Math.abs(xDistance) > Math.abs(yDistance) && Math.abs(xDistance) < 250)
        {
            chargeRemaining = 60;
            chargePlayer = "x";
        }
        else if(Math.abs(yDistance) > Math.abs(xDistance) && Math.abs(xDistance) < 250)
        {
            chargeRemaining = 60;
            chargePlayer = "y";
        }
        else
        {
            chargePlayer = "false";
        }
    }
    
    public void chargeAtPlayer()
    {
        if("x".equals(chargePlayer))
        {
            if(xDistance > 0) {
            xMomentum = moveSpeed; }
            else {
            xMomentum = -moveSpeed; }
            yMomentum = 0;
            
            chargeRemaining = (chargeRemaining - 1);
            if(chargeRemaining <= 0)
            {
                chargePlayer = "false";
            }
        }
        if("x".equals(chargePlayer))
        {
            if(yDistance > 0) {
            yMomentum = moveSpeed; }
            else {
            yMomentum = -moveSpeed; }
            xMomentum = 0;
            
            chargeRemaining = (chargeRemaining - 1);
            if(chargeRemaining <= 0)
            {
                chargePlayer = "false";
            }
        }
        if("false".equals(chargePlayer))
        {
            yMomentum = 0;
            xMomentum = 0;
            findPlayer();
        }
    }
    
    public void moveMe()
    {
        realXloc = realXloc + xMomentum;
        realYloc = realYloc + yMomentum;
        setLocation((int)realXloc, (int)realYloc);
        
        checkBoundaries();
        
        if(getOneIntersectingObject(Character.class) != null && recharge <= 0)
        {
            recharge = 60;
            stats.damagePlayer(1);
        }
    }
    
    public void damageEnemy(double amount)
    {   
        BloodSplat bloodsplat = new BloodSplat();
        getWorld().addObject(bloodsplat, getX(), getY());
        bloodsplat.setTrans((int)((10 - health) * 25.5));
        
        health = (health - amount);
        
        if(health <= 0)
        {
            getWorld().removeObject(this);
        }
    }
}
What am I doing wrong? Or is the idea just beyond me at the moment? I'm fine with it if I have to give up on it, there's lots more types of enemies I'm going to code.
bourne bourne

2011/12/12

#
On line 108, did you mean if("y".equals(chargePlayer)) instead of if("x".equals(chargePlayer))
darkmist255 darkmist255

2011/12/12

#
Ha! Of all the mistakes I could've made, it had to be a typo XD! Thanks a million for reading through the code :D! I hate making people read so many lines.
bourne bourne

2011/12/12

#
Yeah no problem. No idea if that would fix it, didn't read too much into it. Trying to convince myself to study for finals ha! Let me know what happens
darkmist255 darkmist255

2011/12/12

#
Yeah, it fixed it. Next step, a bit of AI for how far to charge :D! Good luck with your finals!
bourne bourne

2011/12/12

#
Thanks! Have fun! XD
davmac davmac

2011/12/12

#
I think there's a similar mistake on line 81: else if(Math.abs(yDistance) > Math.abs(xDistance) && Math.abs(xDistance) < 250) You probably meant: else if(Math.abs(yDistance) > Math.abs(xDistance) && Math.abs(yDistance) < 250) Beware of copy-paste! :)
darkmist255 darkmist255

2011/12/12

#
Haha, thanks! I'm gonna read through this again for any more copy-paste mistakes. So, advice to all: NEVER code when you're very tired :D! I did this yesterday after working a few hours on a model then doing my homework, bad idea :D.
You need to login to post a reply.