A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Access of possibly undefined property... argh!!!!

  1. #1
    flashguy
    Join Date
    May 2005
    Location
    In the mountains
    Posts
    341

    Access of possibly undefined property... argh!!!!

    Hi!

    I have this simple piece of code below:

    Code:
    function createShapes():void
    {
    	var X:int=50;
    	var Y:int=55;
    	
    	for(var i:int=0;i<35;i++)
    	{
    		var shape:MovieClip = new numberBox_mc();
    		shape.x = X;
    		shape.y = Y;
    		shape.eatNumber.text = "";
    		shape.name = "shape"+i;
    		addChildAt(shape, i);
    		X+=60;
    		if (X == 410)
    		{
    			X=50;
    			Y+=60;
    		}
    	}
    }
    
    function feedShapes():void
    {
    	for(var i:int=0;i<35;i++)
    	{
    		var myshape:DisplayObject = getChildByName("shape"+i);
    		// error happens in the next line of code
    		myshape.eatNumber.text = String(i);
    	}
    }
    The compiler keep spiting out the following error message:
    1119: Access of possibly undefined property eatNumber through a reference with static type flash . display : DisplayObject .

    What really annoys me is the 'possibly' word within the message. It doesnt make any sense to me and sounds as if the compiler would be trying to guess things. Doesnt it at least seems to be obvious enough that this code will be ran against one or more mcs that will be created at run time - and therefore doesnt exist YET? What if I would be creating it totally from scratch, programatically????

    Anyway, how to turnaround this odd behavior?

    PS: Yes, I have the 'numberBox_mc' mc at the library!

    Last edited by yanmoura; 01-03-2010 at 11:24 AM.
    Visit my business at http://www.ballooncreator.com - Software Tool For Party Balloons Online Design!

  2. #2
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    getChild and getChildAt returns are cast as DisplayObject.

    DisplayObjects don't have a property called "eatNumber"

    you need to cast that returned DisplayObject as the subclass that does have that property

    PHP Code:
    numberBox_mc(myshape).eatNumber.text String(i); 

  3. #3
    flashguy
    Join Date
    May 2005
    Location
    In the mountains
    Posts
    341
    Thanks, Moagrius, it worked fine and fixed the problem.

    I just would like that Flash compiler could be a bit more clear when warning about errors... I sincerelly hate to bug you guys with such simple questions that I could figure out myself since the messages wouldn't be like enigmas...

    Thank you for the help and have a great 2010!
    Visit my business at http://www.ballooncreator.com - Software Tool For Party Balloons Online Design!

  4. #4
    ___________________
    Join Date
    May 2004
    Posts
    3,174

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