flash actionscript:
I am trying to create my own class that extends MovieClip so that I can draw custom shapes easily. I don't want to have to drag anything to the stage, I want to be able to create the entire application in code. I want my custom class to be able to respond automatically to events such as onPress and onRelease. The rectangle draws, however I cannot get the onPress and onRelease functionality to work. Any ideas what might be wrong? // Example.as This is the entry point into the application. class Example { static var app:Example; function Example() { } static function main(mc) { app = new Example(); var clip:MyRectangle = new MyRectangle(); clip.SetDimensions(140, 150); clip.SetColor(0xFF00F0); clip.Render(); } } // MyRectangle.as Draws the rectangle but does not respond to any events. class MyRectangle extends MovieClip { private var m_clip:MovieClip; private var m_width:Number; private var m_height:Number; private var m_color:Number; function MyRectangle() { var depth:Number = _root.getNextHighestDepth(); m_clip = _root.createEmptyMovieClip("clip" + depth, depth); m_width = 10; m_height = 10; m_color = 0xFF0000; this.onPress = doPress; this.onRelease = doRelease; } public function SetDimensions(width:Number, height:Number):Void { m_width = width; m_height = height; } public function SetColor(col:Number):Void { m_color = col; } public function Render():Void { // Draw border around the stage m_clip.lineStyle(0, 0, 100); m_clip.beginFill(m_color, 100); m_clip.lineTo(m_width, 0); m_clip.lineTo(m_width, m_height); m_clip.lineTo(0, m_height); m_clip.lineTo(0, 0); m_clip.endFill(); } private function doPress():Void { this.startDrag(); } private function doRelease():Void { this.stopDrag(); } }
I seemed to have solved the problem by replacing this.onPress = doPress; this.onRelease = doRelease; with m_clip.onPress = doPress; m_clip.onRelease = doRelease; But I have a few remaining questions. 1. However, is this design typical for creating new movieclips of specfic shapes? 2. In this case does MyRectangle need to extend movieclip? 3. Are there better design patterns to do something like this?
Don't see what you're looking for? Try a search.
|