Copy link to clipboard
Copied
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);
}
}
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Thank you for replying, I moved addEventListener(Event.ENTER_FRAME,onEnterFr); inside one of the fuctions. I'm still having the Error 1013, I don't understand the nesting problem, (Still newbie) can you explaing what I have to do.
Copy link to clipboard
Copied
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);
}
}
}
Copy link to clipboard
Copied
Feel like hitting my head of a wall.
It solved the 1013 Error though.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.