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/7

#
How do i make letters randomly spawn from the top of the screen and drop down to the bottom? They should be random letters and when typed should be removed....Help?
danpost danpost

2013/6/7

#
What aspect of this are you having trouble with and what code have you tried?
alagon alagon

2013/6/8

#
Well i have tried creating the letters with a random spawner, they just bug out. The movement is fine...it's just the spawning of the letters and that they should be a specific letter and stay that specific letter. I have done nothing so far to try and remove the letters.
danpost danpost

2013/6/8

#
danpost wrote...
What code have you tried?
It is difficult to correct code without seeing it. Also explain how it bugs out trying to create the random letters.
alagon alagon

2013/6/9

#
public void act() 
    {
        long currentTime = System.currentTimeMillis();  
        elapsedTime += currentTime - lastTime;  
        lastTime = currentTime;
       if (elapsedTime / 100 > 25)  
       {
           GreenfootImage img = new GreenfootImage(100, 50);  
        img.setColor(Color.BLACK); //Change this to the colour of text that you want  
        float fontSize = 30.0f; //Change this to the font size that you want  
        Font font = img.getFont().deriveFont(fontSize); //This adjusts the default font to the correct size.  
        img.setFont(font);  
        final int letter = Greenfoot.getRandomNumber(26);
        String[] alpha = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O",
                "P","Q","R","S","T","U","V","W","X","Y","Z"};
        str = alpha[Greenfoot.getRandomNumber(alpha.length-1)];
    }
       setLocation(getX(), getY()+1);
danpost danpost

2013/6/9

#
The only thing the 'act' method should have in it is the movement code and the removal code if it reaches the bottom of the window. The constructor should determine the letter and you should have an instance String or char field to hold its letter (so you have something to compare the input to). Not knowing exactly what you wanted, it appears you want the object to wait two and a half seconds while moving down the screen before displaying its letter (I fear that is not what you wanted). If you require more assistance, please explain what is supposed to happen with the creation, the behavior after creation, etc.; and explain what the purpose of tracking the elapsed time is supposed to be doing.
alagon alagon

2013/6/10

#
So what' supposed to happend, is that the letter is suposed to spawn with the bomb, and when the letter is typed, then the bomb is supposed to explode. The bomb and letter drop down to the bottom of the screen.
danpost danpost

2013/6/10

#
Is the letter on the bomb or is the letter a separate object that has to drop with the bomb? It would be easier if the letter was placed (drawn) directly on the image of the bomb. That way you do not have to work with two different objects; placing them into the world and removing them, as well as having to 'tie' each bomb to a specific letter and moving them together.
alagon alagon

2013/6/11

#
Yes, exactly! How do i do that? And also how do i do the destruction?
danpost danpost

2013/6/11

#
I would start with a minimum of the following two fields for the bomb class:
private static final String letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private String letter;
// then in the constructor, select a letter with
letter = ""+letters.charAt(Greenfoot.getRandomNumber(26));
// 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)
getImage().drawImage(image, xOff, 15); // puts letter on bomb
The only thing the 'act' method should do is: (1) make the bomb fall; (2) check for letter pressing; and, (3) check for bottom edge of world. Basically:
public void act()
{
    setLocation(getX(), getY()+1); // fall
    if (!Greenfoot.isKeyDown(letter)) // check correct letter pressed
    {
        // create explosion
        // possibly increment a counter of exploded bombs
        getWorld().removeObject(this);
    }
    if (getWorld() != null && getY() == getWorld().getHeight()-1) // check world edge
    {
        // possibly increment a counter of non-exploded bombs
        getWorld().removeObject(this);
    }
}
I will explain the destruction (or explosion) in my next post.
danpost danpost

2013/6/11

#
I believe that this asteroid scenario has an Explosion class that you can use. You not only need the class code, but also the image and the sound for the explosion from that scenario. Then, line 6 above only need add a new Explosion object into the world:
getWorld().addObject(new Explosion(), getX(), getY());
Line 11 in my fist code post above should have had '15' replaced with 'yOff'.
alagon alagon

2013/6/11

#
So the first part of the post i should put in the world class? The second bit and third bit worked for me thanks :D
danpost danpost

2013/6/11

#
Everything given above should go in the Bomb class.
alagon alagon

2013/6/12

#
Wouldn't that then overwrite the image of the bomb?
danpost danpost

2013/6/12

#
It will not overwrite, but draw on the image. Except for the letter itself, the image being drawn is transparent (which does not change the image being drawn on).
There are more replies on the next page.
1
2
3