Add additional functionality to an instance of a class?
Posted: Tue Jan 01, 2019 7:16 pm
I’m not so sure I understand the usefulness of making classes (pseudo-classes).
I’ve created a class called Shape (based on the video www.youtube.com/watch?v=KDXUE1enqXE). It takes 5 arguments: x, y, xSpeed, ySpeed and shapeMode (which is used to make the shape either “fill” or “line”). I didn’t include width and height because I extended this class with 2 sub-classes, Rectangle and Circle. Circles don’t have width and height. This class (Shape) has NO update or draw functions.
The two sub-classes are very similar to each other and have 2 arguments and update and draw functions. Rectangle has width and height and Circle has radius and numSegments (the number of segments for drawing the circle). They work fine. If I create an instance with an xSpeed and/or a ySpeed they do as you’d expect: they appear on the screen and move as specified by the speeds.
With these classes, I can make instances of them but I’m not sure how to actually use them. For example, if I make two instances and I want one of them to stay in the game (like a ball), how do I add the functionality of bouncing off the edges/”walls”? And if I want the other to go off screen when it gets to an edge/”wall” and be destroyed (like a bullet), how do I add that functionality?
I guess the question boils down to “How do you give a specific instance of a class additional functionality that other members of the class don’t have?” Is this possible as in the bounce off walls vs go off screen example I mentioned above?
I’ve created a class called Shape (based on the video www.youtube.com/watch?v=KDXUE1enqXE). It takes 5 arguments: x, y, xSpeed, ySpeed and shapeMode (which is used to make the shape either “fill” or “line”). I didn’t include width and height because I extended this class with 2 sub-classes, Rectangle and Circle. Circles don’t have width and height. This class (Shape) has NO update or draw functions.
The two sub-classes are very similar to each other and have 2 arguments and update and draw functions. Rectangle has width and height and Circle has radius and numSegments (the number of segments for drawing the circle). They work fine. If I create an instance with an xSpeed and/or a ySpeed they do as you’d expect: they appear on the screen and move as specified by the speeds.
With these classes, I can make instances of them but I’m not sure how to actually use them. For example, if I make two instances and I want one of them to stay in the game (like a ball), how do I add the functionality of bouncing off the edges/”walls”? And if I want the other to go off screen when it gets to an edge/”wall” and be destroyed (like a bullet), how do I add that functionality?
I guess the question boils down to “How do you give a specific instance of a class additional functionality that other members of the class don’t have?” Is this possible as in the bounce off walls vs go off screen example I mentioned above?