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 12-8. CompositeView.as (continued)<br />

}<br />

}<br />

}<br />

aChildren.push(c);<br />

override public function update(event:Event = null):void<br />

{<br />

for each (var c:ComponentView in aChildren)<br />

{<br />

c.update(event);<br />

}<br />

}<br />

Note the overridden update( ) function in the CompositeView class shown in<br />

Example 12-8. It calls the update method in all its children. Therefore, calling the<br />

update( ) function in the root node of the <strong>com</strong>posite view structure will cascade<br />

down and traverse the <strong>com</strong>ponent tree updating all views. Let’s subclass<br />

CompositeView and ComponentView classes and create a nested view structure to see<br />

how this works.<br />

Creating nested views<br />

To illustrate nested views, we will create a <strong>com</strong>posite view node and two child <strong>com</strong>ponent<br />

views as shown in Figure 12-2.<br />

Nested views<br />

Root node<br />

registers to receive<br />

update notices<br />

Model<br />

RootNodeView<br />

Update events<br />

cascade down from<br />

parent to child views<br />

CharCodeLeafView ACIICharLeafView<br />

Displays the<br />

character code of<br />

the key pressed<br />

Figure 12-2. Nested view structure in minimalist example<br />

Receives update<br />

events from model<br />

Displays the ASCII<br />

character code of<br />

the key pressed<br />

We will first create a <strong>com</strong>posite view called RootNodeView. This <strong>com</strong>posite view will<br />

receive keyboard input events from the stage. This view will also register to receive<br />

Minimalist Example of an MVC Pattern | 439

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

Saved successfully!

Ooh no, something went wrong!