Flash ActionScript
Unleash the unlocked areas of ActionScript by exploring 'object-oriented programming.'
This might be the first time you have come across an object-oriented approach to ActionScript, so you need to understand the underlying concepts before you begin writing code. You need to understand what an object is, what a class is, how objects and classes are related and how they communicate by using messages. This approach is the same for all object-oriented languages and scripts.
What Is an object? If you look around you now you'll see many examples of real-world objects, such as your dog, your computer, your phone or your book. These physical objects have two characteristics: they all have properties (variables) and behaviour (functions). For example, cats have different properties (name, colour, hunger) and behaviour (purring, meowing and licking).
In Spriteris we have a game board. This is the area on which the pieces are stored and which dictates the position and rotation of the active falling piece. The board has properties (rows, columns, pieces, lines cleared, level) and behaviour (move piece, rotate piece, shift piece, check board, create new piece and so on).
Software objects are modelled after real-world objects. A software object maintains its state in one or more properties (variables). A software object applies its behaviour as functions. The board object has an active piece, which is an instance of a piece object. The active piece is moved down the board until it lands on the bottom or another piece and then another piece is chosen randomly and created in order to start the process again.
In object-oriented programming, you might want to represent real-world cats as software objects in an animation program. You can also use software objects to model abstract concepts. For example, an event is a common object used in ActionScript to represent the action of a user pressing a mouse button or a key on the keyboard.
Everything that the software object knows (properties) and can do (behaviour) is expressed by the variables and the methods within that object. A software object that modelled your car would have variables that indicated the car's current state: its speed is 10mph, its engine speed is 2000rpm, and its current gear is fifth. These variables are formally known as instance variables because they contain the state for a particular car object, and in object-oriented terminology, a particular object is called an instance.
In addition to its variables, the software car would also have different methods to make it start, go forward, reverse, steer and brake. These methods are known as instance methods because they inspect or change the state of a particular car instance.
Get top Black Friday deals sent straight to your inbox: Sign up now!
We curate the best offers on creative kit and give our expert recommendations to save you time this Black Friday. Upgrade your setup for less with Creative Bloq.
What is a class? In the real world you often have many objects of the same kind. For example, your piece is just one of many pieces in the game. Using object-oriented terminology, we say that your piece object is an instance of the class of objects known as pieces. Pieces have properties (rotation, colour, structure) and behaviour (rotate, width, height, getData) in common. Each piece's properties are independent of and can be different from that of other pieces.
When building pieces, the programmer takes advantage of the fact that pieces share characteristics, building all the pieces from the same scheme. It wouldn't be very efficient to produce a new scheme for every individual piece created.
In object-oriented software, it's also possible to have many objects of the same kind that share characteristics. Like the 'piece' creator, you can take advantage of the fact that objects of the same kind are similar and you can create a scheme for those objects. A software scheme or blueprint for objects is called a class.
What is inheritance? Generally speaking, objects are defined in terms of classes. You know a lot about an object by knowing its class. Even if you don't know what a Rover Mini is, if I told you it was a car, you would know that it had fourwheels, two doors and a steering wheel. Object-oriented systems take this a step further and allow classes to be defined in terms of other classes. For example, BMW, Ford Escort and SAAB are all kinds of cars. In object-oriented terminology, BMW, Ford Escort and SAAB are all subclasses of the car class. Similarly, the car class is the superclass of BMW, Ford Escort and SAAB.
As you follow through the Spriteris game, we take you through a step-bystep introduction to designing and implementing a game using Object- Oriented ActionScript. If you're feeling adventurous you could even extend the game with your own pieces or create a multi-player version!
This tutorial can be found in issue 40 of Computer Arts Special. To order a copy, please contact backissues@futurenet.co.uk. For more expert tutorials on 3D, Web and Photoshop, plus interactive CD ROM, subscribe for 13 issues here.
Download the tutorial pdf here and the accompanying files below.
Thank you for reading 5 articles this month* Join now for unlimited access
Enjoy your first month for just £1 / $1 / €1
*Read 5 free articles per month without a subscription
Join now for unlimited access
Try first month for just £1 / $1 / €1
The Creative Bloq team is made up of a group of design fans, and has changed and evolved since Creative Bloq began back in 2012. The current website team consists of eight full-time members of staff: Editor Georgia Coggan, Deputy Editor Rosie Hilder, Ecommerce Editor Beren Neale, Senior News Editor Daniel Piper, Editor, Digital Art and 3D Ian Dean, Tech Reviews Editor Erlingur Einarsson and Ecommerce Writer Beth Nicholls and Staff Writer Natalie Fear, as well as a roster of freelancers from around the world. The 3D World and ImagineFX magazine teams also pitch in, ensuring that content from 3D World and ImagineFX is represented on Creative Bloq.