2 Replies Latest reply on Oct 19, 2006 10:55 PM by Mr Binitie

    Sprite instantiation is driving me crazy

    Mr Binitie Level 1
      Try as I might I cannot seem to create an instance of a sprite on stage. Not even copying the examples form the FlexBuilder 2 documentation seems to help. I can instanciate a whole host of other things, but Sprites and MovieClips no. What step am I missing? Is there anything anywhere that is basic and will yield result that are visible.
      Please help, I am really frustrated!!!!:( AAARRRGH!!!


        • 1. Re: Sprite instantiation is driving me crazy
          ntsiii Level 3
          Post a little sample app.

          Tracy
          • 2. Re: Sprite instantiation is driving me crazy
            Mr Binitie Level 1
            Here is the code for an image scroller component I built which works. Its a draft so there's a bit of tried and commented out code. You can see an example here.
            www.centralmasswebdesign.com/scroll/photoNav.html

            I just wanted to see if I could draw a Sprite, as I had so far been unsuccessful in FlexBuilder2.
            The code I place here will hang at initialisation until you comment out the drawSprite method, then all will be well.


            package dStcomponents.aB2.comp
            {
            import mx.containers.Canvas;
            import flash.display.DisplayObject;
            import flash.display.Sprite;
            import mx.controls.Image;
            import mx.validators.ValidationResult;
            import mx.rpc.http.HTTPService;
            import flash.net.*;
            import flash.events.*;
            import flash.utils.*;
            import mx.effects.*;
            import flash.filters.*;
            import mx.core.Application;
            import flash.utils.Timer;
            import flash.events.TimerEvent;


            public class ImageMouseNavAS extends Canvas
            {
            private var pSprite:Sprite;
            private var pContainer0:Image;
            private var pContainer1:Image;
            private var loader:URLLoader;
            private var request:URLRequest;
            private var aXML:XML;
            private var xlen:Number;
            private var XML_URL:String;
            private var myListenerObj:Object = new Object();
            private var pTimer:Timer;


            public function ImageMouseNavAS()
            {
            super();
            //writeln("::::Image Slider::::" + "\n");

            loader = new URLLoader();
            XML_URL = "content/img.xml";
            request = new URLRequest(XML_URL);

            configureListeners(loader);

            try {
            loader.load(request);
            } catch (error:Error) {
            //writeln("Unable to load requested document.");
            }

            //loader.addEventListener(Event.COMPLETE, onComplete);

            this.horizontalScrollPolicy = "off";
            this.verticalScrollPolicy = "off";
            //stage.frameRate = 36;
            drawSprite(20, 20)
            //this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
            setUpTimer();
            }

            public function setXLength(n:Number):void
            {
            xlen = n;
            }

            public function getXLength():Number
            {
            return xlen;
            }
            public function setUpTimer():void
            {
            //writeln("::::setUpTimer::::");
            pTimer = new Timer(50, 0);
            pTimer.addEventListener(TimerEvent.TIMER, slideTimeControl);
            pTimer.start()
            }

            public function onComplete():void
            {
            //writeln("::::onComplete::::");
            XML.ignoreWhitespace = true;

            aXML = new XML(loader.data);
            //writeln(aXML.children().length() + "\n");
            setXLength(aXML.children().length())
            createNewChildren(getXLength());
            }



            public function createNewChildren(n:int):void
            {
            this.width = 500;
            pContainer0 = new Image();
            pContainer1 = new Image();
            var f:DropShadowFilter = new DropShadowFilter(10,30,0x000000,.8);
            var myFilters:Array = new Array();
            myFilters.push(f);


            for (var i:int = 0; i<(n*2); i++)
            {
            var pImage:* = "p"+i;
            pImage = new Image();
            pImage.width = 120;
            pImage.height = 95;
            pImage.maintainAspectRatio = false;
            pImage.mouseEnabled = true;
            pImage.useHandCursor = true;
            pImage.alpha = .5;
            pImage.filters = myFilters;
            pImage.x = i*pImage.width;
            pImage.source = "images/"+aXML.children()[i%n].attribute("id");
            pImage.data = {path:aXML.children()[i%n].attribute("path")}
            pImage.addEventListener(MouseEvent.CLICK, clickHandler)
            pContainer0.addChild(pImage);

            }

            this.addChild(pContainer0);
            pContainer0.width = n*120;
            //pContainer0.mouseEnabled = true;
            //pContainer0.useHandCursor = true;
            pContainer1.x = pContainer0.x + pContainer0.width;
            this.addEventListener(MouseEvent.ROLL_OVER, rollOverHandler);
            this.addEventListener(MouseEvent.ROLL_OUT, rollOutHandler);

            }

            public function writeln(s:String):void
            {
            mx.core.Application.application.alert.text += s + "\n"
            }

            public function slideControl():void
            {
            var w:Number = this.width/2;
            writeln(String(w));
            }

            public function slideTimeControl(t:TimerEvent):void
            {
            var w:Number = this.width/2;
            var npx:Number;
            //writeln(String(w));

            if (contentMouseY > 0 && contentMouseY < this.height)
            {


            if
            (this.contentMouseX > 0 && this.contentMouseX < 250 )
            {
            npx = ((250 - contentMouseX)/ 250)*15;
            //writeln(String(npx));
            pContainer0.x -= Math.floor(npx);
            pContainer1.x -= Math.floor(npx);
            if (pContainer0.x <= (-1*(120*getXLength())))
            {
            pContainer0.x = pContainer0.x + (120*getXLength());
            }
            }
            else if (this.contentMouseX > 250 && this.contentMouseX < 500)
            {
            npx = ((contentMouseX -250)/ 250)*15;
            //writeln("after: "+String(npx));
            pContainer0.x += Math.floor(npx);
            if (pContainer0.x >= 0)
            {
            pContainer0.x = pContainer0.x - (120*getXLength());
            }
            }
            else if (this.contentMouseX == 250)
            {
            pContainer0.x +=0
            }

            }

            }

            private function clickHandler(event:Event):void
            {
            //writeln("clickHandler: " + event.currentTarget["data"]["path"]);
            var url:String = event.currentTarget["data"]["path"];
            var variables:URLVariables = new URLVariables();
            var requested:URLRequest = new URLRequest(url);
            requested.data = variables;
            try {
            navigateToURL(requested);
            }
            catch (e:Error) {
            //parentApplication.alert.text = "ERROR!!";
            }
            }

            private function rollOverHandler(event:Event):void
            {
            //writeln("rollOverHandler: " + "rolled over");
            //removeEventListener("enterFrame", enterFrameHandler);
            //if (this.mouseX >= this.)
            }

            private function rollOutHandler(event:Event):void
            {
            //writeln("rollOutHandler: " + "rolled out" + this.mouseX);
            pContainer0.x -=5
            //removeEventListener("enterFrame", enterFrameHandler);
            }

            public function drawSprite(w:Number, h:Number):Sprite
            {
            var s:Sprite = new Sprite();
            s.graphics.beginFill(0x000000, 0);
            s.graphics.drawRect(0, 0, w, h);
            this.addChild(s);
            return s;
            }

            private function configureListeners(dispatcher:IEventDispatcher):void
            {
            dispatcher.addEventListener(Event.COMPLETE, completeHandler);
            dispatcher.addEventListener(Event.OPEN, openHandler);
            dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
            dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            }

            private function completeHandler(event:Event):void
            {
            var loader:URLLoader = URLLoader(event.target);
            //writeln("completeHandler: " + loader.data);
            onComplete();
            }

            private function openHandler(event:Event):void {
            //writeln("openHandler: " + event);
            }

            private function progressHandler(event:ProgressEvent):void {
            //writeln("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
            }

            private function securityErrorHandler(event:SecurityErrorEvent):void {
            //writeln("securityErrorHandler: " + event);
            }

            private function httpStatusHandler(event:HTTPStatusEvent):void {
            //writeln("httpStatusHandler: " + event);
            }

            private function ioErrorHandler(event:IOErrorEvent):void {
            //writeln("ioErrorHandler: " + event);
            }


            }
            }

            The MXML code for FlexBuilder2

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" xmlns:ns2="aB2.comp.*" width="500" height="110"
            horizontalScrollPolicy="off" verticalScrollPolicy="off" backgroundAlpha="0" frameRate="36">
            <!--mx:TextArea x="147" y="128" id="alert" wordWrap="true" backgroundAlpha="0" width="346" height="290"/-->
            <ns2:ImageMouseNavAS x="0" y="7" id="pSlider" width="500" height="100">
            </ns2:ImageMouseNavAS>
            </mx:Application>

            [a /]