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
how do I keep adding the same object every 120 pixels downwards
By Orko, with 1 reply.
Replied to by SPower, over 9 years ago:
You could use a for-loop for that, instead of writing it all out in separate lines. And please, use the code tags next time instead of bold tags.
Using getX & getY in world?
By idk1234, with 119 replies.
Last reply by davmac, over 9 years ago:
what do you mean by initialised? Do you mean this?
No. Those statements
declare
the variables, but do not initialise them (give them a value). So, they get the default value for reference variables, which is null (essentially meaning "no object"). That is why you later get an exception - you're trying to call setLocation on no object. It is difficult to help you because it's not clear what class the "respawn" and "respawnLocation" methods are in. If it is in the Virus class, you don't need to call setLocation on
another
object. If its in some other class,
Side Scrolling World
By AreebRaza, with 2 replies.
Last reply by AreebRaza, over 9 years ago:
Appreciate for the reply but i finally found a scenario that suits my scenario.
making objects fall down
By dearhart18, with 1 reply.
Replied to by valdes, over 9 years ago:
Check out my
Glutton Babies
. Class Comida and subclasses are the falling down objects int the act() method of my World is the random creation
Help with understanding getOneIntersectingObject
By Bat8, with 2 replies.
Last reply by Bat8, over 9 years ago:
SPower wrote...
The getOneIntersectingObject returns one of the objects that is currently intersecting the actor calling the function. If many enemies are touching your actor, then, it would only return one of those. If, however, there are no objects currently intersecting it, then it cannot return an object, so it returns null. Null is the type for 'no object', you could say. So, the if-statment is to check if the function does not return null, i.e. if it does return an object.
Thank you! I was looking for an explanation on the internet. All of them were very long and complicated and
Spawn new items after this items disappear
By Vojin, with 1 reply.
Replied to by SPower, over 9 years ago:
I'm assuming that at some point you do one of these things: <Code Omitted>You could simply add a line before doing this: <Code Omitted>It is important that if you use option 3, you add the new object
before
removing the object itself. because after it is removed, getWorld() will return null.
Delay spawn of objects when holding down a specific key?
By Sonarwave, with 6 replies.
Last reply by SPower, over 9 years ago:
I had put a comment in the code I posted, which explained that fireBullet was referring to the code you posted. But I'm glad it works now :)
Making an enemy character stay on the screen until killed.
By xlRenn, with 2 replies.
Last reply by valdes, over 9 years ago:
your variable movespeed, looks something like this: <Code Omitted> int your act() method or in other method called by act() <Code Omitted>
Scaling an Image
By ZacBuzzsaw, with 3 replies.
Last reply by davmac, over 9 years ago:
Turns out the image rescales when the run button is pressed
That indicates you have the code in an act() method. You should instead put it in the constructor for the actor.
Changing Smoke Image
By KatLady325, with no replies.
I have a game of Breaking Bricks I am working on. Ive been trying to change the image of the smoke every time the ball hits a brick, and I really have no clue how. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Ball extends Actor { private final int MAX_SPEED = 5; private int deltaX; // x movement speed private int deltaY; // y movement speed private int count = 2; private boolean stuck = true; // stuck to paddle private Brick brick; public void act() { if (!stuck && !((Board) getWorld()).getCheck()) { move(); makeSmoke(); checkOut(); } } private void move() { setLocation (getX() + deltaX, getY() + deltaY); checkPaddle(); checkBrick(); checkWalls(); } private void checkPaddle() { Actor paddle = getOneIntersectingObject(Paddle.class); if (paddle != null && getY() < paddle.getY()) { deltaY = -deltaY; int offset = getX() - paddle.getX(); deltaX = deltaX + (offset/10); if (deltaX > MAX_SPEED) { deltaX = MAX_SPEED; } if (deltaX < -MAX_SPEED) { deltaX = -MAX_SPEED; } Greenfoot.playSound("Pad.wav"); } } private void checkBrick() { brick = (Brick)getOneIntersectingObject(MetalBrick.class); if(brick==null){ brick = (Brick)getOneIntersectingObject(NormalBrick.class); } if(brick != null){ if(getY() > brick.getY()){ setLocation(getX(),getY()+2); deltaY = -deltaY; } else if(getY() < brick.getY()){ setLocation(getX(),getY()-2); deltaY = -deltaY; } else{ deltaX = -(deltaX+1); } if(brick instanceof MetalBrick){ Greenfoot.playSound("Bop.wav"); MetalBrick metalBrick = (MetalBrick)brick; metalBrick.decreaseCount(); if(metalBrick.isZero()){ metalBrick.remove(); ((Board) getWorld()).breakBrick(); } else{ metalBrick.updateImage(); } } if(brick instanceof NormalBrick){ Greenfoot.playSound("Bop.wav"); NormalBrick normalBrick = (NormalBrick)brick; normalBrick.decreaseCount(); if(normalBrick.isZero()){ normalBrick.remove(); ((Board) getWorld()).breakBrick(); } } } } private void checkWalls() { if (getX() == 0 || getX() == getWorld().getWidth()-1) { deltaX = -deltaX; } if (getY() == 0) { deltaY = -deltaY; } } private void checkOut() { if (getY() == getWorld().getHeight()-1) { ((Board) getWorld()).ballIsOut(); getWorld().removeObject(this); } } public void moveLR(int dist) { setLocation (getX() + dist, getY()); } public void moveUD(int dist) { setLocation (getX(), getY() + dist); } private void makeSmoke() { count--; if (count == 0) { getWorld().addObject ( new Smoke(), getX(), getY()); count = 2; } } public void release() { deltaX = Greenfoot.getRandomNumber(11) - 5; deltaY = -MAX_SPEED; stuck = false; } } import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Smoke extends Actor { private GreenfootImage image; // the original image private int fade; // the rate of fading public Smoke() { image = getImage(); fade = Greenfoot.getRandomNumber(4) + 1; // 1 to 3 if (fade > 3) { fade = fade - 2; } } public void act() { shrink(); } private void shrink() { if(getImage().getWidth() < 10) { getWorld().removeObject(this); } else { GreenfootImage img = new GreenfootImage(image); img.scale ( getImage().getWidth()-fade, getImage().getHeight()-fade ); setImage (img); } } } (very new to coding,sorry if this is unnecessary)
How can I build in a character choice
By JustQuinty, with no replies.
So I'm making a game for school, the only thing I did so far is build a world and I created two actor which I want to choose between. I will be adding other actors later on. Can anyone give me some advice?
Editing a second level
By kgorman, with 2 replies.
Last reply by kgorman, over 9 years ago:
Works great! Not sure why though, but I'll deal with that later - thank you!
Why Greenfoot wont let me publish my game :(
By AngelusNeko13, with 2 replies.
Last reply by AngelusNeko13, over 9 years ago:
thanks, ill see what i can do
Issues with player-controlled character falling through platforms
By jphb12, with 1 reply.
Replied to by danpost, over 9 years ago:
You must determine the direction that R2D2 is "falling" (up or down) when making contact with a platform to figure out whether he should stop below or above the platform (if moving up, then stop under the platform; if moving down, stop to stand on the platform). My
Jump and Run Demo w/Moving Platform
scenario shows how this is accomplished in the Who class.
Having a problem with changing the world.
By unpeaceful, with 5 replies.
Last reply by unpeaceful, over 9 years ago:
danpost wrote...
unpeaceful wrote...
It is an array of strings, is there any way to convert strings to a world or even the other way around?
You would have to do that systematically, one check at a time. Java now supports strings in 'switch' statements. You could possibly do something like this: <Code Omitted>If your java does not yet support strings in 'switch' statements, you will need to use 'if' statements for each case. Line 10 can be uncommented to show what an objet turned to a string would look like.
Thank you so much! I've been stressing over this for a whil
462
463
464
465
466
467
468
X