在通过计时器事件添加部分蛇时遇到问题
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){
curJ -= 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;}}
}
}
}
答案 0 :(得分:1)
您需要将事件参数添加到处理TimerEvent的任何函数。
private function placeFood(event:TimerEvent):void {
答案 1 :(得分:0)
如果你在模型 - 视图 - 控制器框架中拥有它会很好。你有很多事情发生,不同的听众可能无法完美匹配。让视图类设置swf,将蛇添加到舞台,制作网格,保持监听器输入。输入然后由控制器处理。请记住,View基本上是添加到舞台的类,大多数都遵循游戏流程。有一个_controller.startTimer();功能。然后,您想要发生的所有内容都可以在控制器中处理。当你想让事情爆炸时,改变一个告诉视图的模型变量。 Controller对存储在模型中的所有变量(包括字段上的项目数组)进行更改,并且在完成计算时,它将更改模型的一个变量。然后模型将触发changeHanldler函数,该函数调用视图来移动内容,更改一些文本,播放声音,切换屏幕,摆动蛇,播放自己的扭曲的蛇摆动....