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);
}
}
}
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.
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);
}
}
}
North America
Europe, Middle East and Africa
Asia Pacific