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
Gingervitis Gingervitis

2013/3/29

#
Oh ok... Now I have another question. If I were to pick up two power-up, it looks like you only have one power-up. Is there a way to make so if you get more than one power-up, they rotate at different spots? I also plan to add more power-ups that rotate around the actor and it will look weird if they rotate on top of each other.
danpost danpost

2013/3/29

#
The way I set it up is this: if you pick up another powerup before the first one has expired then the timer is reset on the one it already has and the other is not added. It is equivalent to the old one being replaced with the new one with a new timer.
Gingervitis Gingervitis

2013/3/29

#
I did notice that.... Then is there a way to make it so when the power-ups are circling the crab, the crab cannot be eaten by the lobster? Right now it only eats the lobster if the actual power-up hits it.
Gingervitis Gingervitis

2013/3/29

#
How can I get a boolean statement to return true from another class?
danpost danpost

2013/3/29

#
Since your Lobster class has the code to remove the Crab object when a Lobster object contact it, that is where the code needs change. The method 'lookForCrab' needs to be adjusted as follows:
public void lookForCrab()
{ 
    Crab crab = (Crab)getOneIntersectingObject(Crab.class);
    if (crab == null) return;
    if (crab.isPoweredUp())
    {
        getWorld().removeObject(this);
        return;
    }
    getWorld().removeObject(crab);
    Greenfoot.playSound("au.wav");
    Greenfoot.stop();
}
Add this method to the Crab class:
public boolean isPoweredUp()
{
    return (powerUp != null && powerUp.getWorld() != null);
}
Notice the removal of the calls to 'canSee' and 'eat' in the 'lookForCrab' method. The reason I removed them is because the code was unnecessarily looking for the crab object on multiple occasions (in 'eat', in 'canSee' and in checking the powerup status). As a sidenote: I particularly do not care for the Animal class as there is little to gain from using it (other than some readability), and it does have some drawbacks.
Gingervitis Gingervitis

2013/3/29

#
I set was able to set up a boolean statement in the Lobster class that makes it only be able to eat the crab when false. That is why I asked about getting a boolean statement to return true from another class.
danpost danpost

2013/3/29

#
Also, line 11 of the 'isCanSeeDP' method I provided earlier for the Crab class code can be changed:
// from
if (powerUp == null || powerUp.getWorld() == null) return;
// to
if (!isPoweredUp()) return;
Gingervitis Gingervitis

2013/3/29

#
My crab still gets eaten when the power-up doesn't touch the lobster.
danpost danpost

2013/3/29

#
Let us try this again:
// the Lobster class 'lookForCrab' method
public void lookForCrab()
{ 
    Crab crab = (Crab)getOneIntersectingObject(Crab.class);
    if (crab == null) return;
    if (crab.isPoweredUp())
    {
        getWorld().removeObject(this);
        return;
    }
    getWorld().removeObject(crab);
    Greenfoot.playSound("au.wav");
    Greenfoot.stop();
}
// the Crab class 'ifCanSeeDP' method
public void ifCanSeeDP()
{
    if (canSee(DominatorPower.class))
    {
        eat(DominatorPower.class);
        getWorld().removeObjects(getWorld().getObjects(DominatorPower1.class));
        powerUp = new DominatorPower1();
        getWorld().addObject(powerUp, getX(), getY());
        timer1 = 500;
    }
    if (!isPoweredUp()) return;
    angle = (angle+5)%360;  
    powerUp.setLocation(getX(), getY());  
    powerUp.setRotation(angle);  
    powerUp.move(40);
    powerUp.turn(90);
    timer1--;
    if (timer1 == 0) getWorld().removeObject(powerUp);
}

// the Crab class 'isPoweredUp' method
public boolean isPoweredUp()
{
    return (powerUp != null && powerUp.getWorld() != null);
}
Copy/paste these in your code, replacing any previous code for these methods and try again. Report back as to results.
Gingervitis Gingervitis

2013/3/29

#
What was before was the Lobster could not eat the Crab at all when your 'lookForLobster' code was included so I will see what happens when I change the code.
Gingervitis Gingervitis

2013/3/29

#
Okay. That fixed the issue of having the power-up, but now it appears that when I don't have the power-up, the Lobster is eating me at a closer range because of line 4 in your first code.
danpost danpost

2013/3/29

#
You can change that back to 'getOneObjectAtOffset(0, 0, Crab.class)' if you want.
Gingervitis Gingervitis

2013/3/29

#
If I change that, then the lobster can eat me when I have the power-up..... Is there a way to have both?
danpost danpost

2013/3/29

#
Line 6 in the 'lookForCrab' code above should prevent the Lobster from eating the crab when it has the powerup.
Gingervitis Gingervitis

2013/3/29

#
Yes, so if I take that out it will work in both cases?
There are more replies on the next page.
1
2
3
4
5