dcsimg
A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: problem accessing custom method

  1. #1
    Senior Member
    Join Date
    Aug 2000
    Posts
    173

    problem accessing custom method

    Hi All
    I am trying to get the hang of as3 and oop but having some issues. I have a very simple class that draws a square and makes it a button. it has one perameter that that takes a string and returns it. It also has a click listener that on click traces that string to make sure it was set correct. This works.

    On stage I create 4 instances of this class using a for loop. I can trace the properties of each instance from a new button on stage but I cant get the custom method returned
    Code:
    //works
    	trace("imgUrl "+container.getChildAt(1).x);
    	//does not
    	trace("imgUrl "+container.getChildAt(1).Img);
    I am not sure what I am doing wrong or why this is not working. here is all the code

    movie frame one
    Code:
    import com.classes.MyClass;
    
    
    var container:Sprite=new Sprite  ;
    container.name="container";
    addChild(container);
    for (var i=0; i < 3; i++) {
    	var myInstance:MyClass=new MyClass("property");
    	container.addChild(myInstance);
    	myInstance.name="Button"+i;
    	myInstance.x=68*i;
    	myInstance.Img="method"+i;
    	myInstance.addEventListener(MouseEvent.CLICK, clickedBtn);
    	function clickedBtn(e:MouseEvent):void {
    		trace(e.currentTarget.Img)
    		//trace("imgUrl "+e.currentTarget.Img);
    	}
    }
    var bgSquare:Sprite=new Sprite  ;
    bgSquare.graphics.beginFill(0x666666);
    bgSquare.graphics.drawRoundRect(0,0,64,44,0,0);
    bgSquare.graphics.endFill();
    bgSquare.name="bgSquare";
    addChildAt(bgSquare,0);
    container.x=200;
    container.y=200;
    
    bgSquare.addEventListener(MouseEvent.CLICK, clicked);
    function clicked(e:MouseEvent):void {
    	//works
    	trace("imgUrl "+container.getChildAt(1).name);
    	//does not
    	trace("imgUrl "+container.getChildAt(1).Img);
    }
    this is my class
    Code:
    package com.classes{
    	import flash.display.*;
    	import flash.events.*;
    
    	public class MyClass extends Sprite {
    		public var imgUrl:String = new String();
    		var btnCon:Sprite=new Sprite ();
    		public function MyClass($myString:String) {
    			trace($myString);
    			var container:Sprite=new Sprite  ;
    			addChild(container);
    			container.name="container";
    			var btn:Sprite=new Sprite  ;
    			btn.graphics.beginFill(0xFF0000);
    			btn.graphics.drawRoundRect(0,0,64,44,0,0);
    			btn.graphics.endFill();
    			btn.name="bgSquare";
    			container.addChildAt(btn,0);
    			btnCon.addChild(btn);
    			addChild(btnCon);
    		}
    		public function set Img(value:String):void {
    			imgUrl=value;
    			trace("set "+imgUrl);
    		}
    		public function get Img():String {
    			return imgUrl;
    			trace("retutn")
    		}
    	}
    }

  2. #2
    Palindrome emordnilaP Diniden's Avatar
    Join Date
    Feb 2008
    Posts
    230
    First thing I see, and it may work after this is for your .Img property you are adding to your Sprite you say "method"+i. Saying "method"+i does not work because "method" is a string and i is an integer or number.

    So change it up to something like this "method"+i.toString(); and that will be one step towards getting it right. And actually, it's prolly the only thing wrong : /

  3. #3
    Senior Member
    Join Date
    Aug 2000
    Posts
    173
    thanx but that did nothing I removed the I and I am still getting the same error at compile time even before I press the button

    1119: Access of possibly undefined property Img through a reference with static type flash.displayisplayObject.

    like I said the wierd thing is if I jsut change the property Img to x or width it returns the value so I know that I have the correct path as I can access other methods/properties. so the only thing I can think of is its some access issue but not sure I am stumped

  4. #4
    Palindrome emordnilaP Diniden's Avatar
    Join Date
    Feb 2008
    Posts
    230
    insteand of using the get and set methods of what you're doing, just turn them into functions getImg and setImg then instead of saying Img = blah; just use the setImg() functions. Much less sloppy that way and will focus your thinking while programming. I believe if you use the get and set the way you are using them you have to still declare a var for it. I dunno, I never use that way so I wouldn't know. But try what I said and that should clean it up.

  5. #5
    Senior Member
    Join Date
    Aug 2000
    Posts
    173
    I have tried that as well and I keep getting the same error. its very strange 1119: Access of possibly undefined property its like the method is private but its not. I can call it from the object it self if I use e.currentTarget.Img so I know it works but if I try to call it from any where other then it self container.getChildAt(1).Img or container.Button0.Img gives teh 1119 error not sure whats wrong but it has me very stuck on this project

  6. #6
    Palindrome emordnilaP Diniden's Avatar
    Join Date
    Feb 2008
    Posts
    230
    I got it. It's erroring where it you put getChildAt(1).Img

    getChildAt() returns a DisplayObject type NOT your class. Your class has to be referenced by it's instance name, anything dealing with the display list is all just the graphical aspect of it all.

    So, try putting your class instances into an array so that you can reference your classes through the array instead. This should clear it up.

  7. #7
    Senior Member
    Join Date
    Aug 2000
    Posts
    173
    I am not following, if my class extends sprite and x, width etc are sprite methods and I can get those by calling
    trace("imgUrl "+container.getChildAt(1).name);
    which retuns the name or
    trace("imgUrl "+container.getChildAt(1).x);
    which returns the x why would this not return my method as well
    trace("imgUrl "+container.getChildAt(1).Img);

    this works when applied to the object direct
    myInstance.addEventListener(MouseEvent.CLICK, clickedBtn);
    function clickedBtn(e:MouseEvent):void {
    trace(e.currentTarget.Img)
    //trace("imgUrl "+e.currentTarget.Img);
    }


    can you please give me an axample

    thanx for your help

  8. #8
    Senior Member
    Join Date
    Aug 2000
    Posts
    173
    there must be a god four days later and the help of a bunch of forums and a ton of searching the array thing did the trick.

    Code:
    import com.classes.MyClass;
    import flash.events.EventDispatcher;
    import flash.events.Event;
    
    var container:Sprite=new Sprite  ;
    container.name="container";
    addChild(container);
    
    var locInstance:MyClass=new MyClass("property");
    container.addChild(locInstance);
    locInstance.name="ButtonLoc";
    locInstance.y=68;
    locInstance.x=68;
    locInstance.Img="locmethod";
    //DisplayObjectContainer
    locInstance.addEventListener(MouseEvent.CLICK, clickedBtnloc);
    function clickedBtnloc(e:MouseEvent):void {
    	trace(e.currentTarget.Img);
    	//trace("imgUrl "+e.currentTarget.Img);
    }
    
    var locArray:Array = new Array();
    for (var i=0; i < 3; i++) {
    	locArray[i] = new MyClass("property");
    	//container.addChild(myInstance);
    	locArray[i].name="Button"+i;
    	locArray[i].x=68*i;
    	locArray[i].Img="method"+i;
    	//DisplayObjectContainer
    	locArray[i].addEventListener(MouseEvent.CLICK, clickedBtn);
    	function clickedBtn(e:MouseEvent):void {
    		//trace(e.currentTarget.Img);
    		trace("imgUrl "+e.currentTarget.Img);
    	}
    	
    	container.addChild(locArray[i])
    }
    var bgSquare:Sprite=new Sprite  ;
    bgSquare.graphics.beginFill(0x666666);
    bgSquare.graphics.drawRoundRect(0,0,64,44,0,0);
    bgSquare.graphics.endFill();
    bgSquare.name="bgSquare";
    addChildAt(bgSquare,0);
    container.x=200;
    container.y=200;
    
    bgSquare.addEventListener(MouseEvent.CLICK, clicked);
    function clicked(e:MouseEvent):void {
    	trace("imgUrl asdsa "+locArray[2].Img);
    }
    
    container.getChildAt(1).dispatchEvent(new MouseEvent(MouseEvent.CLICK, true, false));

  9. #9
    Palindrome emordnilaP Diniden's Avatar
    Join Date
    Feb 2008
    Posts
    230
    Glad ya got it.

    The reason it didn't work for your custom properties was because x and name and y are all properties of a displayObject, so they worked fine. However, a DisplayObject does NOT have your properties even if your class is a displayObject. When it returns a displayObject you only get the properties of a displayObject, you can't access any of the properties from a class that inherited from displayObject.

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