ActionScript 3.0 Design Patterns

(Chris Devlin) #1

18 | Chapter 1: Object-Oriented Programming, Design Patterns, and ActionScript 3.0


Also, minor changes have to be made to the test file. The supertype implemented


must be changed. Example 1-8 shows the new test class,TestEncap, for theEncap


class.


Go ahead and test it by changing the Document class name toTestEncap. This time,


though, you’ll get the following error in the Complier Errors panel:


Line 11: 1178: Attempted access of inaccessible property dogTalk through a reference
with static type Encap.
Source: encap.dogTalk="Meow";

It shows the source of the error to be the line:


encap.dogTalk="Meow";

That error reflects the fact that it attempted to access a private variable outside the


class. To fix the script, comment out the offending line:


//encap.dogTalk="Meow";

public function Encap( )
{
addChild(textFld);
textFld.x=100;
textFld.y=100;
}
function showDogTalk( )
{
textFld.text=dogTalk;
}
}
}


Example 1-8. TestEncap.as


package
{
import flash.display.Sprite;


public class TestEncap extends Sprite
{
public var encap:Encap
public function TestEncap( )
{
encap=new Encap( );
encap.dogTalk="Meow";
encap.showDogTalk( );
addChild(encap);
}
}
}


Example 1-7. Encap.as (continued)

Free download pdf