A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: [RESOLVED] Can't set array values inside a class

  1. #1
    Senior Member somlemeg's Avatar
    Join Date
    Aug 2000
    Posts
    168

    resolved [RESOLVED] Can't set array values inside a class

    This is problably a noob question, sorry.
    I have this code inside a public class:
    Code:
    // background
    const ww = 480;
    const hh = 272;
    
    // data for the target
    var targDisp:int = 25;
    var targWidth:int = 50;
    var targHeight:int = 50;
    var targMargin:int = 10;
    
    // Define positions
    var targPositions:Array = new Array();
    
    targPositions[0] = [(ww / 2) - (targWidth / 2),targMargin];// upper mid position
    targPositions[1] = [(ww / 2) - (targWidth / 2),targHeight - targMargin];// lower mid position
    targPositions[2] = [targMargin,(hh / 2) - (targHeight / 2)];// left
    targPositions[3] = [targWidth - targMargin,(hh / 2) - (targHeight / 2)];// right
    When running the code I get:
    Code:
    Line 51	1120: Access of undefined property targPositions.
    And so on. I get no error for declaring the targPositions aray, however get lots of error when setting values to it.

    If I place the code in the main timeline it works fine.

  2. #2
    Mod cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    I don't get any error:
    PHP Code:
    package 
    {
        
    import flash.display.Sprite;
        public class 
    Arraytest extends Sprite
        
    {
            public function 
    Arraytest()
            {
                
    // background
                
    const ww 480;
                const 
    hh 272;

                
    // data for the target
                
    var targDisp:int 25;
                var 
    targWidth:int 50;
                var 
    targHeight:int 50;
                var 
    targMargin:int 10;

                
    // Define positions
                
    var targPositions:Array = new Array();

                
    targPositions[0] = [(ww 2) - (targWidth 2),targMargin];// upper mid position
                
    targPositions[1] = [(ww 2) - (targWidth 2),targHeight targMargin];// lower mid position
                
    targPositions[2] = [targMargin,(hh 2) - (targHeight 2)];// left
                
    targPositions[3] = [targWidth targMargin,(hh 2) - (targHeight 2)];// right

                
    trace(targPositions[0]);
            }
        }

    Arraytest is the Document class here.
    - The right of the People to create Flash movies shall not be infringed. -

  3. #3
    Senior Member somlemeg's Avatar
    Join Date
    Aug 2000
    Posts
    168
    Ooops, I had placed the code right under the class. Why can I define variables there, but not arrays?

    PHP Code:
    package 
    {
        
    import flash.display.Sprite;
        public class 
    Arraytest extends Sprite
        
    {
                
    // background
                
    const ww 480;
                const 
    hh 272;

                
    // data for the target
                
    var targDisp:int 25;
                var 
    targWidth:int 50;
                var 
    targHeight:int 50;
                var 
    targMargin:int 10;

                
    // Define positions
                
    var targPositions:Array = new Array();

                
    targPositions[0] = [(ww 2) - (targWidth 2),targMargin];// upper mid position
                
    targPositions[1] = [(ww 2) - (targWidth 2),targHeight targMargin];// lower mid position
                
    targPositions[2] = [targMargin,(hh 2) - (targHeight 2)];// left
                
    targPositions[3] = [targWidth targMargin,(hh 2) - (targHeight 2)];// right

                
    public function Arraytest()
                {
                    
    trace (targPositions[0][0])
                }
        }


  4. #4
    Mod cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    You need to place the execution within the function frame:

    public function Arraytest()
    {
    targPositions[0] = [(ww / 2) - (targWidth / 2),targMargin];// upper mid position
    targPositions[1] = [(ww / 2) - (targWidth / 2),targHeight - targMargin];// lower mid position
    targPositions[2] = [targMargin,(hh / 2) - (targHeight / 2)];// left
    targPositions[3] = [targWidth - targMargin,(hh / 2) - (targHeight / 2)];// right
    trace(targPositions[0][0]);
    }

    Also you should define your variables as public or private and not let the compiler guess it, for example

    private var targMargin:int = 10;
    - The right of the People to create Flash movies shall not be infringed. -

  5. #5
    Senior Member somlemeg's Avatar
    Join Date
    Aug 2000
    Posts
    168
    Thank you very much!




    P.s: the link to your book in the footer signature does not seem to work.

  6. #6
    Mod cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    Thanks
    - The right of the People to create Flash movies shall not be infringed. -

Tags for this Thread

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