Component 2 (Greenfoot) Flashcards
How to make an object move and turn
Go into public void act()
move();
turn();
Put the numbers in the move and turn brackets.
How to make an object turn at world’s edge
Use this function:
if(isAtEdge()){
Insert function here
}
How to make an object move randomly
Go into public void act()
{
if (Greenfoot.getRandomNumber(100)<10){
turn(Greenfoot.getRandomNumber(90)-45);
}
}
How to make an object delete/eat another object when there is a collision
if (isTouching(objectname.class)){
removeTouching(objectname.class);
}
How to make a sound when objects collide
Go into public void act()
if(isTouching(objectname.class)){
Greenfoot.playSound(“soundname.wav”);
}
How to move an object move when a key is pressed
Go into public void checkKeyPress(){
if(Greenfoot.isKeyDown(“Left)){
turn(-4);
}
Also, go into ‘act’ and add under move: checkKeyPress()
Continue in the same way, for turn (4) instead of -4.
How to add a counter to a world
Make a new class called counter, and type the code below:
public class Counter extends Actor { private int totalCount = 0;
public Counter() { setImage(new GreenfootImage("0", 20, Color.WHITE, Color.BLACK)); }
public void bumpCount(int amount) { totalCount += amount; setImage(new GreenfootImage("" + totalCount, 20, Color.WHITE, Color.BLACK)); } }
How to get the counter working in other classes
Go into the word class, and type the following code:
private Counter theCounter; public worldname() { super(8, 8, 60); setBackground("filename.jpg"); theCounter = new Counter(); addObject(theCounter, 0, 0); prepare(); } public Counter getCounter() { return theCounter; }
How to make the object delete, play a sound, and move the counter up by 1 every time it collides with another object
if(Iceberg!=null) { World world = (World)getWorld(); world.removeObject(Iceberg); Greenfoot.playSound("soundname.wav"); Counter counter = ocean.getCounter(); counter.bumpCount(1); }