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

2012/6/15

Start command is weird. Help!

Zamoht Zamoht

2012/6/15

#
This is my world code:
public myWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(6, 6, 60); 

        setPaintOrder(Introduction.class, CompleteScreen.class, ResetLevel.class, Player.class, Rock.class, Orange.class, Grass.class);
        
        prepare();
        
        Greenfoot.start();
    }
The problem is that Greenfoot.start() doesn't work all the time. The start/pause button shows "Pause" when i compile, but the actors only act two out of three times. Sometimes it works if i use the reset button or if i compile again. So is this a bug or am i doing something wrong. I'm not sure if "public myWorld" is the right place to put the code, or if this is even possible. Oh, I forgot to mention that what I want the program to do is to "run" on execute, so the user doesn't need to click the run button. Thank you for your time.
danpost danpost

2012/6/15

#
You show your world constructor; but, it calls the 'prepare' method, which you did not show. The code given appears to be fine, but cannot say anything about the 'prepare' method which is also executed during world creation.
davmac davmac

2012/6/16

#
What version of Greenfoot do you have? Have you uploaded your scenario somewhere?
Zamoht Zamoht

2012/6/16

#
The prepare isn't the problem since i've tried to run without it and no luck there, but here is the code.
private void prepare()
    {
        Introduction introduction = new Introduction();
        addObject(introduction, getWidth()/2, getHeight()/2);
    }
I'm using 2.2.1 and no i haven't uploaded the scenario. If you need more information to solve the problem I could upload it, but since the program isn't done at all I didn't feel like doing it. Okay since the world creates an other object when executed. Here is the code.
public class Introduction extends Actor
{
    public static final float FONT_SIZE = 24.0f;
    public static final int WIDTH = 500;
    public static final int HEIGHT = 500;
    
    private int levelIntro = 0;
    /**
     * Act - do whatever the Introduction wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        if (Greenfoot.mouseClicked(null))
        {
            levelIntro = ((Puzzle) getWorld()).level;
            ((Puzzle) getWorld()).level(levelIntro);
        }
    }    
    
    public Introduction()
    {
        makeImage("Use WASD to move.", "Oranges are movable.", "Rocks aren't movable.", "Reach the grass to win!", "Click anywhere to continue.");
    }
    
    private void makeImage(String move, String orange, String rock, String grass, String click)
    {
        GreenfootImage image = new GreenfootImage(WIDTH, HEIGHT);

        image.setColor(new Color(0, 0, 0, 160));
        image.fillRect(0, 0, WIDTH, HEIGHT);
        image.setColor(new Color(255, 255, 255, 100));
        image.fillRect(0, 0, WIDTH, HEIGHT);
        Font font = image.getFont();
        font = font.deriveFont(FONT_SIZE);
        image.setFont(font);
        image.setColor(Color.WHITE);
        image.drawString(move, 60, 90);
        image.drawString(orange, 60, 160);
        image.drawString(rock, 60, 230);
        image.drawString(grass, 60, 300);
        image.drawString(click, 60, 370);
        setImage(image);
    }
}
And please don't mind the weird actors.
Zamoht Zamoht

2012/6/16

#
Here is my scenario. It doesn't seem to bug on this page, but i wonder why it doesn't work in Greenfoot and why it works here. I tested the code on the wombats scenario and there it works fine, so guess I messed up somewhere. Any ideas? As you can see I just started programming, but I have to start somewhere right?
danpost danpost

2012/6/16

#
I looked at your code, and did not find anything that would cause this behaviour (though I did have to go to the Task Manager and force close it, once). A couple of minor changes would/might help. Change 'null' in line 14 of the Introduction code above to 'this' (this change should not affect anything, but since the object covers the whole world, it certainly would not hurt). The second change is in the CompleteScreen class. Replace the 'setImage(image)' statement with the following:
GreenfootImage baseImage = new GreenfootImage(WIDTH + 60, HEIGHT + 60);
baseImage.drawImage(image, 60, 60);
setImage(baseImage);
This will center the image in the window.
Zamoht Zamoht

2012/6/16

#
Thank you very much! I tried some different things, and I found out that the start() works fine with objects like the wombat from the tutorial scenario though it acts weird when I add "if" to the code like here.
public void act()
    {
        if (Greenfoot.mouseClicked(this))
        {
            level = (( Puzzle) getWorld()).level;
            ((Puzzle) getWorld()).level(level);
        }
    }
I think the problem is that the program doesn't react when I click my mouse. Now, the weird thing is that this doesn't happen all the time. I have updated my scenario here, so you can (if you want of course) see for yourself. Note the wombat running around in the background, while you can't click the introduction in the foreground. (As I mentioned the start() works fine when i use it in the wombats/tutorial scenario, but start() acts weird when I add it to the balloons scenario.) Thank you for your time.
You need to login to post a reply.