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

2014/4/17

Help for Bomb Power-UP

1
2
3
Neymar.Jr Neymar.Jr

2014/4/17

#
I coded the bomb class but it won't compile for some reason. Could someone help me out, thanks. Here's my code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bomb here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bomb extends PowerUps
{
    /**
     * Act - do whatever the Bomb wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        Player1Collision();
        Player2Collision();

    }    
    
    public void Player1Collision()
    {
        if (getWorld() != null)
        {
            Player1 collision = (Player1) getOneIntersectingObject(Player1.class);
            if (collision != null) 
            {
                SpaceWorld space = (SpaceWorld)getWorld(); //gets the space world

                space.removeObject(collision);
                
                getWorld().removeObject(Enemies.class);
                
            }
        }
    }
    
    public void Player2Collision()
    {
        if (getWorld() != null)
        {
            Player2 collision = (Player2) getOneIntersectingObject(Player2.class);
            if (collision != null) 
            {
                SpaceWorld space = (SpaceWorld)getWorld(); //gets the space world

                space.removeObject(this);
                
                getWorld().removeObject(Enemies.class);
                
            }
        }
    }
}
danpost danpost

2014/4/17

#
Lines 33 and 50 are trying to remove an actor object, but all you are giving it is a class. What actor are you trying to remove from the world on those lines? if an Enemies object, then which Enemies object?
danpost danpost

2014/4/17

#
Why is it coded that when a Player1 object touches the bomb, the player is removed; and, when a Player2 object touches the bomb, the bomb is removed? I am not sure that is what you intended.
Neymar.Jr Neymar.Jr

2014/4/17

#
I'm trying to remove all the enemies under the class enemies.
Neymar.Jr Neymar.Jr

2014/4/17

#
I intend that if player 1 or 2 touches the bomb it is removed then removes/destroys all the enemies on the screen
danpost danpost

2014/4/17

#
Then line 48 should be:
space.removeObject(collision);
and lines 33 and 50 should be:
space.removeObjects(space.getObjects(Enemies.class));
Notice the 's' in 'removeObjects'. This method requires a list of objects to remove, which 'getObjects' provides.
danpost danpost

2014/4/17

#
Maybe you meant 'it' as being "the bomb" (not the player) 'is removed then removes/destroys all the enemies...'. In which case, change both lines 31 and 48 to:
space.removeObject(this);
Neymar.Jr Neymar.Jr

2014/4/17

#
Thanks it works great but I have another question which is how to make it appear at different places and at random times through out the game.
danpost danpost

2014/4/17

#
You will need an instance int field in your subclass of world to be a timer for spawning bombs. As long as it is at zero, nothing will happen. You can give it a random value to count down from immediately or have events that occur within your game trigger the random setting of the field. Only when the counter goes from one to zero will you spawn a bomb. The code for that (using 'timer' as the int field name) will be something like this:
if (timer > 0)
{
    timer--;
    if (timer == 0) 
    {
        // code spawning a bomb
    }
}
In the code for spawning a bomb, get random values for the x and y coordinates of the location to spawn. Use 'Greenfoot.getRandomNumber' for the random portion of the values. You may want to add a set value to the timer along with the random portion when setting it. This will give a minimum time between spawnings. For example, if you wanted the spawn to occur between 20 and 60 seconds after setting the timer, then:
int minTime = 20;
int maxTime = 60;
int timeRange = maxTime-minTime;
int fps = /** frames per second your scenario is running at */;
// set timer with
timer = fps*(minTime+Greenfoot.getRandomNumber(timeRange));
At normal scenario speeds (speed slider around the middle), the frames per second is between 50 and 60. You can adjust the value of the 'fps' field as needed.
Neymar.Jr Neymar.Jr

2014/4/18

#
Thanks but can you give me the getrandomnumber code please because I'm not sure how to do it.
Neymar.Jr Neymar.Jr

2014/4/18

#
Also for the timer = fps*(minTime+Greenfoot........ when I compile it it says identifier expected
danpost danpost

2014/4/18

#
You need to declare the timer field at the top of the class:
private int timer;
Neymar.Jr Neymar.Jr

2014/4/18

#
I did but i keep getting the same error
danpost danpost

2014/4/18

#
You will need to show the current class code and post the entire error message.
Neymar.Jr Neymar.Jr

2014/4/18

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.GraphicsEnvironment;
import java.awt.Font;

/**
 * Write a description of class SpaceWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SpaceWorld extends World
{
    GreenfootSound backgroundMusic = new GreenfootSound("Step Up Revolution.mp3");
    int minTime = 20;
    int maxTime = 60;
    int timeRange = maxTime-minTime;
    int fps = 60;
    private int timer; 
    timer = fps*(minTime+Greenfoot.getRandomNumber(timeRange));


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

    }

    //starts music when game execution starts
    public void started()
    {
        backgroundMusic.playLoop();
    }
    int baseX = 0;
    // 'bgi' stands for BackGroundImage
    GreenfootImage bgi = new GreenfootImage("1.jpg"); 
    int bgiWidth = bgi.getWidth();

    public void act()
    {
        baseX = (baseX + bgiWidth - 1) % bgiWidth;
        update();

        if (timer > 30)
        {
            timer--;
            if (timer == 0) 
            {
               Greenfoot.getRandomNumber(360) < 20)

            }
        }
        
         if (Enemies.class != null)
        {
            World world;
            world = getWorld();
            SpaceWorld spaceworlde = (Spaceworld)world;
           
            
            int x = 100+Greenfoot.getRandomNumber(500);  
            int y = 100+Greenfoot.getRandomNumber(500);  
            Enemies.class.setLocation(x, y); 
            return;
        }    
    }

    private void update()
    {
        if (baseX != 0) getBackground().drawImage(bgi, baseX - bgiWidth, 0);
        if (baseX < getWidth()) getBackground().drawImage(bgi, baseX, 0);
    }

    /**
     * Constructor for objects of class SpaceWorld.
     * 
     */
    public SpaceWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(900, 386, 1, false);
        update();
        backgroundMusic.setVolume(65);

        GreenfootImage img = new GreenfootImage(900,386);

        setBackground("1.jpg");

        addStars(500);

        prepare();
    }

    public void addStars(int howMany)
    {
        for(int s=0; s<howMany; s++) {
            int x = Greenfoot.getRandomNumber( getWidth() );
            int y = Greenfoot.getRandomNumber( getHeight() );
            addObject( new Star(),x,y);

        }   
    }   
The error message is "identifier expected" and is says to add an identifier next to the "r" in timer in the code of timer = fps*(minTime+Greenfoot.getRandomNumber(timeRange));
There are more replies on the next page.
1
2
3