10.12.2012 Views

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Example 1-7. Encap.as (continued)<br />

}<br />

}<br />

public function Encap( )<br />

{<br />

addChild(textFld);<br />

textFld.x=100;<br />

textFld.y=100;<br />

}<br />

function showDogTalk( )<br />

{<br />

textFld.text=dogTalk;<br />

}<br />

Also, minor changes have to be made to the test file. The supertype implemented<br />

must be changed. Example 1-8 shows the new test class, TestEncap , for the Encap<br />

class.<br />

Example 1-8. TestEncap.as<br />

package<br />

{<br />

import flash.display.Sprite;<br />

}<br />

public class TestEncap extends Sprite<br />

{<br />

public var encap:Encap<br />

public function TestEncap( )<br />

{<br />

encap=new Encap( );<br />

encap.dogTalk="Meow";<br />

encap.showDogTalk( );<br />

addChild(encap);<br />

}<br />

}<br />

Go ahead and test it by changing the Document class name to TestEncap. This time,<br />

though, you’ll get the following error in the Complier Errors panel:<br />

Line 11: 1178: Attempted access of inaccessible property dogTalk through a reference<br />

with static type Encap.<br />

Source: encap.dogTalk="Meow";<br />

It shows the source of the error to be the line:<br />

encap.dogTalk="Meow";<br />

That error reflects the fact that it attempted to access a private variable outside the<br />

class. To fix the script, <strong>com</strong>ment out the offending line:<br />

//encap.dogTalk="Meow";<br />

18 | Chapter 1: Object-Oriented Programming, <strong>Design</strong> <strong>Patterns</strong>, and <strong>ActionScript</strong> <strong>3.0</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!