WEEK 6: ARRAYS AND CLASS

INTRO

This week has been very cold, so I wanted to reminisce about the summer with ice cream. When the user presses the mouse, an ice cream scoop will appear. You can place the scoops anywhere on the canvas, but the cone is a guide to where to place your scoops. 

PROCESS

CREATING THE ICE CREAM SCOOP

Learning from last week, I simplified my starting point to one unit. I sketched what I wanted the ice cream to look like and noted the dimensions. For my scoop, I needed 1 large ellipse and 3 smaller ellipses.

Since I wanted to create multiple ice cream scoops, I made a class called Icecream. That way I could put the 4 ellipses in one class rather than draw 4 ellipses and figure out the math every time I wanted to create a scoop.

As I was coding I realized I wanted the ice cream to appear when clicking on the screen, like you are adding scoops to a cone, so I wanted to use mousePressed. And, since I was drawing ellipses as the bottom of the scoop, I might as well experiment with a for loop instead of creating 3 ellipses.

I experimented with creating a for loop within the class and was not producing useful results. I visited the Coding Lab where I learned how to distinguish the ellipses by creating different variables in the constructor function and used math with the radii of the ellipses, to create the correct ratio of the ellipses.

I created a “for loop” for the ice cream in the class. However, it wasn’t showing up on the screen, so I had to add a for loop to the “draw” function. So my question was – Since I have a for loop in my class, why do I have to create another for loop in draw instead of just calling the class in the draw function?

CREATING MULTIPLE ICE CREAMS SCOOPS

If my ice cream was going to appear when the mouseisPressed, then the x and y coordinate of the ice cream scoop would be the coordinates of the mouse. In the constructor function, I would pass in parameters into this.x and this.y in the form of the x and y-coordinates of the mouse. And if I were going to continuously add scoops, then I could use an array and use the push() function to add more and more scoops to my array and to my screen. To continuously generate ice cream scoops, I had a for loop in draw function.

defining my array
mousePressed function

CHANGING ICE CREAM FLAVORS

I thought it would be fun to change the ice cream scoop color each time you clicked your mouse, to signify a new flavor. I also thought changing the color would not be too difficult. Was I wrong! I originally tried an array of colors and created a for loop to cycle through the indexes of the array. However, it was only producing the 2nd index color, white, in the array upon each mousePress.

I worked with a classmate, where she created a counter and once the counter reached the 2nd index in the list, it would restart the array. That involved conditionals such as “if counter%3 ==0, fill(color[0]); and more conditionals after hat. It worked so it was a creative way to do it! I still wondered if there was an easier way to to it, and someone suggested that my program might be thinking my colors were text arrays rather than colors and to individually define the index colors and eliminating the counter. That leads to cleaner and simpler code. So, my main question from that is how do I know if an array is appropriate for what I’m coding or when am I overcomplicating things by using an array?

Leave a comment