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

2013/6/7

Letters

1
2
3
alagon alagon

2013/6/12

#
Alright i shall implement this code and post the results here...
danpost danpost

2013/6/12

#
A slight improvement is to remove the following line:
private static final String letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
and set the 'letter' field with:
letter = ""+((char)(Greenfoot.getRandomNumber(26)+65));
alagon alagon

2013/6/13

#
Here is the code for the bomb...it doesn't appear to work...saying that getImage needs a return type...

public class Bomb extends Actor
{ 
int animation = 1;  
private String letter = ""+((char)(Greenfoot.getRandomNumber(26)+65));
// then in the constructor, select a letter with 
// followed by drawing the letter on the bomb  
int fontsize = 36; // fontsize of letter (adjust at will)  
Color white = Color.white, trans = new Color(0, 0, 0, 0);  
GreenfootImage image = new GreenfootImage(letter, fontsize, white, trans); // creates letter image  
int xOff = 25; // horizontal letter placement on bomb (adjust at will)  
int yOff = 15; // vertical letter placement on bomb (adjust at will)  
 // puts letter on bomb 
getImage().drawImage(image, xOff, 15);

public void act()  
{  

    if (getY()<520) 
    {
           
            setImage("Missile/"+ animation +".png");
            setLocation(getX(), getY()+1);  
            animation++;
            if(animation ==12)
            {
                animation=1;
            }   
            if (!Greenfoot.isKeyDown(letter)) // check correct letter pressed  
        {  
            // create explosion   
            getWorld().removeObject(this);
        }
    }  
    else
    {  
        {   
            setImage("Explosion/"+ animation +".png"); 
            animation++;
            if (animation == 25)
            {
                getWorld().removeObject(this);
            }
        } 
        
    }  
    
}
}
danpost danpost

2013/6/13

#
A lot of what I gave you was supposed to go inside the constructor of the class:
public class Bomb extends Actor
{ 
    int animation = 1;
    private String letter = ""+((char)(Greenfoot.getRandomNumber(26)+65));

    public Bomb()
    {
        int fontsize = 36; // fontsize of letter (adjust at will)  
        Color white = Color.white, trans = new Color(0, 0, 0, 0);  
        GreenfootImage image = new GreenfootImage(letter, fontsize, white, trans); // creates letter image  
        int xOff = 25; // horizontal letter placement on bomb (adjust at will)  
        int yOff = 15; // vertical letter placement on bomb (adjust at will)  
        // put letter on bomb 
        getImage().drawImage(image, xOff, 15);
    }

    public void act()  
    {
        if (getY()<520) 
        {
            setImage("Missile/"+ animation +".png");
            setLocation(getX(), getY()+1);  
            animation++;
            if(animation ==12)
            {
                animation=1;
            }   
            if (!Greenfoot.isKeyDown(letter)) // check correct letter pressed  
            {  
                // create explosion   
                getWorld().removeObject(this);
            }
        }  
        else
        {  
            setImage("Explosion/"+ animation +".png"); 
            animation++;
            if (animation == 25)
            {
                getWorld().removeObject(this);
            }
        } 
    }  
}
Lines 6 through 15 is the constructor method within the Bomb class.
alagon alagon

2013/6/14

#
It is not able to find the "Color" in line 9...help?
danpost danpost

2013/6/14

#
Add the following import at the top of the class:
import java.awt.Color;
alagon alagon

2013/6/14

#
I added that to the class and now...it's not spawning any bombs at all...It appears to remove itself immediately...help?
danpost danpost

2013/6/14

#
Please post your current Bomb class code and the code in your world where the bombs are spawned.
alagon alagon

2013/6/14

#
Bomb Code
import greenfoot.*;
import java.awt.Color;

public class Bomb extends Actor  
{   
    int animation = 1;  
    private String letter = ""+((char)(Greenfoot.getRandomNumber(26)+65));  
  
    public Bomb()  
    {  
        int fontsize = 18; // fontsize of letter (adjust at will)    
        Color white = Color.GREEN, trans = new Color(0, 0, 0, 0);    
        GreenfootImage image = new GreenfootImage(letter, fontsize, white, trans); // creates letter image    
        int xOff = 25; // horizontal letter placement on bomb (adjust at will)    
        int yOff = 15; // vertical letter placement on bomb (adjust at will)    
        // put letter on bomb   
        getImage().drawImage(image, xOff, 15);  
    }  
  
