Week 06
Function II, Object and Array
1. Function with Parameters
Define with Parameter
fxunction sun(x,y,rays){
ellipse(x,y,20,20);
for(i = 0; i < rays; i++){
rect(x-30,y+20,30,30);
}
}Call with Parameters
sun(10,10,100); You can call many more
sun(10,10,100);
sun(20,20,110);
sun(30,30,120);
sun(40,40,130); Example
Let's draw a lolipop..
Lolipop with 'define' and 'call' function lolipop()
Now lolipop function with parameters
Define with parameters: x, y, diameter
Call with parameters: 100, 120, 150, 200, 300 .....
https://editor.p5js.org/kdoodoo/sketches/HJA4tbGsm
2. Function that returns a value
No value returned
Value returned
Let's Define with Parameter AND Return
Let's Call
https://editor.p5js.org/kdoodoo/sketches/HyLwxmfiQ
3. Object
We learned simple object.
Object with function
Function in the object uses this.x and this.y
How to read object elements.
Example
Define functions inside of the object : display and move
Call functions inside of the object: bubble.display() and bubble.move()
https://editor.p5js.org/kdoodoo/sketches/rk-AvQfim
4. Array
List of values in the [ ]
Similar to object in the { }
Array has indices, indexes : 0, 1, 2, 3, 4, 5, 6....
A. Access Values in the Array
https://editor.p5js.org/kdoodoo/sketches/Sk4k1NGj7
B. Array can have numbers, strings, objects.
Example
https://editor.p5js.org/kdoodoo/sketches/H1jPb4Mom
world.length gives the last index of the array
Example
Drawing 4 ellipses
Drawing 4 ellipses with for loop.
As we discussed, we can use .length here:
https://editor.p5js.org/kdoodoo/sketches/By0YvVfoQ
5. Array and Object (w/ Functions)
Example
https://editor.p5js.org/kdoodoo/sketches/S1cjoVGs7
6. It is complex :( So, Let's do 'Constructor Functions'
Constructor function is to make a 'new' object.
https://editor.p5js.org/kdoodoo/sketches/ryzlPfmoX
7. Add and Remove object for the array.
We can add value to the array like this:
A. So we use push()
Add bubble into bubbles[ ] by push()
https://editor.p5js.org/kdoodoo/sketches/H1DifHGiQ
B. Splice is checking code and remove the value.
https://editor.p5js.org/kdoodoo/sketches/BJ8byLMjQ
Splice (index, how many items from index)
Assignment: if Confident > with Array.
https://goo.gl/forms/iacdNBqXLv3gi4X82
Last updated