The ImageManager class, introduced in the last post, has been modified to expand its usability. In the first iteration, the id of the image displayed was contained internally and could not be used by an outside function or class; in the modified version the current image id is announced to external event listeners via a custom event dispatcher. To accomplish this, the ButtonPressedEvent class is used.
The buttonEvent – dispatched by the instance of the ButtonPressedEvent class – is used to pass the current image’s id to a function that displays the title of the image.
The behavior of the previous and next arrows has been changed to indicate on which side of the image the mouse is on. When the mouse is on the right half of the image, the next arrow is at full brightness (alpha=1) while the previous arrow is transparent (alpha=0.3) and vice versa.
The code below implements the ImageManager class with the new functionality. An event listener waiting for a ButtonPressedEvent has been added to the ImageManager instance. The HTMLText class is used to display each image title while the removePreviousInstance function is used to erase the previous title from the screen.
import com.dtk.ImageManager; import com.dtk.ButtonPressedEvent; import com.dtk.HTMLText; // array of image urls & titles var images:Array = [ ]; images.push( { url : 'http://www.as3dtk.com/swf/images2/driveway.jpg', title : 'Red garage, fence' } ); images.push( { url : 'http://www.as3dtk.com/swf/images2/2930.jpg', title : '2930' } ); images.push( { url : 'http://www.as3dtk.com/swf/images2/tow-wall.jpg', title : 'Tow Away Wall' } ); images.push( { url : 'http://www.as3dtk.com/swf/images2/gray-garage.jpg', title : 'Garage' } ); images.push( { url : 'http://www.as3dtk.com/swf/images2/blue-doors.jpg', title : 'Blue Doors' } ); // instance of ImageManager class displays 1st image from array var im:ImageManager = new ImageManager( 400, 250, 10, images ); im.x = 10; im.y = 20; im.addEventListener( ButtonPressedEvent.BUTTON_PRESSED, onButtonPressed ); addChild( im ); /** * function called by event listener for ButtonPressedEvent * */ function onButtonPressed( e:ButtonPressedEvent ):void { // remove previous instance of HTMLText removePreviousInstance( 'Title' ) // e.buttonEvent is id of image displayed - passed by custom dispatched event // instance of HTMLText class displaying image title var tt:HTMLText = new HTMLText( 300, 'Verdana', 12, 0x575757, 'LEFT', images[e.buttonEvent].title ); tt.x = 20; tt.y = 290; tt.name = 'Title'; addChild( tt ); } /** * function removes previous class instance specified by parameter iname * */ function removePreviousInstance( iname:String ):void { // loop through all children of parent object for ( var i:uint=0; i<numChildren; i++ ) { // if sprite name matches parameter iname then delete if ( getChildAt( i ).name == iname ) { removeChildAt( i ); } } } |
I have also rewritten and re-commented a good portion of the ImageManager class file to make to the coding more readable and understandable.
Download files: ImageManager_Mod.zip (45)
No Comments so far ↓
There are no comments yet...Kick things off by filling out the form below.