0
votes

I'm trying to make an edit of this bad snake version for actionscript to use as entertainment in a website I'm working with. My issue is a 1010 error which doesn't seem to make sense to me. I've tried debugging without success.

The first output that appears printed is the following:

Fonts should be embedded for any text that may be edited at runtime, other than text with the "Use Device Fonts" setting. Use the Text > Font Embedding command to embed fonts.

And whenever my snake eats food (so to say), I get the error:

TypeError: Error #1010: A term is undefined and has no properties. at Main/onEnterFrame()

The full code is this:

package{
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event; //used for ENTER_FRAME event

public class Main extends MovieClip{
    const speed:int = 20;//speed of the snake
    var score:int;
    var vx:int;
    var vy:int;
    var head:SnakePart;
    var gFood;
    var SnakeDirection:String;
    var snake:Array;

    public function Main(){
        init();
    }
    function init():void {
        //Initialize everything!
        vx = 1; vy = 0;
        score = 1;
        snake = new Array();
        SnakeDirection = "";
        //add food to the stage
        addFood();
        //add snakes head to the stage
        head = new SnakePart();
        head.x = stage.stageWidth/2;
        head.y = stage.stageHeight/2;
        snake.push(head);
        addChild(head);

        stage.addEventListener(KeyboardEvent.KEY_UP , onKeyUp);
        stage.addEventListener(KeyboardEvent.KEY_DOWN , onKeyDown);
        addEventListener(Event.ENTER_FRAME , onEnterFrame);
        //ENTER_FRAME listener is attached to main class and not to the stage directly
    }
    //This function will add food to the stage
    function addFood():void {
            if(score%2==0){
                gFood = new SnakePart();
            }else{
                gFood = new Food();
            }
        gFood.x = 50 + Math.random()*(stage.stageWidth-100);
        gFood.y = 50 + Math.random()*(stage.stageHeight-100);
        addChild(gFood);
    }
    //this function will reset the game
    function reset():void {
        removeChild(gFood);
        addFood();
        head.x = stage.stageWidth/2;
        head.y = stage.stageHeight/2;
        vx = 1;vy = 0;

        for(var i = snake.length-1;i>0;--i){
            removeChild(snake[i]);
            snake.splice(i,1);
        }
    }
    function onKeyDown(event:KeyboardEvent):void{
        if(event.keyCode == Keyboard.LEFT){
           SnakeDirection = "left";
        }else if (event.keyCode == Keyboard.RIGHT) {
           SnakeDirection = "right";
        }else if (event.keyCode == Keyboard.UP) {
            SnakeDirection = "up";
        }else if (event.keyCode == Keyboard.DOWN) {
            SnakeDirection = "down";
        }
    }
    function onKeyUp(event:KeyboardEvent):void {
        if(event.keyCode == Keyboard.LEFT) {
            SnakeDirection = "";
        }else if(event.keyCode == Keyboard.RIGHT) {
            SnakeDirection = "";
        }else if(event.keyCode == Keyboard.UP ) {
            SnakeDirection = "";
        }else if(event.keyCode == Keyboard.DOWN){
            SnakeDirection = "";
        }
    }
    function onEnterFrame(event:Event):void {
        //setting direction of velocity
        if(SnakeDirection == "left" && vx != 1) {
            vx = -1;
            vy = 0;
            //head.rotate(-90);
        }else if(SnakeDirection == "right" && vx != -1) {
            vx = 1;
            vy = 0;
        }else if(SnakeDirection == "up" && vy != 1) {
            vx = 0;
            vy = -1;
        }else if(SnakeDirection == "down" && vy != -1) {
            vx = 0;
            vy = 1;
        }

        //collison with stage
        if(head.x - head.width/2 <= 0){
            score = 0;
            reset();
        }
        if(head.x + head.width/2 >= stage.stageWidth){
            score = 0;
            reset();
        }
        if(head.y - head.height/2 <= 0){
            score = 0;
            reset();
        }
        if(head.y + head.height/2 >= stage.stageHeight){
            score = 0;
            reset();
        }
        //move body of the snake
        for(var i = snake.length-1;i>0;--i){
            snake[i].x = snake[i-1].x;
            snake[i].y = snake[i-1].y;
        }
        //changing the position of snake's head
        head.x += vx*speed;
        head.y += vy*speed;
        //collision with tail
        for(var i = snake.length-1;i>=1;--i){
            if(snake[0].x == snake[i].x && snake[0].y == snake[i].y){
                reset();
                break;
            }
        }
        //collision with food
        if(head.hitTestObject(gFood)){
            score += 1;
            removeChild(gFood);
            addFood();
            var bodyPart;
            if(score%2==0){
                bodyPart = new Food();
            }else{
                bodyPart = new SnakePart();
            }
            bodyPart.x = snake[snake.length - 10].x;
            bodyPart.y = snake[snake.length - 10].y;
            snake.push(bodyPart);
            addChild(bodyPart);
        }
        //display scores
        txtScore.text = String(score);
    }
}

}

Feel free to ask anything!

1
first things first... install a flash debug version of the player so you get the exact line for your Runtime Error. (adobe.com/support/flashplayer/downloads.html) then post the output of the error so its easier to see where the issue lies. Its appears that in your onEnterFrame method there is a null parameter, either: head, stage, snake or txtScore - mihai
@mihai thanks a bunch, that really helped. however I get a second problem. I'm trying to rotate the snake's head to fit the direction it's going. It tells me rotate is not a function head.rotate(-90); - Sebastian Alegre
@mihai update: I changed it to rotation. TypeError: Error #1006: value is not a function. - Sebastian Alegre
rotate is not a valid call. use head.rotation=90 instead, assuming head inherits from DisplayObject - mihai

1 Answers

0
votes

based on our conversation this seemed to be the solution

  1. for the fonts, find the textfields that are dynamic and in the properties panel select embed, then check the characters you need embedded otherwise they might not show up at runtime

  2. runtime 1010 error, it appears that in your onEnterFrame method there is a null parameter, either: head, stage, snake or txtScore.

suggest installing flash player debug to tell you exactly which line the error is on: http://www.adobe.com/support/flashplayer/downloads.html