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..
https://editor.p5js.org/kdoodoo/sketches/HyAmvbGj7
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 .....
2. Function that returns a value
No value returned
Value returned
Let's Define with Parameter AND Return
Let's Call
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()
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
B. Array can have numbers, strings, objects.
Example
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:
5. Array and Object (w/ Functions)
Example
6. It is complex :( So, Let's do 'Constructor Functions'
Make(construct) object
Constructor function is to make a 'new' object.
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()
B. Splice is checking code and remove the value.
Splice (index, how many items from index)
Assignment: if Confident > with Array.
Last updated