Skip navigation
JamieBilsl1972
Currently Being Moderated

Error 1013: The private attribute may be used only on class property definitions

May 7, 2012 5:59 PM

Tags: #on #class #only #private #be #may #property #the #attribute #used #definitions

Been trying to solve this problem for the past 4 hours, been searching all over different forums and I still don't get it, I'm a complete newbie with AS3 and just starting to learn it. Can someone tell me what I have done wrong to get this error.

 

package {

          import flash.display.Sprite;

          import flash.events.Event;

          public class Main extends Sprite {

                    private const FIELD_WIDTH:uint=16;

                    private const FIELD_HEIGHT:uint=12;

                    private const TILE_SIZE:uint=40;

                    private var the_snake:the_snake_mc;

                    private var snakeDirection:uint;

                    private var snakeContainer:Sprite= new Sprite();

                    private var bg:bg_mc=new bg_mc();

                    public function Main() {

                              addChild(bg);

                              placeSnake(); }

                              addEventListener(Event.ENTER_FRAME,onEnterFr);

private function placeSnake():void {

          addChild(snakeContainer);

          var col:uint=Math.floor(Math.random()*(FIELD_WIDTH-10))+5;

          var row:uint=Math.floor(Math.random()*(FIELD_HEIGHT-10))+5;

          snakeDirection=Math.floor(Math.random()*4);

          the_snake=new the_snake_mc(col*TILE_SIZE,row*TILE_SIZE,snakeDirection+1);

          snakeContainer.addChild(the_snake);

          switch (snakeDirection) {

                    case 0 : // facing left

                    trace("left");

                    the_snake = new the_snake_mc((col+1)*TILE_SIZE,row*TILE_SIZE,6);

                    snakeContainer.addChild(the_snake);

                    the_snake = new the_snake_mc((col+2)*TILE_SIZE,row*TILE_SIZE,6);

                    snakeContainer.addChild(the_snake);

                    break;

                    case 1 : // facing up

                    trace ("up");

                    the_snake = new the_snake_mc(col*TILE_SIZE,(row+1)*TILE_SIZE,5);

                    snakeContainer.addChild(the_snake);

                    the_snake = new the_snake_mc(col*TILE_SIZE,(row+2)*TILE_SIZE,5);

                    snakeContainer.addChild(the_snake);

                    break;

                    case 2 : // facing down

                    trace ("down");

                    the_snake = new the_snake_mc((col-1)*TILE_SIZE.row*TILE_SIZE,6);

                    snakeContainer.addChild(the_snake);

                    the_snake = new the_snake_mc((col-2)*TILE_SIZE.row*TILE_SIZE,6);

                    snakeContainer.addChild(the_snake);

                    break

                    case 3 : // facing right

                    trace ("right");

                    the_snake = new the_snake_mc(col*TILE_SIZE,(row-1)*TILE_SIZE,5);

                    snakeContainer.addChild(the_snake);

                    the_snake = new the_snake_mc(col*TILE_SIZE,(row-2)*TILE_SIZE,5);

                    snakeContainer.addChild(the_snake);

                    break;

private function onEnterFr(e:Event) {     <<   ERROR ON THIS LINE

          var the_head:the_snake_mc=snakeContainer.addChildAt(0) as the_snake_mc;

          var new_piece:the_snake_mc=new the_snake_mc(the_head.x,the_head.y,1);

          snakeContainer.addChildAt(new_piece,1);

          var the_body:the_snake_mc=snakeContainer.getChildAt(2) as the_snake_mc;

          var p:uint=snakeContainer.numChildren;

          var the_tail:the_snake_mc=snakeContainer.getChildAt(p-1) as the_snake_mc;

          var the_new_tail:the_snake_mc=snakeContainer.getChildAt(p-2) as the_snake_mc;

          the_head.moveHead(snakeDirection,TILE_SIZE);

          // brute force

          if (is_up(new_piece,the_head)&&is_down(new_piece,the_body)) {

                    new_piece.gotoAndStop(5);

          }

          if (is_down(new_piece,the_head)&&is_up(new_piece,the_body)) {

                    new_piece.gotoAndStop(5);

          }

          if (is_left(new_piece,the_head)&&is_right(new_piece,the_body)) {

                    new_piece.gotoAndStop(6);

          }

          if (is_right(new_piece,the_head)&&is_left(new_piece,the_body)) {

                    new_piece.gotoAndStop(6);

          }

          // end of brute force

          snakeContainer.removeChild(the_tail);

          }

 

          }

}

 
Replies
  • Currently Being Moderated
    May 7, 2012 6:19 PM   in reply to JamieBilsl1972

    You seem to have some code misplaced.  Just after your Main function you have a line of code...

     

    addEventListener(Event.ENTER_FRAME,onEnterFr);

     

    You probably want to move that inside one fo the functions.

     

    As far as the line you say is the problem, you appear to have that function nested within the placeSnake function.  Do not nest functions like that.

     
    |
    Mark as:
  • Currently Being Moderated
    May 8, 2012 3:18 AM   in reply to JamieBilsl1972

    does this solve your problem

     

    package

    {

              import flash.display.Sprite;

              import flash.events.Event;

     

              public class Main extends Sprite

              {

                        private const FIELD_WIDTH:uint = 16;

                        private const FIELD_HEIGHT:uint = 12;

                        private const TILE_SIZE:uint = 40;

                        private var the_snake:the_snake_mc;

                        private var snakeDirection:uint;

                        private var snakeContainer:Sprite = new Sprite();

                        private var bg:bg_mc = new bg_mc();

     

                        public function Main()

                        {

                                  addChild(bg);

                                  placeSnake();

                                  addEventListener(Event.ENTER_FRAME, onEnterFr);

                        }

     

                        private function placeSnake():void

                        {

                                  addChild(snakeContainer);

                                  var col:uint = Math.floor(Math.random() * (FIELD_WIDTH - 10)) + 5;

                                  var row:uint = Math.floor(Math.random() * (FIELD_HEIGHT - 10)) + 5;

                                  snakeDirection = Math.floor(Math.random() * 4);

                                  the_snake = new the_snake_mc(col * TILE_SIZE, row * TILE_SIZE, snakeDirection + 1);

                                  snakeContainer.addChild(the_snake);

     

                                  switch (snakeDirection)

                                  {

                                            case 0: // facing left

                                                      trace("left");

                                                      the_snake = new the_snake_mc((col + 1) * TILE_SIZE, row * TILE_SIZE, 6);

                                                      snakeContainer.addChild(the_snake);

                                                      the_snake = new the_snake_mc((col + 2) * TILE_SIZE, row * TILE_SIZE, 6);

                                                      snakeContainer.addChild(the_snake);

                                                      break;

     

                                            case 1: // facing up

                                                      trace("up");

                                                      the_snake = new the_snake_mc(col * TILE_SIZE, (row + 1) * TILE_SIZE, 5);

                                                      snakeContainer.addChild(the_snake);

                                                      the_snake = new the_snake_mc(col * TILE_SIZE, (row + 2) * TILE_SIZE, 5);

                                                      snakeContainer.addChild(the_snake);

                                                      break;

     

                                            case 2: // facing down

                                                      trace("down");

                                                      the_snake = new the_snake_mc((col - 1) * TILE_SIZE.row * TILE_SIZE, 6);

                                                      snakeContainer.addChild(the_snake);

                                                      the_snake = new the_snake_mc((col - 2) * TILE_SIZE.row * TILE_SIZE, 6);

                                                      snakeContainer.addChild(the_snake);

                                                      break;

     

                                            case 3: // facing right

                                                      trace("right");

                                                      the_snake = new the_snake_mc(col * TILE_SIZE, (row - 1) * TILE_SIZE, 5);

                                                      snakeContainer.addChild(the_snake);

                                                      the_snake = new the_snake_mc(col * TILE_SIZE, (row - 2) * TILE_SIZE, 5);

                                                      snakeContainer.addChild(the_snake);

                                                      break;

                                  }

                        }

     

                        private function onEnterFr(e:Event)

                        {

                                  var the_head:the_snake_mc = snakeContainer.addChildAt(0) as the_snake_mc;

                                  var new_piece:the_snake_mc = new the_snake_mc(the_head.x, the_head.y, 1);

                                  snakeContainer.addChildAt(new_piece, 1);

     

                                  var the_body:the_snake_mc = snakeContainer.getChildAt(2) as the_snake_mc;

                                  var p:uint = snakeContainer.numChildren;

                                  var the_tail:the_snake_mc = snakeContainer.getChildAt(p - 1) as the_snake_mc;

                                  var the_new_tail:the_snake_mc = snakeContainer.getChildAt(p - 2) as the_snake_mc;

                                  the_head.moveHead(snakeDirection, TILE_SIZE);

     

                                  // brute force

                                  if (is_up(new_piece, the_head) && is_down(new_piece, the_body))

                                  {

                                            new_piece.gotoAndStop(5);

                                  }

                                  if (is_down(new_piece, the_head) && is_up(new_piece, the_body))

                                  {

                                            new_piece.gotoAndStop(5);

                                  }

                                  if (is_left(new_piece, the_head) && is_right(new_piece, the_body))

                                  {

                                            new_piece.gotoAndStop(6);

                                  }

                                  if (is_right(new_piece, the_head) && is_left(new_piece, the_body))

                                  {

                                            new_piece.gotoAndStop(6);

                                  }

                                  // end of brute force

                                  snakeContainer.removeChild(the_tail);

                        }

     

              }

    }

     
    |
    Mark as:
  • Currently Being Moderated
    May 8, 2012 4:27 AM   in reply to JamieBilsl1972

    Nesting a function is when you place one function within another function as you show in your posting.  What _spoboyle has offered is the unnested version of it.

     
    |
    Mark as:
  • Currently Being Moderated
    May 8, 2012 4:49 AM   in reply to JamieBilsl1972

    Errors are like that... some errors take precedence such that othersa are not reported at first.  It looks like you have numerous errors that you need to fix.  You will need to tackle them one at a time, so consider it an opportunity to learn.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points