    public void act()    
    {  
        if (getY()<520)   
        {  
            setImage("Missile/"+ animation +".png");  
            setLocation(getX(), getY()+1);    
            animation++;  
            if(animation ==12)  
            {  
                animation=1;  
            }     
            if (!Greenfoot.isKeyDown(letter)) // check correct letter pressed    
            {    
                // create explosion     
                getWorld().removeObject(this);  
            }  
        }    
        else  
        {    
            setImage("Explosion/"+ animation +".png");   
            animation++;  
            if (animation == 25)  
            {  
                getWorld().removeObject(this);  
            }  
        }   
    }    
}
World Code import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Background here. * * @author (your name) * @version (a version number or a date) */ public class Background extends World { private long lastTime = System.currentTimeMillis(); private long elapsedTime = 0; int counter = 0; public Background() { super(1000, 600, 1); setBackground("Back.png"); GreenfootSound backgroundMusic = new GreenfootSound("Theme.mp3"); backgroundMusic.playLoop(); } public void act() { long currentTime = System.currentTimeMillis(); elapsedTime += currentTime - lastTime; lastTime = currentTime; int x; if (elapsedTime / 100 > 12) { elapsedTime = 0; counter++; addObject(new Bomb(), Greenfoot.getRandomNumber(getWidth()-100)+50, 0); if (counter == 15) { addObject(new Nuke(),Greenfoot.getRandomNumber(getWidth()-100)+50, 0); counter = 0; } } } }
alagon alagon

2013/6/14

#
I checked the code and the error appears to be on this line:
if (!Greenfoot.isKeyDown(letter)) // check correct letter pressed      
            {      
                // create explosion       
                getWorld().removeObject(this);    
            }  
Because if i removed it, the program would work.
danpost danpost

2013/6/14

#
Sorry, my bad. Change line 31 to:
if (Greenfoot.isKeyDown(letter))
(removing the exclamation point ('!').
alagon alagon

2013/6/14

#
The Letters don't show up T_T
danpost danpost

2013/6/14

#
The letters are being drawn; however, you need to adjust your 'xOff' and 'yOff' values so that the letter is centered over the main body of your bomb.
alagon alagon

2013/6/15

#
They are in the center of the bomb....yet they don't show up. If i manually spawn the bombs in, then the letters appear before i press "run".
danpost danpost

2013/6/15

#
Oh, the problem is you are rotating between 12 different 'missile' images for the bomb. You will need to draw the letter on all twelve images and store them in an array. Try the following for your Bomb class:
public class Bomb extends Actor
{ 
    int animation = 0;
    private String letter = ""+((char)(Greenfoot.getRandomNumber(26)+65));
    private GreenfootImage[] images = new GreenfootImage[12];
    private boolean exploding;

    public Bomb()
    {
        int fontsize = 36; // fontsize of letter (adjust at will)  
        Color white = Color.white, trans = new Color(0, 0, 0, 0);  
        GreenfootImage image = new GreenfootImage(letter, fontsize, white, trans); // creates letter image  
        int xOff = 25; // horizontal letter placement on bomb (adjust at will)  
        int yOff = 15; // vertical letter placement on bomb (adjust at will)  
        // put letter on bombs
        int i = 0;
        while (i < 11)
        {
            images[i] = new GreenfootImage("Missle/"+(i+1)+".png");
            images[i].drawImage(image, xOff, yOff);
            i = i + 1;
        }
        setImage(images[0]);
    }

    public void act()  
    {
        if (!exploding)
        {
            setLocation(getX(), getY()+1);  
            animation++;
            if(animation ==11)
            {
                animation=0;
            }   
            setImage(images[animation]);
            if (Greenfoot.isKeyDown(letter) ||  getY()>=520) 
            {  
                exploding = true;
                animation = 1;
            }
        }  
        else
        {  
            setImage("Explosion/"+ animation +".png"); 
            animation++;
            if (animation == 25)
            {
                getWorld().removeObject(this);
            }
        } 
    }  
}
There are more replies on the next page.
1
2
3