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

2012/3/11

power ups?

1
2
3
4
5
GamesGrinder1998 GamesGrinder1998

2014/4/17

#
danpost wrote...
If you use that code, ver batim, you would need a 'Powerup' class.
what do i put in the powerup class
danpost danpost

2014/4/17

#
Remember, I said that the second code-set given was a generalization of the first. The first one has a specific type of powerup in mind. The second set was for 'any' powerup, generally. It is not intended to be used directly. You will have to determine what kind of powerup you want to implement. Create a class for that powerup, so there is an actor for your starfighter to intersect with and 'pick up the ability'. The general 'blueprint' was followed in the first code-set, creating a powerup that provides extra speed to the actor. Now, you have to follow that 'blueprint' to create the powerup of your choice for your starfighter.
GamesGrinder1998 GamesGrinder1998

2014/4/17

#
danpost wrote...
Remember, I said that the second code-set given was a generalization of the first. The first one has a specific type of powerup in mind. The second set was for 'any' powerup, generally. It is not intended to be used directly. You will have to determine what kind of powerup you want to implement. Create a class for that powerup, so there is an actor for your starfighter to intersect with and 'pick up the ability'. The general 'blueprint' was followed in the first code-set, creating a powerup that provides extra speed to the actor. Now, you have to follow that 'blueprint' to create the powerup of your choice for your starfighter.
so the first set of code i put it in a class which extends from a the second set of code in a class which is extended from the actor? P.S i'm lost
danpost danpost

2014/4/17

#
The Powerup class should be named a little more specifically (like SpeedPowerUp, ShotPowerUp, ShieldPowerUp, whatever). Or, you could just use one class for all your powerups. Use static int fields to number the different powerups you will use; use a static array of Strings for the different images for each powerup; and have an argument in the constructor of the Powerup class to receive the type of powerup it is to be. The constructor will then set the appropriate image and save its int type. A 'get' method would be helpful to return the type of powerup that it is.
GamesGrinder1998 GamesGrinder1998

2014/4/17

#
danpost wrote...
The Powerup class should be named a little more specifically (like SpeedPowerUp, ShotPowerUp, ShieldPowerUp, whatever). Or, you could just use one class for all your powerups. Use static int fields to number the different powerups you will use; use a static array of Strings for the different images for each powerup; and have an argument in the constructor of the Powerup class to receive the type of powerup it is to be. The constructor will then set the appropriate image and save its int type. A 'get' method would be helpful to return the type of powerup that it is.
so i basically copy all the first code into my starfighter
danpost danpost

2014/4/17

#
GamesGrinder1998 wrote...
so the first set of code i put it in a class which extends from a the second set of code in a class which is extended from the actor? P.S i'm lost
Yes, you are. The only code-set needed for the SpeedBoost powerup is given in the first code-set. The second code-set broke down the individual parts of what was needed and made more general so that any powerup could be created by following its 'blueprint'. Each part in the second code-set can be found within the first code-set (modified for the specific powerup). How about if I put it this way. You cannot have a general powerup. What ability will it give? You need a specific type of powerup in mind to 'transform' (using the 'blueprint') the second code-set into working code for that type of powerup.
danpost danpost

2014/4/17

#
GamesGrinder1998 wrote...
so i basically copy all the first code into my starfighter
Not all of it, just those parts that are 'reflected' in the second code-set; which is, after looking at it, pretty much all of it.
GamesGrinder1998 GamesGrinder1998

2014/4/17

#
danpost wrote...
GamesGrinder1998 wrote...
so i basically copy all the first code into my starfighter
Not all of it, just those parts that are 'reflected' in the second code-set; which is, after looking at it, pretty much all of it.
so i put the first set of code into my starfighter and the actual powerup would contain what sort of code?
danpost danpost

2014/4/17

#
danpost wrote...
The Powerup class should be named a little more specifically (like SpeedPowerUp, ShotPowerUp, ShieldPowerUp, whatever). Or, you could just use one class for all your powerups. Use static int fields to number the different powerups you will use; use a static array of Strings for the different images for each powerup; and have an argument in the constructor of the Powerup class to receive the type of powerup it is to be. The constructor will then set the appropriate image and save its int type. A 'get' method would be helpful to return the type of powerup that it is.
I though this was a fairly good start on what should go into the actual powerup class. The only thing not mentioned was a timer that the act method would run down and remove the actor if it reaches zero. This is only if the powerup was to exist for a short time and disappear if not picked up within the allotted time.
GamesGrinder1998 GamesGrinder1998

2014/4/17

#
danpost wrote...
danpost wrote...
The Powerup class should be named a little more specifically (like SpeedPowerUp, ShotPowerUp, ShieldPowerUp, whatever). Or, you could just use one class for all your powerups. Use static int fields to number the different powerups you will use; use a static array of Strings for the different images for each powerup; and have an argument in the constructor of the Powerup class to receive the type of powerup it is to be. The constructor will then set the appropriate image and save its int type. A 'get' method would be helpful to return the type of powerup that it is.
I though this was a fairly good start on what should go into the actual powerup class. The only thing not mentioned was a timer that the act method would run down and remove the actor if it reaches zero. This is only if the powerup was to exist for a short time and disappear if not picked up within the allotted time.
sorry i'm getting confused, so which parts should i put in starfighter and powerup??
danpost danpost

