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

2016/1/23

make object disappear after 3 hits (take damage)

1
2
3
nico_vo nico_vo

2016/2/3

#
I have a problem with adding an object to my world when I get a shield powerup. this shield is supposed to appear where the rocket is, but it just appears in random places all over my world this is my code:
public void getPowerUp(){
        Actor powerUp = getOneIntersectingObject(PowerUp.class);
        if(powerUp != null){
            getWorld().addObject(new Shield(), this.getY(), this.getX());
        }      
    }
Any ideas of why is this happening? here is a screenshot of an example of where the shield appears: http://imgur.com/Cxibtzq It may seem obvious that I want it just around my rocket, but I say it just in case ;)
nico_vo nico_vo

2016/2/3

#
I also want to add some background music, but just when I click play, but I don't know how to make it just sound once, as when I add Greenfoot.playSound("background music.mp3"); in my act method, it obviously starts to play it 60 times per second, but if I add it in my constructor, it starts to play before hitting play button :S
danpost danpost

2016/2/3

#
nico_vo wrote...
I have a problem with adding an object to my world when I get a shield powerup. this shield is supposed to appear where the rocket is, but it just appears in random places all over my world this is my code: < Code Omitted > Any ideas of why is this happening?
You are only showing the code that places the Shield object into the world. I will presume that the code given is in the class of you rocket. You need to show the code that controls the location of the shield while in the world (could be somewhere else in the class of the rocket or in the Shield class -- or maybe both, incorrectly).
nico_vo nico_vo

2016/2/3

#
Yeah, the code I put is in my rocket's class. The shield class has just exactly the same code for controlling the shield's movement as for controlling the rocket's movement, plus making it disappear after some time. What I need now, is to make the shield appear taking the rocket's position, so that the rocket is just in the middle of the shield
qazxsw21 qazxsw21

2016/2/3

#
what?
danpost danpost

2016/2/3

#
The shield is something that should "belong" to the rocket and the rocket is what should control the location of the shield. This means to keep a reference to the Shield object and the code controlling its location in the class of the rocket.
nico_vo nico_vo

2016/2/3

#
How can I do this then?
danpost danpost

2016/2/3

#
nico_vo wrote...
How can I do this then?
For example:
public class Rocket extends Actor
{
    private Actor shield;

    public Rocket()
    {
        shield = new Shield();
    }

    public void act()
    {
        // current code
        if (< condition to add shield >) addObject(shield, getX(), getY());
        if (shield.getWorld() != null) shield.setLocation(getX(), getY());
    }
}
The Shield class should have the timer for its removal and code to remove asteroids in range.
nico_vo nico_vo

2016/2/3

#
The second "if" condition in the act method is for setting the location of the shield where the rocket is, no?
nico_vo nico_vo

2016/2/3

#
qazxsw21 wrote...
what?
What don't you understand?
danpost danpost

2016/2/3

#
nico_vo wrote...
The second "if" condition in the act method is for setting the location of the shield where the rocket is, no?
Yes. It places the shield at the current location of the rocket if the shield is currently in the world.
Super_Hippo Super_Hippo

2016/2/3

#
nico_vo wrote...
I also want to add some background music, but just when I click play, but I don't know how to make it just sound once, as when I add Greenfoot.playSound("background music.mp3"); in my act method, it obviously starts to play it 60 times per second, but if I add it in my constructor, it starts to play before hitting play button :S
You can use the 'started' method.
private GreenfootSound backgroundMusic = new GreenfootSound("background music.mp3");

public void started()
{
    backgroundMusic.playLoop();
}

public void stopped()
{
    backgroundMusic.pause();
}
nico_vo nico_vo

2016/2/4

#
Super_Hippo wrote...
nico_vo wrote...
I also want to add some background music, but just when I click play, but I don't know how to make it just sound once, as when I add Greenfoot.playSound("background music.mp3"); in my act method, it obviously starts to play it 60 times per second, but if I add it in my constructor, it starts to play before hitting play button :S
You can use the 'started' method.
private GreenfootSound backgroundMusic = new GreenfootSound("background music.mp3");

public void started()
{
    backgroundMusic.playLoop();
}

public void stopped()
{
    backgroundMusic.pause();
}
And whith these two methods, what do I do? Do I put in my act method started(); or does it just play without doing anything because of being in world class?
danpost danpost

2016/2/4

#
nico_vo wrote...
And whith these two methods, what do I do? Do I put in my act method started(); or does it just play without doing anything because of being in world class?
The 'started' and 'stopped' methods are World class methods that are called automatically by the greenfoot framework when the specific change in the running state of your scenario occurs while that particular World object is active. I stress that last part because the methods are not called when you change worlds (although, it is possible to call the methods programmatically when a world change occurs). The 'started' method is called automatically when the 'Run' button is clicked or when 'Greenfoot.start' is executed. The 'stopped' method is called automatically when the 'Pause' button is clicked or when 'Greenfoot.stop' is executed. Again, only the methods declared within the currently active world are called automatically during these times.
You need to login to post a reply.
1
2
3