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

2016/9/30

Why are powerups so confusing?

1
2
dERPscHNOzzLE dERPscHNOzzLE

2016/9/30

#
Hi I'm trying to figure out how to add powerups to my game and I've already looked through other discussions and didn't understand them, I need help
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class Player extends Actor
{
    private final int SPEED_BOOST_TIMER = 115;
    private int speedBoostTimeLeft = SPEED_BOOST_TIMER;
    private int mySpeed;
    private boolean gotSpeedBoost = false;
    public Player()
    {
        mySpeed = 5;
    }     
    public void act()
    {
        get(SpeedBoost.class);
        if (gotSpeedBoost)
        {
            speedBoostTimer();
        }
    }
    public void getSpeedBoost()
    {
        Actor actor = getOneObjectAtOffset(0, 0, SpeedBoost.class);
        if (actor != null) 
        {
            getWorld().removeObject(actor);
            gotSpeedBoost = true;
            speed += 5;
        }
    }
    public void speedBoostTimer()
    {
        speedBoostTimeLeft--;
        if (speedBoostTimeLeft <= 0)
        {
            gotSpeedBoost = false;
            speed -= 5;
            speedBoostTimeLeft = SPEED_BOOST_TIMER;
        }
    }
}
Super_Hippo Super_Hippo

2016/9/30

#
Look at line 16. You are trying to call method which is called 'get' and pass the SpeedBoost class as a parameter. There is no 'get' method though. The line should read:
getSpeedBoost();
You should also remove line 24 and change line 25 through 27 to the following:
if (isTouching(SpeedBoost.class))
{
    removeTouching(SpeedBoost.class);
Maybe you should check that you don't increase the speed if the speed was already increased and instead, only set the timer to max again. This is what I mean: (the extra methods are not needed)
public void act()
{
    if (isTouching(SpeedBoost.class)
    {
        removeTouching(SpeedBoost.class);
        speedBoostTimeLeft = SPEED_BOOST_TIMER;
        if (!gotSpeedBoost)
        {
            speed += 5;
            gotSpeedBoost = true;
        }
    }
    
    if (gotSpeedBoost)
    {
        speedBoostTimeLeft--;
        if (speedBoostTimeLeft == 0)
        {
            gotSpeedBoost = false;
            speed -= 5;
        }
    }
}
Probably there should be some moving code in the act method too.
dERPscHNOzzLE dERPscHNOzzLE

2016/9/30

#
I was following one of dan's ancient posts and it was saying stuff about having a a subclass for power up but not for SpeedBoost. I'm so lost
dERPscHNOzzLE dERPscHNOzzLE

2016/9/30

#
and speed is underlined and i dont know why
dERPscHNOzzLE dERPscHNOzzLE

2016/9/30

#
and actor
Super_Hippo Super_Hippo

2016/9/30

#
It should be mySpeed and not speed.
dERPscHNOzzLE dERPscHNOzzLE

2016/9/30

#
ok and what about actor?
danpost danpost

2016/9/30

#
There are up to three stages to implementing a powerup. First, you need an actor that is picked up to give (or take) some or all of the ability. It just needs to be distinguishable from other types of powerups if you have more than one. Next, the actor that picks it up must adjust some behavioral state of something (usually of itself). Finally, if the powerup is not permanent, then a timer field will need implemented as well so the ability can be reversed when exhausted. Remember that there are many ways to code thing. Some are just better than other (for various reasons). The reason I suggest that you not put each type powerup in a separate class is because is makes your code unnecessarily bulky (too many classes). A powerup is a powerup regardless of what it does. It is easy enough to distinguish one from another by use of a simple field (a different value for each type powerup). One of the reasons that I created my Value Display Tutorial scenario was to show how a simple actor class can be used for multiple type objects. The SimpleActor class referred to within it can also be used for the powerups. Even without a field, there are ways to make one distinguishable from another (which I will not get into now).
danpost danpost

2016/9/30

#
dERPscHNOzzLE wrote...
ok and what about actor?
I do not see any issue with 'actor'. What error message are you getting on it?
dERPscHNOzzLE dERPscHNOzzLE

2016/9/30

#
cannot find symbol - variable actor
dERPscHNOzzLE dERPscHNOzzLE

2016/9/30

#
its only in getSpeedBoost()
dERPscHNOzzLE dERPscHNOzzLE

2016/9/30

#
Im not quite sure why I changed it but this is what it is now
public void getSpeedBoost()
    {
        if (isTouching(SpeedBoost.class))
        {
            removeTouching(SpeedBoost.class);
        }
        if (actor != null) 
        {
            getWorld().removeObject(actor);
            gotSpeedBoost = true;
            mySpeed += 5;
        }
    }
dERPscHNOzzLE dERPscHNOzzLE

2016/9/30

#
it compiled but it wont increase my speed when I grab the SpeedBoost
danpost danpost

2016/9/30

#
dERPscHNOzzLE wrote...
it compiled but it wont increase my speed when I grab the SpeedBoost
Try this:
if (isTouching(SpeedBoat.class))
{
    removeTouching(SpeedBoat.class);
    gotSpeedBoot = true;
    mySpeed += 5;
}
dERPscHNOzzLE dERPscHNOzzLE

2016/9/30

#
its still not working, here is my current code for my mouse
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


/**
 * Write a description of class Mouse here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Mouse extends Player
{
    private final int SPEED_BOOST_TIMER = 115;  
    private int speedBoostTimeLeft = SPEED_BOOST_TIMER;  
    private int mySpeed;  
    private boolean gotSpeedBoost = false; 
    private int x;
    private int y;
    
    public Mouse()
    {
        getImage().scale(95, 50);
        mySpeed = 5;
    }
    /**
     * Mouse will eat cheese when it comes across it.
     * Mouse will move when keys are pressed.
     */
    public void act()
    {
       isKeyDown();
       lookForCheese();
       cheeseInWorld();
       getSpeedBoost();
       if (gotSpeedBoost)
       {
           speedBoostTimer();
       }
    }
    /**
     * If the up arrow key is pressed the mouse will move forward.
     * If the down arrow key is pressed the mouse will move backward.
     * If the left arrow key is pressed the mouse will turn left.
     * If the right arrow key is pressed the mouse will turn right.
     */
    public void isKeyDown()
    {
        if (Greenfoot.isKeyDown("up"))
        {
            move(mySpeed);
        }
        if (Greenfoot.isKeyDown("down"))
        {
            move(-mySpeed);
        }
        if (Greenfoot.isKeyDown("left"))
        {
            turn(-5);
        }
        if (Greenfoot.isKeyDown("right"))
        {
            turn(5);
        }
    }
    /**
     * Check whether we have found a Cheese.
     * If we have, eat it.  If not, do nothing.
     */
    public void lookForCheese()
    {
        if (canSee(Cheese.class))
        {
            eat(Cheese.class);
            Greenfoot.playSound("Ring.mp3");
        }
    }
    /**
     * Check if there is any cheese in the world
     */
    public void cheeseInWorld()
    {
        if (getWorld().getObjects(Cheese.class).isEmpty())
        {
            World myWorld = getWorld();
            YouWin youwin = new YouWin();
            myWorld.addObject(youwin, myWorld.getWidth()/2, myWorld.getHeight()/2);
            Greenfoot.playSound("HappyT.mp3");
            Greenfoot.stop();
        }
    }
}
There are more replies on the next page.
1
2