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

2013/3/29

moving and turning objects

1
2
3
4
5
6
danpost danpost

2013/3/29

#
This is a line 6, not line 4. Let me show you the new 'lookForCrab' method now:
// the Lobster class 'lookForCrab' method
public void lookForCrab()
{ 
    Crab crab = (Crab)getOneObjectAtOffset(0, 0, Crab.class); // this line gives more play area for crab around the lobster
    if (crab == null) return;
    if (crab.isPoweredUp()) // this line ensures the powered-up crab is not eaten by lobster
    {
        getWorld().removeObject(this);
        return;
    }
    getWorld().removeObject(crab);
    Greenfoot.playSound("au.wav");
    Greenfoot.stop();
}
Gingervitis Gingervitis

2013/3/29

#
With that, I can still can get eaten with the power-up..... this requires a boolean statement that will make it " Crab crab = (Crab)getOneObjectAtOffset(0, 0, Crab.class);" when it doesn't have a power-up and "Crab crab = (Crab)getOneIntersectingObject(Crab.class); " when it has the power-up.....
danpost danpost

2013/3/29

#
OK. Try this:
// the Lobster class 'lookForCrab' method
public void lookForCrab()
{ 
    Crab crab = (Crab)getOneIntersectingObject(Crab.class); 
    if (crab == null) return;
    if (crab.isPoweredUp()) // this line ensures the powered-up crab is not eaten by lobster
    {
        getWorld().removeObject(this);
        return;
    }
    if (getObjectAtOffset(0, 0, Crab.class) == null) return;
    getWorld().removeObject(crab);
    Greenfoot.playSound("au.wav");
    Greenfoot.stop();
}
Gingervitis Gingervitis

2013/3/29

#
i got an error while compiling at line 11 saying it can't find the symbol method
danpost danpost

2013/3/29

#
I just got the method name wrong. Change line 11 to:
if (getOneObjectAtOffset(0, 0, Crab.class) == null) return;
Gingervitis Gingervitis

2013/3/29

#
Yay! That fixed it completely!!! Thank you!!!! One more question, and then I'm done with questions for the day... well I hope... I am still having trouble with getting my Crab class to set boolean statements from other classes to be true.... If this is in my worm class:
public void wormPowerUp()
    {
        if (wormPowerUp == true)
        {
            Crab crab = (Crab) getWorld().getObjects(Crab.class).get(0);  
            turnTowards(crab.getX(), crab.getY());  
            move(3);
        }
    }
what will I have to put in my crab class to make it true when it eats the worm power-up? (the response to this should help me with other power-ups)
danpost danpost

2013/3/29

#
What is the name of the class the creates the worm powerup?
Gingervitis Gingervitis

2013/3/29

#
the crab eats the WormPower class to create the WormPowerUp class.
Gingervitis Gingervitis

2013/3/29

#
the worm class is also involved with the power-up. The worm power-up makes all the worms on screen go to the crab, but the wormPowerUp class rotates around the crab like the other power-up
danpost danpost

2013/3/29

#
OK. It would be basically the same as the DominatorPower powerUp.
// add instance field to Crab class
private int wpTimer;
// in act
ifCanSeeWP();
// add methods
private void ifCanSeeWP()
{
    if (canSee(WormPowerUp.class))
    {
        eat(WormPowerUp.class);
        wpTimer = 500;
    }
    if (wormPowered()) wpTimer--;
}

public boolean wormPowered()
{
    return wpTimer > 0;
}

// the 'wormPowerUp' method in your Worm class
private void wormPowerUp()
{
    Crab crab = (Crab)getWorld().getObjects(Crab.class).get(0);
    if (crab != null && crab.wormPowered())
    {
        turnTowards(crab.getX(), crab.getY());
        move(3);
    }
}
danpost danpost

2013/3/29

#
Is it really necessary for the worm powerup to rotate around the crab also? The fact that the worms are migrating toward the crab should be enough to show that the powerup is active. If you want it still to rotate, let me know.
Gingervitis Gingervitis

2013/3/30

#
I was already able to make it rotate around but now that you mention it, I will take that out.... I must have missed something because the worms aren't moving toward the crab.
danpost danpost

2013/3/30

#
Did you add 'wormPowerUp();' to the act method of the Worm class? I did. And WOW. Three or four levels went by lickity-split! Might want to slow the migration toward the crab and decrease the length of the powerup.
danpost danpost

2013/3/30

#
I changed 'wpTimer = 500;' to 'wpTimer = 200;' at line 11 above; and I also changed 'move(3);' to 'move(2);' at line 28. That seemed to make it a little better.
Gingervitis Gingervitis

2013/3/30

#
Can I publish my source code? I think I missed a step.....
There are more replies on the next page.
1
2
3
4
5
6