Component 2 (Greenfoot) Flashcards

1
Q

How to make an object move and turn

A

Go into public void act()
move();
turn();

Put the numbers in the move and turn brackets.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to make an object turn at world’s edge

A

Use this function:

if(isAtEdge()){
Insert function here
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to make an object move randomly

A

Go into public void act()
{
if (Greenfoot.getRandomNumber(100)<10){
turn(Greenfoot.getRandomNumber(90)-45);
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to make an object delete/eat another object when there is a collision

A

if (isTouching(objectname.class)){
removeTouching(objectname.class);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to make a sound when objects collide

A

Go into public void act()

if(isTouching(objectname.class)){
Greenfoot.playSound(“soundname.wav”);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to move an object move when a key is pressed

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to add a counter to a world

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to get the counter working in other classes

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to make the object delete, play a sound, and move the counter up by 1 every time it collides with another object

A
if(Iceberg!=null)
{
    World world = (World)getWorld();
    world.removeObject(Iceberg);
    Greenfoot.playSound("soundname.wav");
    Counter counter = ocean.getCounter();
    counter.bumpCount(1);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly