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

2014/8/8

How can I get my snowmen to be lined up down the right hand side of the screen instead of at the top?

m.davies m.davies

2014/8/8

#
public NewWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 

        prepare();

        addObject(new Car1(),400,200);
        for(int i=1;i<10;i++){
            addObject (new Snowman (),80*i, 30);}

    }
Super_Hippo Super_Hippo

2014/8/8

#
addObject (new Snowman(), getWidth()-30, 60*i);
m.davies m.davies

2014/8/10

#
Wonderful, thanks
m.davies m.davies

2014/8/10

#
I would also like my snowmen to move to across the screen to the left, instead of down. Could you help with this please.
if (Greenfoot.getRandomNumber (4) ==2){
            setLocation (this.getX(),this.getY()+2); 
m.davies m.davies

2014/8/10

#
Oh, I;ve got it now. I worked it out experimenting with the code you gave me in the last reply. Thanks
setLocation (this.getX()-2,this.getY()); 
Super_Hippo Super_Hippo

2014/8/10

#
The world is built up as a coordinate system. The top left corner is (0,0) which means x=0 and y=0. The x-coordinate increases to the right, the y-coordinate increases to the bottom. So as you got it, decreasing the x-coordinate will let your snowmen move to the left side.
m.davies m.davies

2014/8/10

#
Thanks, I think I get it. Could I just ask you to explain the for loop for me? I know I am using it but I'm not confident of my understanding about how it works. My first code for along the top didn't use getWidth. Then the getWidth was used for down the side. I don't understand completely how it works yet?
Super_Hippo Super_Hippo

2014/8/10

#
'getWidth()' returns the width of the world. In your world, which you created with 'super(800, 600, 1);', the width is 800. So 'getWidth()-30' is the same as 800-30=770. If you should change the size of your world, then your snowmen will be on the same position relative to the right side if you use 'getWidth()-30' instead of 770.
m.davies m.davies

2014/8/10

#
I have inspected the code and I can see how it knows to increment the placement of the snowmen at 60 each time. But the how does it know to place the object at the Y of 770? Oh I see what the code means. It is get the width of the screen and then - 30. Sorry to take you through each step with my thinking, but it is helping!
m.davies m.davies

2014/8/10

#
My security is stopping me from opening up the scenarios from the Greenfoot site. Perhaps you could help to point me in the right direction to solve this?
danpost danpost

2014/8/10

#
The 'for' loop is explained here in the Java tutorials.
danpost danpost

2014/8/10

#
Go to your Control Panel and open up the Java console to change your Java security setting to Medium. There is also a checkbox to allow java content to be run in your browser. Make sure it is checked also.
You need to login to post a reply.