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

2012/3/11

power ups?

1
2
3
4
programmer22 programmer22

2012/3/11

#
how can I get power ups to work on my game? like can u give me an example ? for a speed booster power up
matt.milan matt.milan

2012/3/11

#
one way i do it is like this
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
        //etc etc
        }
    
        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;
	    }
	}
}


you'd have to make a new actor called SpeedBoost, and add it to your world. i think this would work.
programmer22 programmer22

2012/3/11

#
ok i was working on it already and i use something called ..... step_size that way i only have to change the variable but i will certainly try to make this timer work ty for the time and effrt put into this reply have un programming =)
matt.milan matt.milan

2012/3/11

#
oh yeah i'm sure there's a few ways to get this done. i try to think about all aspects of my power up. Where does it come from? a monster drop, a random appearance, a set location? does it disappear after a short time if i dont collect it? is the boost permanent or temporary? then after i work out all those questions and write the first code, i usually end up with a ton of bugs. that's when the fun starts ;) looking forward to seeing your finished code
programmer22 programmer22

2012/3/11

#
hello, matt i have a question the timer doesnt exactly work .... when it collects the power up it ticks off one on the time then the power up wont go away because the timer wont hit zero any ideas ?
matt.milan matt.milan

2012/3/11

#
for the class variable, we need something like this
private final int MAX_TIME = 100;
private int timeleft = MAX_TIME;
private boolean powerUpActive = false;
in act, you need something like this
if (powerUpActive)
{
    powerUpScript();
}
powerUpActive would be a boolean. it would start off false. it would be set true when actor eats that powerup
if (canSee(Powerup.Class))
{
    eat(Powerup.Class);
    powerUpActive = true;
    statModified += modifier;
}
finally, for the script to have a function and count down correctly, i'd try
public void powerUpScript()
{
    timeLeft--;
    if (timeLeft <= 0)
    {
        powerUpActive = false;
        statModified -= modifier;
        timeLeft = MAX_TIME;
    }
}
statModified can be whatever you like, modifier can be whatever you like also.
GamesGrinder1998 GamesGrinder1998

2014/4/16

#
matt.milan wrote...
one way i do it is like this
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
        //etc etc
        }
    
        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;
	    }
	}
}


you'd have to make a new actor called SpeedBoost, and add it to your world. i think this would work.
what class did you put this in?
danpost danpost

2014/4/16

#
GamesGrinder1998 wrote...
<code omitted> what class did you put this in?
Look again (at line 1). Be careful. I did notice some typos, bad variable names and an invalid method call in the code.
GamesGrinder1998 GamesGrinder1998

2014/4/16

#
danpost wrote...
GamesGrinder1998 wrote...
<code omitted> what class did you put this in?
Look again (at line 1). Be careful. I did notice some typos, bad variable names and an invalid method call in the code.
but if i try to use it in my game which is like space invaders or galaga, would it go in the starfighter or fireball
danpost danpost

2014/4/16

#
It would be more likely that you would want the starfighter to have special abilities. Once a fireball is launched, I doubt that you would want to change the way it behaves.
GamesGrinder1998 GamesGrinder1998

2014/4/16

#
danpost wrote...
It would be more likely that you would want the starfighter to have special abilities. Once a fireball is launched, I doubt that you would want to change the way it behaves.
thanks what about the following code as stated above? where do i put this
matt.milan wrote...
for the class variable, we need something like this
private final int MAX_TIME = 100;
private int timeleft = MAX_TIME;
private boolean powerUpActive = false;
in act, you need something like this
if (powerUpActive)
{
    powerUpScript();
}
powerUpActive would be a boolean. it would start off false. it would be set true when actor eats that powerup
if (canSee(Powerup.Class))
{
    eat(Powerup.Class);
    powerUpActive = true;
    statModified += modifier;
}
finally, for the script to have a function and count down correctly, i'd try
public void powerUpScript()
{
    timeLeft--;
    if (timeLeft <= 0)
    {
        powerUpActive = false;
        statModified -= modifier;
        timeLeft = MAX_TIME;
    }
}
statModified can be whatever you like, modifier can be whatever you like also.
danpost danpost

2014/4/16

#
GamesGrinder1998 wrote...
thanks what about the following code as stated above? where do i put this
matt.milan wrote...
< code omitted >
That is basically the same code. It is just generalized a bit.
GamesGrinder1998 GamesGrinder1998

2014/4/17

#
danpost wrote...
GamesGrinder1998 wrote...
thanks what about the following code as stated above? where do i put this
matt.milan wrote...
< code omitted >
That is basically the same code. It is just generalized a bit.
so it's basically a simpler version of the one on the top??
GamesGrinder1998 GamesGrinder1998

2014/4/17

#
danpost wrote...
GamesGrinder1998 wrote...
thanks what about the following code as stated above? where do i put this
matt.milan wrote...
< code omitted >
That is basically the same code. It is just generalized a bit.
and would i have to make any extra classes or do i put that in the starfighter class
danpost danpost

2014/4/17

#
If you use that code, ver batim, you would need a 'Powerup' class.
There are more replies on the next page.
1
2
3
4