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
6
7
danpost danpost

2014/4/18

#
Build your scenario by implementing one powerup at a time. First add the Powerup class to your project; then start with the Speed_PU powerup. Once you got it working, add the next one -- and so on.
GamesGrinder1998 GamesGrinder1998

2014/4/18

#
danpost wrote...
Build your scenario by implementing one powerup at a time. First add the Powerup class to your project; then start with the Speed_PU powerup. Once you got it working, add the next one -- and so on.
so would the speed_PU extend from the powerup class so it can inherit some sort of code
danpost danpost

2014/4/18

#
GamesGrinder1998 wrote...
so would the speed_PU extend from the powerup class so it can inherit some sort of code
Nothing will extend the Powerup class. The only think you will do in the Powerup class is assign the type of powerup a unique number in line 5 (as already shown) and list the name of the image field in line 6 for that powerup (as already shown). With those two things, that particular type of powerup is ready to be added to the world and picked up (or disappear if not picked up in time). The object that picks it up (the starfighter -- in the starfighter class) will detect, pick-up and implement the new ability. In other words the three powerups already listed in lines 5 and 6 are ready (as far as powerups are concerned). Everything else is done somewhere else.
GamesGrinder1998 GamesGrinder1998

2014/4/18

#
danpost wrote...
GamesGrinder1998 wrote...
so would the speed_PU extend from the powerup class so it can inherit some sort of code
Nothing will extend the Powerup class. The only think you will do in the Powerup class is assign the type of powerup a unique number in line 5 (as already shown) and list the name of the image field in line 6 for that powerup (as already shown). With those two things, that particular type of powerup is ready to be added to the world and picked up (or disappear if not picked up in time). The object that picks it up (the starfighter -- in the starfighter class) will detect, pick-up and implement the new ability. In other words the three powerups already listed in lines 5 and 6 are ready (as far as powerups are concerned). Everything else is done somewhere else.
will it need a timer code and has the timer code been written already
GamesGrinder1998 GamesGrinder1998

2014/4/18

#
danpost wrote...
GamsGrinder1998 wrote...
so what parts do I use from you and the other code
Like I stated above, the only thing (after making the correction) that you should change in the Powerup class code are the 'static final' fields. The 'static final int' fields give each different type of powerup you will use a unique number. The 'static final GreenfootImage' array will hold the images to use for each different type in numbered order. That is it. Anything else will be dealt with in the classes of the objects that will receive the powerup abilities.
also what is the purpose of drag and Ammo
. drag is the opposite of 'Speed' (like a negative powerup -- one you might want to avoid) and Ammo is for restocking your arsenal. Like I stated above, these are just for example and can be removed or changed to whatever type of powerup you wish to implement.
is the drag and ammo ones important or is it just optional
danpost danpost

2014/4/18

#
GamesGrinder1998 wrote...
is the drag and ammo ones important or is it just optional
No powerup is mandatory. Of course, they are optional. Like I stated, I put them in as examples of how to build the statements for your powerups (the ones that you want to use -- not necessarily the ones I have in there).
GamesGrinder1998 GamesGrinder1998

2014/4/18

#
danpost wrote...
GamesGrinder1998 wrote...
is the drag and ammo ones important or is it just optional
No powerup is mandatory. Of course, they are optional. Like I stated, I put them in as examples of how to build the statements for your powerups (the ones that you want to use -- not necessarily the ones I have in there).
for some reason it won't compile, it says "cannot find symbol - method get (java.lang.Class<SpeedBoost>) here is the code that i got from matt and i used it in my starfighter
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Starfighter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Starfighter 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 Starfighter()
    {
        mySpeed = 5;
    }    

    public void act() 
    {
        checkKeys();
        atWorldEdge(); 
        stopAtWalls(); 
        get(SpeedBoost.class);  
        if (gotSpeedBoost)  
        {  
            speedBoostTimer();  
        }
    }

    public void checkKeys()
    {
        if ( Greenfoot.isKeyDown("left") )
        {
            move(-5);
        }
        if ( Greenfoot.isKeyDown("right") )
        {
            move(5);
        }   
        if("space".equals(Greenfoot.getKey()))
        {
            getWorld().addObject(new Fireball(getRotation()), getX(), getY());
            Greenfoot.playSound("fireball.wav");
        }    
    }

    public void stopAtWalls()
    {  
        if(getOneIntersectingObject(Wall.class)!=null)
        {
            setLocation(x,y);
        }
        else
        {
            x = getX();
            y = getY();
        }    
    }  

    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;  
        }  
    }  
}    
danpost danpost

2014/4/18

#
I did mention to be careful when using his code -- that there were problems with it. Line 28 in your last code post should be:
getSpeedBoost();
GamesGrinder1998 GamesGrinder1998

2014/4/18

#
danpost wrote...
I did mention to be careful when using his code -- that there were problems with it. Line 28 in your last code post should be:
getSpeedBoost();
thanks it did compile but i got a new problem, it highlights in red line 71, the word "speed" and it says cannot find symbol - variable speed...if you can, can you find any other problems with it
GamesGrinder1998 GamesGrinder1998

2014/4/18

#
danpost wrote...
I did mention to be careful when using his code -- that there were problems with it. Line 28 in your last code post should be:
getSpeedBoost();
don't worry i got it to compile, just had to change a few stuff to capital letters now that, what code can i put into my actual speedboost powerup?
GamesGrinder1998 GamesGrinder1998

2014/4/18

#
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.
}
about the second set of code where do i put that and what do i put in this ({/**whatever*/})
danpost danpost

2014/4/18

#
GamesGrinder1998 wrote...
don't worry i got it to compile, just had to change a few stuff to capital letters now that, what code can i put into my actual speedboost powerup?
'change a few stuff to capital letters' does not sound right (I do not see where that is going to fix your problem -- or at least, fix it correctly). 'what code can i put in my actual speedboost powerup' sounds like you are still trying to create a separate class for the specific powerup, which I stated should not be done with my code. '/** whatever */' depends on the powerup, I explained that and you quoted it above. 'about the second set of code where do i put that' -- in the class of the object that will be gaining the ability when picking up the powerup (the starfighter class).
GamesGrinder1998 GamesGrinder1998

2014/4/18

#
so the second set of code would go into the fireball, so does the '/**whatever*/' is what it does and aren't i suppose to create a seperate class for the specific powerup
danpost danpost

2014/4/18

#
GamesGrinder1998 wrote...
so the second set of code would go into the fireball
Let me ask you this, if a fireball comes in contact with a powerup, do you want the powerup to disappear and that fireball to gain a special ability?
so does the '/**whatever*/' is what it doesaren't i suppose to create a seperate class for the specific powerup
Absolutely NOT. Please do not keep repeating the same questions over and over again. A verification may be alright; but, to re-verify so many times is quite unnecessary.
GamesGrinder1998 GamesGrinder1998

2014/4/18

#
danpost wrote...
GamesGrinder1998 wrote...
so the second set of code would go into the fireball
Let me ask you this, if a fireball comes in contact with a powerup, do you want the powerup to disappear and that fireball to gain a special ability?
so does the '/**whatever*/' is what it doesaren't i suppose to create a seperate class for the specific powerup
Absolutely NOT. Please do not keep repeating the same questions over and over again. A verification may be alright; but, to re-verify so many times is quite unnecessary.
what do i put in whatever and do i replace the second set of code with the one that i put down
There are more replies on the next page.
1
2
3
4
5
6
7