Greenfoot Flashcards
How do you register a key input?
public void movement() if (Greenfoot.isKeyDown("x")) { //code } x = right, left, up, down
How do you create and call on functions to public void act?
Make it = public void anthingYouWant() { //code }
Call on it in public void act = anythingYouWant();
How do you add movement?
public void movement() { if(Greenfoot.isKeyDown("right")) { setLocation(getx()+3,getY()); //or setRotation(); move(5); } if(Greenfoot.isKeyDown("left")) { setLocation(getX() -3,getY()); //or setRotation(180); move(5); } if(Greenfoot.isKeyDown("down")) { setLocation(getX(),getY()+3) //or setRotation(270); move(5); } if(Greenfoot.isKeyDown("up")) { setLocation(getX(),getY()-3)) //or setRotation(90); move(5); }
How to add hit detection? And how to make an object disappear.
if (isTouching(x.class)) { removeTouching(x.class); } x = name of the class you want to remove
How to get a random number generator (RNG).
move(Greenfoot.getRandomNumber(20)); turn(Greenfoot.getRandomNumber(180)-90); //these numbers can be changed
How to add sound?
Greenfoot.playSound(“pop.wav”);
pop = name of file
wav = file types, could be MP3 ect…
Very complex and annoying random movement
if (Greenfoot.getRandomNumber(2) < 1) { move(1); } if (Greenfoot.getRandomNumber(10)< 1) { turn(Greenfoot.getRandomNumber(90)-45); }
How do you add boarder collision?
if (getX() == 7) { turn(180); } if (getX() == 0) { turn(180); } if (getY() == 7) { turn(180); } if (getY() == 0) { turn(180); }
How do you add a scoring system?
EXAMPLE //In counter public x() { setImage(new GreenfootImage("0", 20, Color.WHITE, Color.BLACK)); }
public void x(int amount) { totalCount += amount; setImage(new GreenfootImage("" + totalCount, 20, Color.WHITE, Color.BLACK)); }
//In player if (isTouching(Iceberg.class)) { x counter =(x) getWorld().getObjects(x.class).get(0); counter.y(1);
}
What is the superclass?
It’s the main name and the route of other items
They are the:
“World”
“Actor”
What is the class?
The items in the code.
“Ants”
“Rockets”
“Points”
What is a private property?
A single of data that only one class cam use. E.g. totalCount; (For a counter)
What is a comment?
Anything with a /** before it and */ after
What is a method that runs each frame?
The “act()” method