Presentation is loading. Please wait.

Presentation is loading. Please wait.

Organizing Memory in Java

Similar presentations


Presentation on theme: "Organizing Memory in Java"— Presentation transcript:

1 Organizing Memory in Java
Arrays Organizing Memory in Java

2 What is an Array You can think of an Array like a container, it holds several things Previously when we worked with variables each variable represented ONE item (number, text, image, etc). Arrays are unique in that they are stored in a single contiguous block of memory

3

4 Declaring an Array int[] scores = new int[5];
Square brackets, [], tell Java that this is more than just an integer

5 Elements can be initialized at declaration:
Index/Subscript 84 93 97 78 62 score[1] score[3] score[4] score[0] score[2] Elements can be initialized at declaration: int scores[] = {78,84,62,93,97};

6 Computer Scientists count from 0
84 93 97 78 62 score[1] score[3] score[4] score[0] score[2] The first position is score[0] For efficiency, an array is the address of the first element. The number in brackets represents how far from the first item to move. score[4] means start at score and move over by 4 integers (arriving at the 5th)

7 length In Java, arrays have a handy property called length
You can find out how many items are in the array scores.length

8 Task #1 We’re going to play more with Greenfoot and use an Array to do it. Your task will be to bring a character to life with an animation

9 Finding an animation

10

11 Copy/Paste the URL

12 Getting the frames The process we are about to do is called “exploding” a gif There are free online sites that are usually the simplest option

13

14 Greenfoot Project Folder
Save your images in the images folder Keeps your work organized Allows you to refer to images by relative path rather than absolute

15 Other useful tasks

16 Animation with an array
Think of the Array like a filmstrip Each index holds 1 image in the animation As the “filmstrip” plays, individual frames make up a movie 1 2 3 4 5

17 Java private final static GreenfootImage[] IDLE = {
new GreenfootImage("MarioIdle (1).gif"), new GreenfootImage("MarioIdle (2).gif"), new GreenfootImage("MarioIdle (3).gif"), new GreenfootImage("MarioIdle (4).gif"), new GreenfootImage("MarioIdle (5).gif"), new GreenfootImage("MarioIdle (6).gif") }; 1 2 3 4 5 MarioIdle (1) MarioIdle (2) MarioIdle (3) MarioIdle (4) MarioIdle (5) MarioIdle (6)

18 Java private final static GreenfootImage[] IDLE = {
new GreenfootImage("MarioIdle (1).gif"), new GreenfootImage("MarioIdle (2).gif"), new GreenfootImage("MarioIdle (3).gif"), new GreenfootImage("MarioIdle (4).gif"), new GreenfootImage("MarioIdle (5).gif"), new GreenfootImage("MarioIdle (6).gif") }; private – hide the array from everyone but Mario (no one else needs it) final – a constant, don’t allow changes!  NOTICE THE ALL CAPS static – shared by all the Marios that are put in the game GreenfootImage[] – tells Java what we are keeping track of, an array of images IDLE – the name in PROPER style for a constant { contents, of, array, separated, by, commas };

19 Control You’ve probably never though of it before but something has to keep track of which frame to display We need a variable for Mario to remember 1 2 3 4 5 frame_

20 Java public class Mario extends Actor { //initialized in the class because it is static – shared by all Marios private final static GreenfootImage[] IDLE = {new GreenfootImage("MarioIdle (1).gif"), new GreenfootImage("MarioIdle (2).gif"), new GreenfootImage("MarioIdle (3).gif"), new GreenfootImage("MarioIdle (4).gif"), new GreenfootImage("MarioIdle (5).gif"), new GreenfootImage("MarioIdle (6).gif")}; private int frame_;//declare the things Mario needs to remember public Mario() frame_ = 0;//initialized in the constructor – belongs to a Mario } …

21 static demo private static int frame_ vs private int frame
Mario has 6 frames in his animation To help you see the bug of sharing (static) the frame_ variable I’ll put 6 Marios into the World

22 act() Greenfoot is a game engine with a simple rule
Every frame of the movie it asks everything in the game (Worlds/Levels, Actors/Characters, etc.) how it should behave It does this through the act() method public void act() Everyone gets a turn to act before the next frame is rendered

23 act() How should Mario act()? Update frame of animation
Display the current image

24 Update animation frame
Increment the frame number If you have gone past the end of the animation move the frame back to the beginning this will loop the animation frame_++; if( frame_ >= IDLE.length ) { frame_ = 0; }

25 If statements conditional statements allow you to tell Java when code should (and should not) be executed Syntax: if( condition ) { code to execute when condition is true (ignored when false) } Note indenting and braces {}

26 updateAnimation public void updateAnimation() { setImage( IDLE[ frame_ ] ); frame_++; if( frame_ >= IDLE.length ) frame_ = 0; }

27 act public void act() { // Add your action code here. updateAnimation(); } The [ ] brackets let you access the pieces in the array

28 [] brackets 1 2 3 4 5 IDLE[0] IDLE[1] IDLE[2] IDLE[3] IDLE[4] IDLE[5]

29 Your turn We will develop a character that can later be built into a video game To cut down on complexity, try remove gravity from your game (top down – Legend of Zelda, aircraft, space, etc.) We can negotiate games that require gravity/platforms but you should run it past me first We will improve their functionality later Learn to use: An array If statements Declare class variables Continue to: Make methods Declare local variables


Download ppt "Organizing Memory in Java"

Similar presentations


Ads by Google