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

2012/1/22

Probably something stupid :D.

darkmist255 darkmist255

2012/1/22

#
I have the code for this monster to shoot, but for some reason it doesn't feel like putting a new shot into the world. This is the code
    public void decideMoveShoot()
    {
        if(character != null)
        {
            //hypotenuse less that 225 away from player
            if(Math.sqrt(Math.abs((character.getY()-realYloc)*(character.getY()-realYloc) + ((character.getX()-realXloc)*(character.getX()-realXloc)))) < 225)
            {
                if(shotRecharge <= 0)
                {
                    System.out.println("it should have fired"); //This is for debugging. Yes, this is triggered each time I get close.
                    AnnoyanceShooterShot newshot = new AnnoyanceShooterShot();
                //getWorld().addObject(new AnnoyanceShooterShot(), getY(), getX());
                room.addObject(newshot, getY(), getX());
                shotRecharge = shotRechargeTime;
                }
            }
        }
    }
Did I do something incorrectly?
danpost danpost

2012/1/22

#
Let us look at your AnnoyanceShooterShot constructor and related methods.
darkmist255 darkmist255

2012/1/22

#
Here's the entire class for AnnoyanceShooterShot:
public class AnnoyanceShooterShot extends EnemyProjectile
{
    
    Room room;
    Stats stats;
    Character character;
    double realXloc, realYloc;
    double xMomentum = 5;
    double yMomentum = 5;
    double angle = 0;
    double xMove, yMove;
    double shotLife = 60;
    private boolean remove = false;
    
    public void act()
    {
        moveDouble();
        shotDecay();
        checkRemove();
    }
    
    public void moveDouble()
    {        
        realXloc = realXloc + (xMove * xMomentum);
        realYloc = realYloc + (yMove * yMomentum);
        
        setLocation((int)realXloc, (int)realYloc);
        
        if(hitBoundary())
        {
            getWorld().removeObject(this);
        }
    }
   
    public void addedToWorld(World world)
    {
        room = (Room)getWorld();
        stats = room.stats;
        character = room.character;
        realXloc = getX();
        realYloc = getY();
        pointAtObject(character);
    }
    
    private void shotDecay()
    {        
        if(shotLife < 15)
        {
            getImage().setTransparency(240);
        }
        
        if(shotLife < 10)
        {
            getImage().setTransparency(200);
        }
        
        if(shotLife < 5)
        {
            getImage().setTransparency(120);
        }
        
        if(shotLife < 2)
        {
            getImage().setTransparency(40);
        }
        
        if(shotLife > 0)
        {
            shotLife = shotLife - 1;
        }
        
        if(shotLife <= 0)
        {
            remove = true;
        }
    }
    
    public void pointAtObject(Actor target)
    {
        if(character != null)
        {
            angle = ((Math.atan2(target.getY()-getY(),target.getX()-getX())));
    
            xMove = (Math.cos(angle));
            yMove = (Math.sin(angle));
        }
    }
    
    
    public void checkRemove()
    {
        if(remove)
        {
            getWorld().removeObject(this);
        }
    }
}
danpost danpost

2012/1/22

#
I do not percieve anything wrong with your code, except a couple of minor (probably not of any consequence) things. 1) Line 29 refers to a method 'hitBoundary()' which I do not see anywhere, although moveDouble() is called first in your act() method and continues to call other methods, at what point the actor may not be in the world anymore, due to 'removeObject(this)' in moveDouble(); and 2) Line 80 uses a reference to 'character', whereas the Actor in the parameter is named 'target'. If the target is always the character, then no need for the parameter at all. I copied (and made minor adjustments to) the code snippets above into a sample scenario, and it seems to work OK. The only difference is that I used simple movements for the Enemy and Character (kept the Shot as moveDouble), and used 'move(1);' with random turn(rand(3) * 90 - 90). Also, I did not know what realXloc and realYloc were in your Enemy (monster) snippet, so I changed them to getX() and getY(). My suggestion is to decrease the xMomentum and yMomentum to see if the shot was off the screen before you realized it, and ensure there is an image.
darkmist255 darkmist255

2012/1/22

#
Ahh, yes, I'll replace 'removeObject(this)' with 'remove = true'. hitBoundary() just check if it is outside the screen, so I see no possible problems there. I'm keeping the parameter since I'm thinking of moving that into a class above this one, so that every monster can use it. I'll do some wider troubleshooting like you said (look at the shot class) and see if anything pops up. Strange though, since manually placing the shot works fine.
danpost danpost

2012/1/22

#
One other thing, on lines 12 and 13 of 'decideMoveShoot()', you have 'getY()' before 'getX()'; they need switched.
darkmist255 darkmist255

2012/1/24

#
Wow... Thanks, that was it :D! I'm going to hit my head against the wall for a few minutes now.
You need to login to post a reply.