Programming Fundamentals


ActionScript is an object-oriented programming language. Object-oriented programming, or OOP for short, is one of those big, nasty terms you hear hardcore code geeks throwing around all the time. Consequently, to noncoders, it sounds like OOP is some scary, complicated topic beyond their comprehension . That couldn't be further from the truth! OOP is what is known as a programming methodology, which basically means it's a way to think about, and design solutions for, programming-based problems.

Programming Methodologies

It turns out that there are two main programming methodologies, procedural programming and object-oriented programming. Most of the "classic" programming languages such as Fortran, Pascal, and C, are based on the procedural method. This means that those languages attack problems in a step-by-step manner. They do task A first, then task B, and finally, they do task C. In procedural languages, if you have a bit of code that you use a great deal, you can place that bit of code into a function . Functions are just small packets of reusable code. Functions don't actually do anything until you call them. When you call a function, you can pass information or values into it by using parameters or arguments. A function accepts certain parameters and then spits out a result based on those parameters.

Although procedural languages such as C are still used a great deal, they suffer from a few distinct problems. First of all, although the functions in procedural programming are somewhat reusable, it's simply not possible to reuse the vast majority of procedural code. Why? Most procedural code can't be reused because it is written to attack very specific problems. Therefore, most of the code is extremely specialized.

This specialization creates problems. Unless you are the original creator of a piece of code, it can take a long time to really comprehend what a particular piece of code does. The problem is that you have to understand almost every single line of code to understand the code as a whole.

This leads to the final problem with procedural code ”the fact that it is so focused on procedure. Why is this a problem? In programming, the data or information is as important as the code. There are distinct relationships between data and code that procedural languages simply ignore.

To address these issues and more, object-oriented programming was created. Many newer languages, such as Java, C++, JavaScript, and ActionScript, are object-oriented languages. Each language implements OOP in a different way, but the one thing they all have in common is that they are based on objects.

Objects, Properties, and Methods

What then is an object? An object is simply a collection of related data and code. Sections of code within an object are known as methods , and pieces of data or information within an object are known as properties . That doesn't really explain the power of OOP though; for that you'll need to look at an example.

Say you want to build a car racing game. In a procedural language, you would have to keep track of a great deal of data, such as each car's speed, color , make, model, and so on. You also would need a large number of functions to handle actions such as steering each car, turning each car's headlights on or off, and accelerating and braking each car.

In an OOP language, you simply deal with a collection of car objects. You easily can create multiple car objects, and each car object is an abstract representation, or copy, of a single car object that acts as a "blueprint." The blueprint contains all the data about that car and all the code needed to manipulate the car. When you create new car objects, the blueprint relays all this information to the new objects. For example, each car object could have these same properties:

  • Color

  • Speed

  • Make

  • Model

Note

Abstraction of a car? Sounds a little bizarre. Regardless of whether you realize it, you think in abstractions all the time. A picture of a chair is not a chair, but you recognize the picture as representing a chair . That's an abstraction. That's all objects are ” abstractions of ideas.


Each car object also could have the same methods:

  • Accelerate

  • Brake

  • Turn

  • ToggleHeadlights

The properties describe the object; the methods make the object do something. Although each car object is created from the same blueprint, the actual information inside each car object is unique and not related to any of the other car objects (unless you made it that way). This means that although all car objects have the same properties, one car might be blue and the other might be red. Thus, after you define a blueprint for how your car object works, you can easily stamp out a bunch of car objects based on that blueprint, similar to the way an assembly line creates mass quantities of a product based on a single product design.

Class-Based Languages versus Prototype-Based OOP Languages

It turns out that different languages handle the blueprint idea differently. The first camp, which includes Java and C++, use the idea of a class. A class is just a standalone section of code that fully describes how to build a particular type of object. Class-based languages are, in general, the more robust of the two types of OOP languages.

The other camp, which includes JavaScript and ActionScript, is prototype based. In these languages, you have to create a prototype object , which acts as your blueprint. Prototype objects are just like any other object, except they have extra information in them that describes exactly how to build copies of them. Prototype-based languages incur less overhead than class-based ones, which is why Macromedia chose to make ActionScript a prototype-based language. In Flash, the smaller the file, the better.



Inside Flash
Inside Flash MX (2nd Edition) (Inside (New Riders))
ISBN: 0735712549
EAN: 2147483647
Year: 2005
Pages: 257
Authors: Jody Keating

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net