A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: snake AS3 found on web but somethings wrong

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    7

    snake AS3 found on web but somethings wrong

    http://www.youtube.com/watch?v=-PJczdqs-w8
    SnakeTutorial.flaSNAKE CODE.txt

    there is the Main.As you should as a script.


    my error is this, Untitled.jpg
    could you fix it?

  2. #2
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi, this will at least get rid of the error codes for you,
    PHP Code:
    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 
    gFood:Food;
            var 
    head:SnakePart;
            var 
    SnakeDirection:String;
            var 
    snake:Array;

            public function 
    Main()
            {
                if (
    stage)
                {
                    
    init();
                }
                else
                {
                    
    addEventListener(Event.ADDED_TO_STAGEinit);
                }
            }
            function 
    init(e:Event):void
            
    {
                
    removeEventListener(Event.ADDED_TO_STAGEinit);
                
    stage.addEventListener(KeyboardEvent.KEY_UP onKeyUp);
                
    stage.addEventListener(KeyboardEvent.KEY_DOWN onKeyDown);
                
    //Initialize everything!
                
    vx 1;
                
    vy 0;
                
    score 0;
                
    snake = new Array();
                
    SnakeDirection "";
                
    //add food to the stage
                
    addFood();
                
    //add snakes head to the stage
                
    head = new SnakePart();
                
    head.stage.stageWidth 2;
                
    head.stage.stageHeight 2;
                
    snake.push(head);
                
    addChild(head);

                
    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
            
    {
                
    gFood = new Food();
                
    gFood.50 Math.random()*(stage.stageWidth-100);
                
    gFood.50 Math.random()*(stage.stageHeight-100);
                
    addChild(gFood);
            }
            
    //this function will reset the game
            
    function reset():void
            
    {
                
    removeChild(gFood);
                
    addFood();
                
    head.stage.stageWidth 2;
                
    head.stage.stageHeight 2;
                
    vx 1;
                
    vy 0;

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

    and embed the font for the score filed

  3. #3
    Registered User
    Join Date
    Feb 2014
    Posts
    7
    if (stage)
    {
    init();
    }
    else
    theres an error there in init() it says incorrect number of arguments, and what do you mean by embed the font for the score field..

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