💻
Coding for Creatives Extras
  • Hello
  • Templates & Shortcuts
    • Power of Negative 1
    • Modifiers
    • Start Codes
      • Processing Start Code
      • HTML, CSS, JavaScript Start Code
    • WASD Movement Code
      • Processing Example
      • Unity Example (3D)
      • Unity Example (2D Platformer)
    • Connecting Platforms
  • Useful Links
    • Share & Show Off
    • Software and Apps
    • Free & Affordable Game Assets
  • Exercises
    • Week 1
      • Week 1 Processing Exercise
    • Week 2
      • Week 2 Star Wars Name Exercise
      • Week 2 Mad Libs Exercise
    • Week 3
      • Week 3 Moving Squares Exercise
    • Week 4
      • Week 4-A Movement Using If Statements
      • Week 4-B Boundaries Using If Statements
      • Week 4-C Hover, Click, and Drag Effects
    • Week 5
      • Week 5-A Moving Shapes
      • Week 5-B Looping Arrays
      • Week 5-C Using User Input to Create Arrays
      • Week 5-D For and Foreach Loops in Unity
    • Week 6
      • Week 6-A Snake
      • Week 6-B 2D Character Movement (Unity)
    • Week 7
      • Week 7-A Mario Coin Catch (Unity)
      • Week 7-B Raycasting Example (Unity)
    • TouchDesigner Exercises
      • Colorful Text
      • Audio-Reactive 3D Shapes
      • TouchOSC
  • Glossary and Terms
    • Blank Space / Canvas Origin
    • Shapes / Primitives
    • Color
    • Movement
    • Control
    • Variables
      • Declaring Variables
      • Assigning a Variable
      • Using a Variable
      • Data Types
        • Floating Point or Float
        • Integer or Int
        • Boolean or Bool
        • String
      • Casting a Variable
    • Arrays
      • Creating Arrays
      • Assigning Values to Arrays
      • Using Array Elements
      • Multi-Dimensional Arrays
      • Common Errors
    • Conditional Statements (If/Else)
    • Operators
      • Relational Operators
      • Mathematical Operators
      • Logical Operators
    • Loops
      • While Loops
      • For Loops
      • Foreach Loops
    • Functions & Methods
      • Creating a Function
      • Calling or Invoking a Function
      • Parameters & Arguments
    • Interaction
      • Mouse Interaction
    • Objects, Classes & Libraries
      • Accessing Objects
    • Node-Based Programming
    • Packaging and Sharing
  • TouchDesigner Info
    • Overview
    • Popular TOPs
    • Popular CHOPs
    • Popular SOPs
    • Popular DATs
  • Arduino Info
    • Arduinos!
    • The Parts
    • The Setup
      • Using the Serial Connection
      • Output Component Setup Examples
      • Input Component Setup Examples
      • Create a Night Light
Powered by GitBook
On this page
  • Arrays
  • Step 1: Displaying Text in Processing
  • Step 2: Create an Array and Display Each Item
  • Step 3: Moving the Array into a For Loop

Was this helpful?

  1. Exercises
  2. Week 5

Week 5-B Looping Arrays

The steps below walk you through a Processing activity we did during Week 5. All code should allow you to copy and paste.

Arrays

Arrays are one of the common ways loops are utilized.

Why?

  1. Array values are conveniently accessed by an index - an integer

  2. Arrays vary in sizes and can be quite long

  3. A loop to display or run code per item in ONE frame is easier than using the same code repeatedly

Step 1: Displaying Text in Processing

Displaying text is similar to displaying a shape.

Start by giving the text a color, then use text() to provide the message and where to put it.

void setup() {
  // Creates a canvas 600 pixels by 600 pixels
  size(600, 600);
}

void draw() {
  background(0);      // Black
  fill(247, 231, 206);      // Champagne color
  noStroke();      // No outline color
  
  // Sets a size for the text
  textSize(20);
  
  // Draws the text at (50, 50)
  text("This is text!", 50, 50);
}

Output when played:

Step 2: Create an Array and Display Each Item

Println() will separate lines in a console, but in the canvas, we need to change the y value to create space between each text() object.

String[] pets = { "dog", "igauna", "hamster" };

void setup() {
  // Creates a canvas 600 pixels by 600 pixels
  size(600, 600);
}

void draw() {
  background(0);      // Black
  fill(247, 231, 206);      // Champagne color
  noStroke();      // No outline color
  
  // Sets a size for the text
  textSize(20);
  
  // Draws the text at (50, 50)
  text(pets[0], 50, 50);
  
  // Draws the text at (50, 75)
  text(pets[1], 50, 75);
  
  // Draws the text at (50, 100)
  text(pets[2], 50, 100);
}

Output when played:

Step 3: Moving the Array into a For Loop

As you can see, the full array appears, but only because we have 3 lines of code in our draw().

Every time we update this array, we would need to add or remove these lines.

INSTEAD, we can use a for loop to always display the number of items in the array no matter its length.

First, let's put the changing y coordinate into its own variable.

In this example, the y increases by 25 each time. We can put that into a variable called spacing.

float y = 50;
float spacing = 25;

For loops use control variables - an integer - that updates at the end of each run of the loop. For arrays, the variable i is often used to stand for "index" or "item" of the array.

One of the neat things about arrays is their "length" property. It provides the count of items in an array.

Since the array index starts with zero, the index will never go higher than the length of an array minus one (or array.length - 1).

To move to the next item in the array, we want the control variable i to increase by 1. The shortcut for this is i++.

Putting the control variable, the loop's limits, and a way to update it looks like this:

for (int i = 0; i < pets.length; i++)

Add curly backets { } to this for loop and put what you want to happen inside them.

{
    text(pets[i], 50, y);
    y += spacing;
}

We initialize i and later, it resets to 0 with the loop, BUT we don't reset the y value, so it continues to add the spacing value. So the array appears to display really fast and go off the bottom of the canvas.

Add a line of code before the end curly bracket } of draw() so it resets for each frame.

y = 50;

Here is our full code:

String[] pets = { "dog", "igauna", "hamster" };
float y = 50;
float spacing = 25;

void setup() {
  // Creates a canvas 600 pixels by 600 pixels
  size(600, 600);
}

void draw() {
  background(0);      // Black
  fill(247, 231, 206);      // Champagne color
  noStroke();      // No outline color
  
  // Sets a size for the text
  textSize(20);
  
  for (int i = 0; i < pets.length; i++) {
    text(pets[i], 50, y);
    y += spacing;
  }
  
  y = 50;
}

Output when played:

Now, you can add and remove items from the array without having to change any other code:

String[] pets = { "dog", "igauna", "hamster", "parrot", "goldfish" };
float y = 50;
float spacing = 25;

void setup() {
  // Creates a canvas 600 pixels by 600 pixels
  size(600, 600);
}

void draw() {
  background(0);      // Black
  fill(247, 231, 206);      // Champagne color
  noStroke();      // No outline color
  
  // Sets a size for the text
  textSize(20);
  
  for (int i = 0; i < pets.length; i++) {
    text(pets[i], 50, y);
    y += spacing;
  }
  
  y = 50;
}

Output when played:

PreviousWeek 5-A Moving ShapesNextWeek 5-C Using User Input to Create Arrays

Last updated 4 years ago

Was this helpful?