Real Life Objects, Properties, and Methods
The Object class is the root of all the classes in ActionScript, all are an extension of the Object Class. Object is a dynamic class, meaning it permits adding new properties and methods, it enables you to create your own properties and methods. Creating objects There are two ways to create objects in ActionScript 3. The most common form is with the syntax. When you create a shared object, Flash Player creates a new directory for the application and domain inside its sandbox. It also creates a.sol file that stores the SharedObject data. The default location of this file is a subdirectory of the user’s home directory.
In real life, a car is an object.
A car has properties like weight and color, and methods like start and stop:
Object | Properties | Methods |
---|---|---|
car.name = Fiat car.model = 500 car.weight = 850kg car.color = white | car.start() car.drive() car.brake() car.stop() |
All cars have the same properties, but the property values differ from car to car.
All cars have the same methods, but the methods are performed at different times.
JavaScript Objects
You have already learned that JavaScript variables are containers for data values.
This code assigns a simple value (Fiat) to a variable named car:
Objects are variables too. But objects can contain many values.
This code assigns many values (Fiat, 500, white) to a variable named car:
The values are written as name:value pairs (name and value separated by a colon).
JavaScript objects are containers for named values called properties or methods.
Object Definition
You define (and create) a JavaScript object with an object literal:
Example
Spaces and line breaks are not important. An object definition can span multiple lines:
Example
firstName: 'John',
lastName: 'Doe',
age: 50,
eyeColor: 'blue'
};
Object Properties
The name:values pairs in JavaScript objects are called properties:
Property | Property Value |
---|---|
firstName | John |
lastName | Doe |
age | 50 |
eyeColor | blue |
Accessing Object Properties
You can access object properties in two ways:
or
Example1
Try it Yourself »Example2
Try it Yourself »Object Methods
Objects can also have methods.
Methods are actions that can be performed on objects.
Methods are stored in properties as function definitions.
Property | Property Value |
---|---|
firstName | John |
lastName | Doe |
age | 50 |
eyeColor | blue |
fullName | function() {return this.firstName + ' ' + this.lastName;} |
A method is a function stored as a property.
Example
firstName: 'John',
lastName : 'Doe',
id : 5566,
fullName : function() {
return this.firstName + ' ' + this.lastName;
}
};
The this Keyword
In a function definition, this
refers to the 'owner' of the function.
In the example above, this
is the person object that 'owns' the fullName
function.
In other words, this.firstName
means the firstName
property of this object.
Read more about the this
keyword at JS this Keyword.
Accessing Object Methods
You access an object method with the following syntax:
Example
Try it Yourself »If you access a method without the () parentheses, it will return the function definition:
Example
Try it Yourself »Do Not Declare Strings, Numbers, and Booleans as Objects!
When a JavaScript variable is declared with the keyword 'new
', the variable is created as an object:
var y = new Number(); // Declares y as a Number object
var z = new Boolean(); // Declares z as a Boolean object
Avoid String
, Number
, and Boolean
objects. They complicate your code and slow down execution speed.
You will learn more about objects later in this tutorial.
Home > Articles > Web Development > Dreamweaver & Flash
␡- Grouping and Drawing Objects
This chapter is from the book
This chapter is from the book
Grouping and Drawing Objects
Throughout this chapter, we have gone over how to draw, modify, and erase shapes from the stage. Following are a few helpful tips about grouping them.
Groupies
In the next chapter, you will see how to convert primitive objects to a Graphic symbol, but they are a more permanent solution for grouping shapes. Actually, grouping shapes together is a much less permanent solution because you can quickly group and ungroup them.
To group a few selections together, select Modify, Group (Ctrl+G). To Ungroup them, select Modify, Ungroup (Ctrl+Shift+G or Ctrl+B).
Drawing Objects
New to Flash 8, Drawing Objects are a special kind of group. You can draw your shapes within Drawing Objects automatically if the tool has the Object Drawing option turned on (Shortcut: J). They can also be created by selecting raw shapes (and other Drawing Objects) and selecting Modify, Combine Objects, Union.
What is unique about Drawing Objects is that even though they do not act like raw shapes, you can still modify them the same way with the Arrow tool, the Paint Bucket, the Eraser, and the other drawing tools without having to double-click them to go into Edit mode. You can also adjust them with the Properties Inspector, unlike groups, as shown in Figure 3.23. And like groups, you can stack Drawing Objects over top of one another in the same layer without having to worry about them affecting each other or raw shapes.
Figure 3.23 You can see the difference in the Property Inspector from selecting a group to selecting a Drawing Object.
You can, however, have them affect one another by overlaying them and selecting the other options under Modify, Combine Objects:
Intersect—Leaves only the part of the shape where all shapes overlaid.
Punch—Removes the section of all shapes beneath top shape where the top shape overlaid.
Crop—Leaves sections of all shapes beneath top shape where top shape overlaid. Exact opposite of punch option.
See Figure 3.24 to see what these options mean.
Figure 3.24 The effects of the different Combine Objects options.
Stacking Groups
As you get more familiar with grouping shapes as groups or Drawing Objects, you will notice that as you group them, they tend to stack on top of one another. Controls found under Modify, Arrange can help this, including:
BringtoFront—Brings the selection to the top viewing level in the layer. (Ctrl+Shift+Up)
BringForward—Moves the selection up one viewing level. (Ctrl+Up)
SendBackward—Moves the selection down one viewing level. (Ctrl+Down)
SendtoBack—Sends the selection to the bottom-most viewing level. (Ctrl+Shift+Down)
You can also lock shapes within a layer so that they cannot be selected and then unlock shapes to reselect them.
Now that you have seen how to work with shapes, groups, and Drawing Objects, you’ll learn about bitmaps.
Related Resources
Flash Objects Free
- Book $47.99
- eBook (Watermarked) $38.39
Create Flash Objects Pictures
- Web Edition $38.39