开发者

AS3 TimerEvents:

having problems adding parts of a snake on through timer events

package {

    import flash.display.Sprite;
    import flash.events.KeyboardEvent;
    import flash.events.TimerEvent;
    import flash.ui.Keyboard;
    import flash.utils.Timer;
    import flash.display.MovieClip;

    public class Snake extends MovieClip{

        private const SPEED         :uint = 50;//lower = faster
        private const snakeAttach   :uint = 400;//lower = faster
        private const DIM           :int = 71; //keep this number uneven to have the snake starting in the middle
        private const INITIAL_SIZE  :int = 3; //keep this lower then DIM/2

        private var stopped         :Boolean;
        private var left            :Boolean;
        private var right           :Boolean;
        private var up              :Boolean;
        private var down            :Boolean;
        private var size            :Number;
        private var food            :Sprite;
        private var tmr             :Timer;
        private var addSnake        :Timer;
        private var curI            :Number;
        private var curJ            :Number;
        private var snake           :Array;
        private var grid            :Array;

        public function Snake(){
            size = stage.stageWidth / DIM; //change grid size
            curI = curJ = Math.floor(DIM * 0.5); //change grid size

            initSnake();
            fillGrid();


            addSnake = new Timer(snakeAttach);
            addSnake.addEventListener(TimerEvent.TIMER,placeFood);
            addSnake.start();

            tmr = new Timer(SPEED);
            tmr.addEventListener(TimerEvent.TIMER,move);
            tmr.start();

            stage.addEventListener(KeyboardEvent.KEY_DOWN,changeDir);
        }

        private function fillGrid():void{  //grid
            grid = Make2DArray();

            for (var i:uint = 0; i < DIM; i++){
                for (var j:uint = 0; j < DIM; j++){
                    var sp:Sprite = new Sprite();
                    sp.graphics.beginFill(0xF0F0F0);
                    sp.graphics.lineStyle(1,0xF5F5F5);
                    sp.graphics.drawRect(0, 0, size  - 1, size - 1);
                    sp.x = i * size;
                    sp.y = j * size;
                    addChild(sp);
                    grid[i][j] = sp;
                }
            }   
        }

        private function Make2DArray():Array{   //for the grid
            var a:Array = new Array(DIM);
            for(var i:uint = 0; i < a.length; i++){
                a[i] = new Array(DIM);
            }   
            return a;
        }

        private function initSnake():void{ //initialises the snake
            var center:Number = Math.floor(DIM * 0.5) * size;

            snake = new Array(INITIAL_SIZE);

            for (var i:uint = 0; i < INITIAL_SIZE; i++){
                var sp:Sprite = makeItem();      //adds a body part of makeItem 
                sp.x = center;
                sp.y = center + i * size;
                addChild(sp); //adds to the stage
                snake[i] = sp;  //sets the index to one
            }

            snake.reverse();
        }

        private function makeItem(c:uint = 0):Sprite{  //graphics for item
            var s:Sprite = new Sprite(); 
            s.graphics.beginFill(c);
            s.graphics.lineStyle(2,0x333333);
            s.graphics.drawRect(0, 0, size, size);
            return s;
        }

        private function placeFood(event:TimerEvent):void{   
            var rndI:uint = Math.floor(Math.random() * DIM);  //sets a random integer based on the the floor
            var rndJ:uint = Math.floor(Math.random() * DIM);

            var rndX:Number = grid[rndI][rndJ].x; // sets a grid position for the food item to go
            var rndY:Number = grid[rndI][rndJ].y;

            if (food != null) removeChild(food);  //if there is food on the grid removes the food from the board 

            food = makeItem(Math.random() * 0xFFFFFF);// random color
            food.x = rndX;
            food.y = rndY;

            addChild(food); //adds the food to the board

            for (var i:uint = 0; i < snake.length; i++){
                if (rndY == snake[i].y && rndX == snake[i].x){ 

                }
            } 
        }

        private function move(e:TimerEvent):void{
            if (left){
                curI -= 1;
            }else if (right){
                curI += 1;
            }
            if (up){
                cu开发者_高级运维rJ -= 1;
            }else if (down){
                curJ += 1;
            }

            if (left || right || up || down){
                var s:Sprite = makeItem();

                if (curI > DIM - 1) curI = 0;
                if (curJ > DIM - 1) curJ = 0;

                if (curI < 0) curI = DIM - 1;
                if (curJ < 0) curJ = DIM - 1;

                s.x = grid[curI][curJ].x;
                s.y = grid[curI][curJ].y;

                addChild(s);
                snake.push(s);


                 if (Math.floor(s.x) == Math.floor(food.x) && Math.floor(s.y) == Math.floor(food.y) ){ 
                 //placeFood(); } 
                 else if((tmr.currentCount % 10) > 0) { removeChild(snake[0]); snake.shift(); }
            }

        }

        private function changeDir(e:KeyboardEvent):void{
            if(e.keyCode == Keyboard.LEFT)  {if (!right){left = true;  up = false; down = false; right = false;}}
            if(e.keyCode == Keyboard.UP)    {if (!down) {left = false; up = true;  down = false; right = false;}}
            if(e.keyCode == Keyboard.RIGHT) {if (!left) {left = false; up = false; down = false; right = true;}}
            if(e.keyCode == Keyboard.DOWN)  {if (!up)   {left = false; up = false; down = true;  right = false;}}
        }
    }
}


You need to add an event parameter to any function that's handling a TimerEvent.

private function placeFood(event:TimerEvent):void {


It would be nice if you had it in model-view-controller framework. You have a lot going on and the different listeners might not match up perfectly. Have the view class set the swf, add the snake to the stage, make the grid, hold listeners for inputs. Input then get processed by controller. Remember the View is basically the class that's added to stage and most follows the flow of the game. Have a _controller.startTimer(); function. Then all the stuff that you want to happen can be processed in the controller. And when you want the things to explode, change a model variable that tells the view to do that. Controller makes changes to all the variables (including the array of items on the field) stored in model, and when it's done making calculations it would change one of model's variables. Model would then trigger the changeHanldler function that calls view to move something, change some text, play a sound, switch screens, wiggle the snake, play your own tweened out snake wiggling....

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