Download presentation
Presentation is loading. Please wait.
Published byOscar Owen Modified over 9 years ago
1
Animation Mrs. C. Furman
2
Animation We can animate our crab by switching the image between two pictures. crab.png and crab2.png
3
Greenfoot images We create new images by using the GreenfootImages class. new GreenfootImage(“crab2.png”); This file must be saved in the scenario’s images folder.
4
How Do Actors Get Images? Images are assigned to classes when they are created. Every object created from the class will receive a copy of the image. This image can be changed Each actor can decide to change it’s image.
5
Exercise Check the documentation of the Actor class. There are two methods that allow us to change an actor’s image. What are they called, and what are their parameters? What do they return?
6
this.setImage (new GreenfootImage (“crab2.png”)); We can use the above method to set the image to something new. Two things are happening… A new GreenfootImage is created This new image is being set as the image for the actor.
7
Setting image in Two Lines It is better for us to separate the previous statement into 2. This way we can better switch between the 2 images.
8
Storing Created Images We will need to store the 2 images that we will be switching between We will need variables.
9
Variables They are named memory locations. They are used to store values or objects. We will be creating a type of variable called an instance variable, or field Instance Variables are a bit of memory that belongs to an object. As long as the object exists and can be referenced, the instance variables exist.
10
Identifiers When we name variables we have to follow these rules. Choose names that: 1.Start with a letter 2.Contain only letters, numbers, underscores or the $ symbol 3.Can not be a keyword
11
Variable Identifiers Variable names (identifiers) should clearly describe what we are storing. Use image1, rather than x.
12
Access Specifiers There are 3 kinds of access specifiers, we will discuss 2 of them public – other objects have access to these value and methods private – only this class has access to the values and methods.
13
Instance Variables For the most part, we will make our instance variables private. Our methods up until this point have been public
14
Data Types We also need to know data types when declaring a variable There are 4 primitive types you need to know: int, double, boolean, char Everything else is stored in an object.
15
int stores positive and negative whole numbers and 0
16
double stores real numbers (decimal numbers and integers)
17
boolean true or false
18
char a single character (letter, digit, symbol) char literal – a character enclosed in a single quote. char variables can be assign char literals or numbers
19
Storing our Image Our image is not a primitive, it is a class The type of our instance data is GreenfootImage So…
20
What’s the line? private GreenfootImage image2; Before we add this to Crab, right click on Crab and select inspect This will tell us what variables are already in the Crab object. What variables do you find?
21
Look at Crab Code Do you see WALKING_SPEED declared in Crab? Where is it? Why is it showing up in Crab?
22
Where do we put instance variables??? Instance variables are always listed first in a class. Right after the class header and before the Constructors and methods. This is a convention that Java programmers follow
23
Add Instance Variables to Crab Above the act method add: private GreenfootImage image1; private GreenfootImage image1; private GreenfootImage image2; private GreenfootImage image2; Recompile and right click to inspect the class again. Are these new variables added?
24
Null What are image1 and image2 set to? Null – means that the variables have been declared, but NO objects have been created. We will need to create GreenfootImage objects to be store in image1 and image2
25
Assignments = This is the assignment operator in Java The variable goes on the LEFT always, and the value being assigned goes on the RIGHT. This should not be confused with == which we use to compare two things to see if they are equal
26
Assignments variable = expression; image1 = new GreenfootImage (“crab.png”); image2 = new GreenfootImage (“crab2.png”); We need to put this code into a Constructor method of Crab Constructors main job is to give values to instance data.
27
Create a Constructor Constructors have the same name as the class Constructors have NO return type
28
Above Act and After instance Data write… public Crab() { image1 = new GreenfootImage (“crab.png”); image1 = new GreenfootImage (“crab.png”); image2 = new GreenfootImage(“crab2.png”); image2 = new GreenfootImage(“crab2.png”); this.setImage (image1); this.setImage (image1);}
29
Compile and Inspect Inspect Crab again What are the value of image1 and image2?
30
if..else statements We have looked at if statements.. if (something is true) { do something } { do something } What if the something is false???
31
if.. else if..else provides us with something to do if the condition is false. if (condition) { execute this if condition is true } else { execute this if condition is false }
32
What Do we want to do?? if (the current image is image 1) { change the image to image2 } else { change the image to image1 } The above is pseudocode.
33
.equals( ) When we want to see if 2 objects are the same, we must use.equals() this.getImage ().equals (image1) Current Imageimage1 variable
34
GreenfootImgage getImage() This method returns the current image if (this.getImage().equals(image1)) { this.setImage(image2); this.setImage(image2);}else{ this.setImage (image1); this.setImage (image1);}
35
method toggleImage Write a new method in Crab called toggleImage() What is the return type of this method? The body should be our if.. else statement Be sure to call this.toggleImage() in the act() method. Compile and run… what happens?
36
toogleImage method public void toggleImage() { if (this.getImage().equals(image1)) if (this.getImage().equals(image1)) { this.setImage(image2); this.setImage(image2); } else else { this.setImage (image1); this.setImage (image1); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.