A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: addEventListener

  1. #1
    Member
    Join Date
    Jan 2006
    Location
    Florida
    Posts
    51

    addEventListener

    I added a addEventListener inside of one of my functions

    but i want to get something really specific but nor really sure how this works


    Code:
    private function bitmapLoader(ImageTxt, Image_x, Image_y ) {
    			var loader:Loader = new Loader();
    			var urlReq:URLRequest = new URLRequest("images/category/"+ImageTxt);
    			loader.load(urlReq);
    			loader.x=  Image_x;
    			loader.y=  Image_y+20;
    			addChild(loader);
    			var container:Sprite = new Sprite();
    			addChild(container);
    			var rect:Sprite = new Sprite();
    			rect.graphics.lineStyle(0);
    			rect.graphics.beginFill(0xFF0000);
    			rect.graphics.drawRect(Image_x, Image_y+20, 100, 75);
    			rect.graphics.endFill();
    			container.addChild(rect);
    			rect.addEventListener("click", onClick); // THIS CALLS THE FUNCTION CALLED onClick
    			rect.buttonMode = true;
    			rect.addChild(loader);
    			rect.filters = [ new DropShadowFilter() ];
    		}


    Code:
    private function onClick(myText):void {
    			trace(myText);
                                       // RETURNS [MouseEvent type="click" bubbles=true 
    cancelable=false eventPhase=3 localX=57 localY=22 stageX=197 stageY=137 
    relatedObject=null ctrlKey=false altKey=false shiftKey=false delta=0]
                                       // I would like it to be more like Image1.jpg
    
    		}
    Moises Zaragoza

  2. #2
    Try this:

    I explain everything in the comments, havn't tested it though, so not sure about it.

    PHP Code:
    private function bitmapLoader(ImageTxt:StringImage_xImage_y ) {
        var 
    loader:Loader = new Loader();
        var 
    urlReq:URLRequest = new URLRequest("images/category/"+ImageTxt);
        
    loader.load(urlReq);
        
    loader.x=  Image_x;
        
    loader.y=  Image_y+20;
        
    addChild(loader);
        var 
    container:Sprite = new Sprite();
        
    addChild(container);
        var 
    rect:MovieClip = new MovieClip();//changed rect to a movieclip so I can dynamically add a user defined String that will hold ImgTxt
        
    rect.imgTxt=ImageTxt;//attach the string to the movieclip
        
    rect.graphics.lineStyle(0);
        
    rect.graphics.beginFill(0xFF0000);
        
    rect.graphics.drawRect(Image_xImage_y+2010075);
        
    rect.graphics.endFill();
        
    container.addChild(rect);
        
    rect.addEventListener("click"onClick); // THIS CALLS THE FUNCTION CALLED onClick
        
    rect.buttonMode true;
        
    rect.addChild(loader);
        
    rect.filters = [ new DropShadowFilter() ];
    }

    private function 
    onClick(event:MouseEvent):void {
        
    //event.currentTarget==the MovieClip(rect) that is clicked on
        
        //then access that dynamically created variable call imgTxt I created in the BitmapLoader function
        
    trace(event.currentTarget.imgTxt);


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center