2014/4/17

#
GamesGrinder1998 wrote...
sorry i'm getting confused, so which parts should i put in starfighter and powerup??
Dear confused, there is no code so far given for any powerup class. I gave a suggestion on how a powerup class could be written, but that was it.
GamesGrinder1998 GamesGrinder1998

2014/4/17

#
danpost wrote...
GamesGrinder1998 wrote...
sorry i'm getting confused, so which parts should i put in starfighter and powerup??
Dear confused, there is no code so far given for any powerup class. I gave a suggestion on how a powerup class could be written, but that was it.
ok so to clear things up, pretty much of the first set of code is all i need for the starfighter and the powerup contains the timer and what it does (increases speed)? PSS: IM SO CONFUSED
danpost danpost

2014/4/17

#
You will need a timer in both classes (the starfighter class and the powerup class). The one in the starfighter class is for the lifespan of the ability that the powerup will give. The one in the powerup class is for the lifespan of the pickup time (how long the powerup will be available to be picked up. The one for your starfighter was included in the initial code-sets. The one for the powerup was only mentioned as something that might be implemented in the powerup class. A basic universal Powerup class might be something like this:
import greenfoot.*;

public class Powerup extends Actor
{
    static final int SPEED_PU = 0, DRAG_PU = 1, AMMO_PU = 2; // add as needed
    static final GreenfootImage[] images = { "speedPU.png", "dragPU.png", "ammoPU.png" }; // add as needed; keep the same order as the static final int fields above

    int puType; // a value from the list of static final ints above
    int lifespan = 300; // about 5 to 6 seconds

    // a Powerup object can be created with 'new Powerup(Powerup.SPEED_PU)' or similar
    public Powerup(int type)
    {
        puType = type;
        setImage(images[powerup]);
    }

    public void act()
    {
        lifespan--;
        if (lifespan == 0) getWorld().removeObject(this);
    }

    public int getType()
    {
        return puType;
    }
}
The picking up and implementation of the new ability was also included within the initial code-sets. The type of powerup, however was specific in the initial code-set and, if using this powerup class code, would have to be modified to determine which powerup was picked up (using 'getType' and either a series of 'if-else's or a 'switch' statement. So,
/**  instead of */
Actor actor = getOneObjectAtOffset(0, 0, Powerup.class);
if (actor != null)
{
    getWorld().removeObject(actor);
   // etc.
/** you would use */
Powerup pu = (Powerup)getOneObjectAtOffset(0, 0, Powerup.class);
if (pu != null)
    int kind = pu.getType();
    getWorld().removeObject(pu);
    if (kind == Powerup.SPEED_PU) { /** whatever */ }
    if (kind == Powerup.DRAG_PU) { /* whatever */ }
    // etc.
}
GamesGrinder1998 GamesGrinder1998

2014/4/17

#
danpost wrote...
You will need a timer in both classes (the starfighter class and the powerup class). The one in the starfighter class is for the lifespan of the ability that the powerup will give. The one in the powerup class is for the lifespan of the pickup time (how long the powerup will be available to be picked up. The one for your starfighter was included in the initial code-sets. The one for the powerup was only mentioned as something that might be implemented in the powerup class. A basic universal Powerup class might be something like this:
import greenfoot.*;

public class Powerup extends Actor
{
    static final int SPEED_PU = 0, DRAG_PU = 1, AMMO_PU = 2; // add as needed
    static final GreenfootImage[] images = { "speedPU.png", "dragPU.png", "ammoPU.png" }; // add as needed; keep the same order as the static final int fields above

    int puType; // a value from the list of static final ints above
    int lifespan = 300; // about 5 to 6 seconds

    // a Powerup object can be created with 'new Powerup(Powerup.SPEED_PU)' or similar
    public Powerup(int type)
    {
        puType = type;
        setImage(images[powerup]);
    }

    public void act()
    {
        lifespan--;
        if (lifespan == 0) getWorld().removeObject(this);
    }

    public int getType()
    {
        return puType;
    }
}
The picking up and implementation of the new ability was also included within the initial code-sets. The type of powerup, however was specific in the initial code-set and, if using this powerup class code, would have to be modified to determine which powerup was picked up (using 'getType' and either a series of 'if-else's or a 'switch' statement. So,
/**  instead of */
Actor actor = getOneObjectAtOffset(0, 0, Powerup.class);
if (actor != null)
{
    getWorld().removeObject(actor);
   // etc.
/** you would use */
Powerup pu = (Powerup)getOneObjectAtOffset(0, 0, Powerup.class);
if (pu != null)
    int kind = pu.getType();
    getWorld().removeObject(pu);
    if (kind == Powerup.SPEED_PU) { /** whatever */ }
    if (kind == Powerup.DRAG_PU) { /* whatever */ }
    // etc.
}
thanks if i want different types of powerups, would this code be able to be used for others (such as a shield or two bullets fired at one) and will i need to fill in anything myself or add stuff??? P.SSS sorry if i'm taking too much of your time or asking too many questions
danpost danpost

2014/4/17

#
I was about to edit my code when you posted. Change 'powerup' on line 15 to 'puType'.
There are more replies on the next page.
1
2
3
4
5