This site requires JavaScript, please enable it in your browser!
Greenfoot
Username
Password
Remember Me?
Sign Up, Lost Password
Activity
About
Documentation
Download
Discuss
Scenarios
Discussions
You need to login to take part
Current Discussions
objects interaction
By noona, with 7 replies.
Last reply by danpost, over 10 years ago:
noona wrote...
excuse me but what exactly should i write inside the body of 'textFoundAtKeyPosition' method
danpost wrote...
All the 'textFoundAtKeyPosition' method needs to do is set the field to a value of 2.
That is it!
Reappear on the other side
By khoalabear, with 2 replies.
Last reply by khoalabear, over 10 years ago:
Thanks so much
it work fine but....
By divinity, with 2 replies.
Last reply by divinity, over 10 years ago:
thank you danpost, i got it to work. thanks sooooo much i have another one to post will do it tomorrow
Randomly adding Objects.
By lexboy112333, with 1 reply.
Replied to by danpost, over 10 years ago:
The code given is that which creates and controls the Worm object -- not that which call the (implicit) constructor of the class to create the Worm objects (which is where the code 'new Worm()' would be located).
Buggy Editor (after an Error appeared?)
By Skelot, with 5 replies.
Last reply by davmac, over 10 years ago:
Try disabling graphics hardware acceleration (see
here
). It's not much of a solution but if it helps, you'll know it's some problem with the graphics driver or with Java/graphics interaction.
Make an object rotate in direction of an other object
By MyHeartRocksNRolls, with 14 replies.
Last reply by ttg4l, over 10 years ago:
.
Playing Sound File Once and Stop
By smcgee, with 2 replies.
Last reply by smcgee, over 10 years ago:
<Code Omitted>
Your short lesson was exactly what I needed to understand why the scenario kept playing the sound file again and again. Thank you so much. It now works exactly as I want it to work.
Change the Background
By eve19, with 6 replies.
Last reply by danpost, over 10 years ago:
None of these key actions are ones that require multiple act cycles to process. I think the main issue is due to using the 'isKeyDown' method, which will continue to return a 'true' value for every act cycle the key is down. This is causing the image to change repeatedly until you release the key. For this, you may want to use the 'getKey' method instead, which returns 'true' upon the release of the key (which only happens once for every time the key is pressed). Then, it might look like this:
HELP, I can't figure out how to make my actor only jump once in the air!
By GameCreator, with no replies.
//here is my code, please help import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Welcome to the Walker (Robot) class! * You are the Robot! * * @author (Isaiah Williams) * @version (1.0) */ public class Walker extends Actor { private int vSpeed = 0; // here shows the vertical speed private int acceleration = 1; private int speed = 3; // This shows that when I write speed below it equals 3 private boolean jumping; /* * Here are my images that are for Walker when he walks left only below * Below I tell of synonyms for what I want the images to be called */ private GreenfootImage run0L = new GreenfootImage("tmp-0.png"); // here I show how a soon to be Greenfoot image,tmp-0.png, will be named run0L in Greenfoot language private GreenfootImage run1L = new GreenfootImage("tmp-1.png"); private GreenfootImage run2L = new GreenfootImage("tmp-2.png"); private GreenfootImage run3L = new GreenfootImage("tmp-3.png"); private GreenfootImage run4L = new GreenfootImage("tmp-4.png"); private GreenfootImage run5L = new GreenfootImage("tmp-5.png"); private GreenfootImage run6L = new GreenfootImage("tmp-6.png"); private GreenfootImage run7L = new GreenfootImage("tmp-7.png"); private GreenfootImage run8L = new GreenfootImage("tmp-8.png"); private GreenfootImage run9L = new GreenfootImage("tmp-9.png"); private GreenfootImage run10L = new GreenfootImage("tmp-10.png"); private GreenfootImage run11L = new GreenfootImage("tmp-11.png"); private GreenfootImage run12L = new GreenfootImage("tmp-12.png"); private GreenfootImage run13L = new GreenfootImage("tmp-13.png"); private GreenfootImage run14L = new GreenfootImage("tmp-14.png"); private GreenfootImage run15L = new GreenfootImage("tmp-15.png"); private GreenfootImage run16L = new GreenfootImage("tmp-16.png"); private GreenfootImage run17L = new GreenfootImage("tmp-17.png"); private GreenfootImage run18L = new GreenfootImage("tmp-18.png"); private GreenfootImage run19L = new GreenfootImage("tmp-19.png"); private GreenfootImage run20L = new GreenfootImage("tmp-20.png"); private GreenfootImage run21L = new GreenfootImage("tmp-21.png"); private GreenfootImage run22L = new GreenfootImage("tmp-22.png"); private GreenfootImage run23L = new GreenfootImage("tmp-23.png"); private GreenfootImage run24L = new GreenfootImage("tmp-24.png"); private GreenfootImage run25L = new GreenfootImage("tmp-25.png"); /* * Here are my images that are for Walker when he walks right only below * Below I tell of synonyms for what I want the images to be called */ private GreenfootImage run0R = new GreenfootImage("tmp-0R.png"); // here I show how a soon to be Greenfoot image,tmp-0R.png, will be named run0R in Greenfoot language private GreenfootImage run1R = new GreenfootImage("tmp-1R.png"); private GreenfootImage run2R = new GreenfootImage("tmp-2R.png"); private GreenfootImage run3R = new GreenfootImage("tmp-3R.png"); private GreenfootImage run4R = new GreenfootImage("tmp-4R.png"); private GreenfootImage run5R = new GreenfootImage("tmp-5R.png"); private GreenfootImage run6R = new GreenfootImage("tmp-6R.png"); private GreenfootImage run7R = new GreenfootImage("tmp-7R.png"); private GreenfootImage run8R = new GreenfootImage("tmp-8R.png"); private GreenfootImage run9R = new GreenfootImage("tmp-9R.png"); private GreenfootImage run10R = new GreenfootImage("tmp-10R.png"); private GreenfootImage run11R = new GreenfootImage("tmp-11R.png"); private GreenfootImage run12R = new GreenfootImage("tmp-12R.png"); private GreenfootImage run13R = new GreenfootImage("tmp-13R.png"); private GreenfootImage run14R = new GreenfootImage("tmp-14R.png"); private GreenfootImage run15R = new GreenfootImage("tmp-15R.png"); private GreenfootImage run16R = new GreenfootImage("tmp-16R.png"); private GreenfootImage run17R = new GreenfootImage("tmp-17R.png"); private GreenfootImage run18R = new GreenfootImage("tmp-18R.png"); private GreenfootImage run19R = new GreenfootImage("tmp-19R.png"); private GreenfootImage run20R = new GreenfootImage("tmp-20R.png"); private GreenfootImage run21R = new GreenfootImage("tmp-21R.png"); private GreenfootImage run22R = new GreenfootImage("tmp-22R.png"); private GreenfootImage run23R = new GreenfootImage("tmp-23R.png"); private GreenfootImage run24R = new GreenfootImage("tmp-24R.png"); private GreenfootImage run25R = new GreenfootImage("tmp-25R.png"); private int frame = 1; // Here I show a "frame" equals one private int animationCounter = 0; //Here I show how the animationCounter is zero by itself /** * Act - do whatever the Robot wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { /** * Here are constructors that I want activated in my World! */ checkKey(); // checkKey is used to check what keys are pressed animationCounter ++; // animationCounter is used to count the animations know which frame goew where checkFall(); } public void noMidJump() { if (isTouching(Platform.class)) { String key = Greenfoot.getKey(); // this allows the key to be in effect only once even when held in if ("z".equals(key)) { jump(); } } } public void fall() { setLocation( getX(), getY() + vSpeed); // this will allow Walker to fall by changing the Y cordinate by including the number incorporated with vertical speed if (vSpeed <=9) // 9 is the maximum speed Walker can move upwards/downwards { vSpeed = vSpeed + acceleration; // this shows that as Walker falls acceleration will be added to his fall every cycle // if just = 2;this shows that if Walker is falling he will fall 2 pixels per cycle } jumping = true; // this shows that you must be touching something in order to jump } public boolean onPlatform() { int WalkerHeight = getImage().getHeight(); // Distance from middle of Walker to ground int lookForPlatform = (int)(WalkerHeight/2) - 5 ; // (int) is there so there will be no decimal, - 5 is there because I want some less so it may be so many pixels above the character that will be in contat with the othe picture, + would be so many pixels above the platform/image. Actor Platform = getOneObjectAtOffset(0, lookForPlatform, Platform.class); if (Platform == null) { jumping = true;// this helps to control return false; // this shows if we are not on the ground it will return false } else { return true; // this shows if we are not on the ground it will return true } } public void checkFall() { if (onPlatform()) // if the method checkFallis true vSpeed is normal and fallinf stops for Walker { vSpeed = 0; } else // if the method checkFall is not true then the method fall will take action { fall(); } } public void checkKey() { if (Greenfoot.isKeyDown("Right")) // this shows if the gamer holds the right key the method, moveRight will be put to action moveRight(); if (Greenfoot.isKeyDown("Left")) // this shows if the gamer holds the left key the method moveLeft will be put to action moveLeft(); } public void jump() { vSpeed = -15; // allows for motion upwards fall(); // this allows to bring walker down } public void moveRight() { setLocation(getX()+speed, getY()); // we say positive speed, which is 3, so Walker walks left if (animationCounter % 2 == 0) // this allows the animation to slow, this says every 2 frames a slide of the next image will take place animateRight(); // this is the animation so it looks like Walker is walking to the right } public void moveLeft() { setLocation(getX()-speed, getY()); // we say minus speed, which is 3, so Walker walks left if (animationCounter % 2 == 0) // this allows the animation to slow, this says every 2 frames a slide of the next image will take place animateLeft(); // this is the animation so it looks like Walker is walking to the left } public void animateLeft() { /** * each frame will run a specific picture to make it seem like Walker is walking to the left, when the number of pictures run out the animation will start again. */ if (frame == 0) // here says if the frame is zero (the very first frame there is), image run0L, or tmp-0.png, will appear in place of Walker { setImage(run0L); } else if(frame == 1) // this continues the pattern as shown above... the next frame, 1, (the second frame), image run1L will appear in place of Walker { setImage (run1L); } else if(frame == 2) { setImage (run2L); } else if(frame == 3) { setImage (run3L); } else if(frame == 4) { setImage (run4L); } else if(frame == 5) { setImage (run5L); } else if(frame == 6) { setImage (run6L); } else if(frame == 7) { setImage (run7L); } else if(frame == 8) { setImage (run8L); } else if(frame == 19) { setImage (run9L); } else if(frame == 10) { setImage (run10L); } else if(frame == 11) { setImage (run11L); } else if(frame == 12) { setImage (run12L); } else if(frame == 13) { setImage (run13L); } else if(frame == 14) { setImage (run14L); } else if(frame == 15) { setImage (run15L); } else if(frame == 16) { setImage (run16L); } else if(frame == 17) { setImage (run17L); } else if(frame == 18) { setImage (run18L); } else if(frame == 19) { setImage (run19L); } else if(frame == 20) { setImage (run20L); } else if(frame == 21) { setImage (run21L); } else if(frame == 22) { setImage (run22L); } else if(frame == 23) { setImage (run23L); } else if(frame == 24) { setImage (run24L); } else if(frame == 25) { setImage (run25L); frame = 0; // this shows once frame 25 is reached, it equals frame 0 so it may restart this process of animation return; // just in case if there is an error, once frame 25 is reached the animation will go back to the beginning } frame ++; // this tells the frames are increasing numerically, least to greatest } public void animateRight() { /** * each frame will run a specific picture to make it seem like Walker is walking to the right, when the number of pictures run out the animation will start again. */ if (frame == 0) // here says if the frame is zero (the very first frame there is), image run0R, or tmp-0R.png, will appear in place of Walker { setImage(run0R); } else if(frame == 1) // this continues the pattern as shown above... the next frame, 1, (the second frame), image run1R will appear in place of Walker { setImage (run1R); } else if(frame == 2) { setImage (run2R); } else if(frame == 3) { setImage (run3R); } else if(frame == 4) { setImage (run4R); } else if(frame == 5) { setImage (run5R); } else if(frame == 6) { setImage (run6R); } else if(frame == 7) { setImage (run7R); } else if(frame == 8) { setImage (run8R); } else if(frame == 9) { setImage (run9R); } else if(frame == 10) { setImage (run10R); } else if(frame == 11) { setImage (run11R); } else if(frame == 12) { setImage (run12R); } else if(frame == 13) { setImage (run13R); } else if(frame == 14) { setImage (run14R); } else if(frame == 15) { setImage (run15R); } else if(frame == 16) { setImage (run16R); } else if(frame == 17) { setImage (run17R); } else if(frame == 18) { setImage (run18R); } else if(frame == 19) { setImage (run19R); } else if(frame == 20) { setImage (run20R); } else if(frame == 21) { setImage (run21R); } else if(frame == 22) { setImage (run22R); } else if(frame == 23) { setImage (run23R); } else if(frame == 24) { setImage (run24R); } else if(frame == 25) { setImage (run25R); frame = 0; // this shows once frame 25 is reached, it equals frame 0 so it may restart this process of animation return; // just in case if there is an error, once frame 25 is reached the animation will go back to the beginning } frame ++; // this tells the frames are increasing numerically, least to greatest } }
Make all instances of class do the same thing
By Collin, with 17 replies.
Last reply by danpost, almost 11 years ago:
danpost wrote...
I uploaded a demo scenario to show simple code for selecting a target location for a class of actors. It is
here
.
The linked scenario has been removed due to lack of interest.
Exporting games
By EggsD, with no replies.
I have a very large game made and would like to upload it here so that I can send the link to a possible employer. However the game is too big. Can I zip the image file? I've also tried to export the game to a jar but when transitioning between worlds of different sizes, the game window doesn't adjust as well. If anybody has a solution for either method, I would greatly appreciate it. Thanks!
Accessing a classes method Through getOneObjectAtOffset
By TheJohny, with 5 replies.
Last reply by danpost, almost 11 years ago:
TheJohny wrote...
You are the boss! Works perfectly, thanks alot :) I wasn't aware that it is possible to convert an Actor into a Class type
The bottom of
this page
of the java tutorials explains about explicit casting.
Making Objects Fall Slower(Gravity)
By jmeinicole, with 5 replies.
Last reply by danpost, almost 11 years ago:
You could slow down the scenario speed and make other moving actors move at a faster pace to compensate. However, there are better ways to accomplish what you want to do. It is not clear how to deal with it as it depends on exactly what speed or speeds you require for the actor. If you only need it to move like once in so many act cycles, it would be just a matter of adding a counter. If you require fractional speeds requiring more than a one in the numerator, then you will need two instance fields -- one for a more exact y-coordinate for the actor and the other for the speed.
No Compile button
By ansewanse, with 1 reply.
Replied to by TheJohny, almost 11 years ago:
You probably downloaded the Greenfoot 3.0 version which no longer has a compile button. The scenario gets automatically compiled whenever you make any change to your classes
Sudoku generation (speed and bugs)
By fejfo, with 3 replies.
Last reply by danpost, almost 11 years ago:
fejfo wrote...
Are you saying that I should first fill each cell with a random content and keep retrying until it works ? or should I take every row and put all the 9 different numbers in there and to that for all the rows ?
I am saying to work across each row from top to bottom and randomly place one of the untried possibilities in each cell. If you run out of possibilities in any cell, try another possibility of a previous cell. I did mine something like this:
497
498
499
500
501
502
503
X