Creating Classes

As object orientated programming is something I am not yet comfortable with I feel as though I only understand some of the concepts partially. In order to further grasp the concept I looked at this explanation of objects by Daniel Shiffman on processing.org. I then tried to change work that I had previously done to introduce classes. I used the simple square hover, which I had used in the mindmap that I created for the poster project to become more familiar with the interface. I found that using a simpler example than what we were doing in processing to understand the simple concepts of object orientated programming was very useful to me as it meant that I could see the difference between something I had already used and turn it into a class, it meant that I could focus on one particular concept I was struggling with instead of having to cope with a number of them at the same time. This was helpful because it made me learn through doing by myself rather than through following (though the following part was also useful).

The main problem I was having when trying to convert previous things I had created/altered into classes was with scope and syntax (i.e. I didn’t know what the processing syntax does and doesn’t allow you to do). Changing the simple square mouse hover (which I had originally taken from this example on processing.org) was extremely useful because it made me understand scope better. For example I had a problem in which I wanted to change the position of a rect which was assigned the global variable ‘rectX’, however I wanted to created a local variable which altered the size of the rect but keep the global variable at the same time, I didn’t know how to do this.

Screen Shot 2014-11-26 at 05.52.00

As can be seen above, the global variable, or the class variables int rectX, rectY are assigned.

Screen Shot 2014-11-26 at 06.34.25

In the main setup and draw section i want to create different squares in different positions which when hovered over by the mouse change colour.

Screen Shot 2014-11-26 at 05.53.53

but the problem was that i couldn’t change the rectX and rectY variables to local variables in the display() method because they were being used elsewhere for the mousehover to work.

Screen Shot 2014-11-26 at 05.51.18

I resolved this issue by creating another variable called rectPosX and rectPosY and assigining them variables to the class variables of rectX and rectY.

Screen Shot 2014-11-26 at 06.42.34

This resulted in me being able to create multiple squares in multiple locations which all updated when the mouse ‘hovered’ over each individual position. The use of classes also brings new problems in that I had to make the for loop variables global, which I hadn’t had to do before. You can see a result of the code here.

Working out this small thing helped me understand scope and object orientated programming and now I have understood this I can move forward to understand more complex things. I think in moving forward I need to further investigate arrays, because they are the things that I’m not necessarily struggling with understanding but more with creating and writing the syntax correctly. I also find that although progression is slow moving, I am grasping the fundamental concepts of programming one small step at a time, but as I solve one problem, others arise. I find that the more familiar I become with processing and the more I attempt to create things in processing the more understandable it becomes. I feel as if I carry on coding and trying to work things out the answers will come to me, but I know I am progressing as things that I was trying to work out that seemed impenetrable a year or two ago are now becoming clear. This relates to my iterative process (although not confined to within this unit) and reminds me that although progress sometimes seems to be slow it is made through trying to work things out and persistence, even though at times I generally feel I will never grasp certain things.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s