![]() ![]() For example, the p5.Vector class contains x, y, and z fields. Now you’ve seen that classes contain fields. When you encounter a Cannot read property 'x' of undefined error, try using console.log() statements to figure out which variable is undefined, and then make sure that variable points to an instance instead of undefined. Just like you can create an array of primitive values, you can also create an array of objects:įor ( let i = 0 i height ) Try changing the above code to add a green circle using a third p5.Vector instance. It’s also pretty confusing, and “thinking in objects” can take some time. This is a powerful idea, and it lets you organize your code as it gets more complicated. Updating the red circle does not update the blue circle, and vice-versa. Notice that the red circle and the blue circle are independent. This code creates two variables, each pointing to a different p5.Vector instance. Changing one instance doesn’t change the other instances.įor example, this code creates two p5.Vector instances: One of the most important concepts to understand with objects is that each instance is independent of other instances of the same class. This code uses a p5.Vector instance to represent a falling circle. The “Syntax” section of the p5.Vector reference tells you what parameters the p5.Vector constructor can take.įor example, instead of setting the myVector.x and myVector.y values yourself, you can pass them into the p5.Vector constructor:Īnd here’s the original example using a p5.Vector instance: This is also called a no-args constructor.īut like functions, constructors can also take arguments, passed in as comma-separated values inside the () parentheses. The above constructor does not take any parameters, and the x, y, and z fields inside the instance it creates will point to default values of 0. The p5.Vector() part is also called a constructor because it constructs the instance. The new keyword tells the computer to create a new instance, and the p5.Vector() part tells the computer what class to create an instance of. To create an object, use the new keyword, followed by the name of the class, followed by parentheses () similar to a function call: That means you can create a variable with an object type of p5.Vector, and that variable will contain x, y, and z fields. ![]() For example, the p5.Vector reference tells you that the p5.Vector class contains x, y, and z fields. InstancesĪ class is like a template that tells you what’s available inside an object. Specifically, take a look at the p5.Vector class. Similar to how you looked up primitive types in the reference, you can also find classes in the reference. But how do you know what those related values are?Ī class tells you about a particular object type. Primitive types hold a single value, and object types hold multiple related values. You’ve seen primitive types like numbers and booleans, which hold a single value. This approach works, but it’s generally considered a bad idea because it can be difficult to understand code that uses a bunch of different arrays. This approach of using multiple arrays to hold related data is called parallel arrays. This code uses two arrays: one that holds the x values of the circles, and another that holds the y values. You might use one array to hold x values, and another array to hold y values: Next, let’s say you wanted to show multiple falling circles instead of just one. This code uses primitive x and y variables to represent the position of a falling circle. Let’s start with some code that uses two primitive values to show a falling circle: It’s a single value, without any extra information. When you read this line of code, you know that x holds a primitive value of 7, but you don’t know whether there’s an associated y value. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |