所以我创造了一个闪光蛇游戏,我可以让pacman蛇四处移动,但是我如何修复这个代码,以便当我点击左边时脸部将转到第2帧的左框架,然后去当我点击键盘上的第3帧时向下,然后向上,这是第4帧。
import flash.display.*;
import flash.events.*;
import flash.text.*;
import flash.utils.Timer;
import flash.geom.Point;
import flash.display.Stage;
import flash.ui.Keyboard;
import flash.events.Event;
import flash.events.KeyboardEvent;
var score:int;
score = -10;
stage.focus=stage; //This is coding to make the arrow keys work on the gamescreen straight away with no clicking
// constants
const gridSize:int = 20;
const leftWall:int =100; //There Dimensions for when the snake dies on the left wall i have changed these to fit around the border of my image and to make it suitable
const rightWall:int = 580; //Dimensions for when the snake dies on the left wall i have changed these to fit around the border of my image and to make it suitable
const topWall:int = 75; //Dimensions for when the snake dies on the left wall i have changed these to fit around the border of my image and to make it suitable
const bottomWall:int = 385; //Dimensions for when the snake dies on the left wall i have changed these to fit around the border of my image and to make it suitable
const startSpeed:int = 200; //Dimensions for when the snake dies on the left wall i have changed these to fit around the border of my image and to make it suitable
const startPoint:Point = new Point(260,180);
// game state
var gameSprite:Sprite;
var food:Food = new Food();
var gameTimer:Timer;
var snakeParts:Array;
// snake velocity
var snakeMoveX:Number = 0;
var snakeMoveY:Number = 0;
var nextMoveX:Number = 1;
var nextMoveY:Number = 0;
// create game sprite
gameSprite = new Sprite();
addChild(gameSprite);
// create first part of snake
snakeParts = new Array();
var firstSnakePart = new SnakeHead(); //I have changed this to make two different parts to the snake so that it fits to my plans of the game
firstSnakePart.x = startPoint.x;
firstSnakePart.y = startPoint.y;
snakeParts.push(firstSnakePart);
gameSprite.addChild(firstSnakePart);
// create first food
gameSprite.addChild(food);
placeFood();
// set up listener and timer
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownFunction);
gameTimer = new Timer(startSpeed);
gameTimer.addEventListener(TimerEvent.TIMER,moveSnake);
gameTimer.start();
// register key presses
function keyDownFunction(event:KeyboardEvent) {
if (event.keyCode == 37) {
if (snakeMoveX != 1) {
nextMoveX = -1;
nextMoveY = 0;
snakeHead2.gotoAndStop(2);
}
} else if (event.keyCode == 39) {
if (snakeMoveX != -1) {
nextMoveX = 1;
nextMoveY = 0;
snakeHead2.gotoAndStop(1);
}
} else if (event.keyCode == 38) {
if (snakeMoveY != 1) {
nextMoveY = -1;
nextMoveX = 0;
snakeHead2.gotoAndStop(4);
}
} else if (event.keyCode == 40) {
if (snakeMoveY != -1) {
nextMoveY = 1;
nextMoveX = 0;
snakeHead2.gotoAndStop(3);
}
}
}
// move snake one space in proper direction
function moveSnake(event:TimerEvent) {
snakeMoveX = nextMoveX;
snakeMoveY = nextMoveY;
var newX:Number = snakeParts[0].x + snakeMoveX*gridSize;
var newY:Number = snakeParts[0].y + snakeMoveY*gridSize;
// check for collision
if ((newX > rightWall) || (newX < leftWall) || (newY > bottomWall) || (newY < topWall)) {
gameOver();
} else if (hitTail()) {
gameOver();
} else {
// check for food
if ((newX == food.x) && (newY == food.y)) { // This is placed into the game to check if any food is on the GameScreen, if there isn't then it will spawn the food.
placeFood();
newSnakePart();
gameTimer.delay -= 2;
}
placeTail();
snakeParts[0].x = newX;
snakeParts[0].y = newY;
}
}
// randomly place the food
function placeFood() {
score = score + 10; //This adds 10 points for every piece of food being picked up
scoreField.text = score.toString();
var startPoint: int ;
startPoint = 80; //This makes the food spawn at least 80 pixels into the stage and around the stage meaning it won't go outside the borders
var foodX:int = Math.floor(Math.random()* ( rightWall-leftWall)/gridSize)*gridSize + startPoint;
var foodY:int = Math.floor(Math.random()* ( bottomWall-topWall)/gridSize)*gridSize + startPoint;
food.x = foodX;
food.y = foodY;
}
// add one sprite to snake
function newSnakePart() {
var newPart:SnakePart = new SnakePart();
gameSprite.addChild(newPart);
snakeParts.push(newPart);
var mySound:Sound = new foodSound(); //Adds sound to the game when food spawns and the sound is a mario sound of picking up coins and plays when the food respawns
mySound.play(); //Just a instruction to make my sound play
}
// place all parts of snake
function placeTail() {
for(var i:int=snakeParts.length-1;i>0;i--) {
snakeParts[i].x = snakeParts[i-1].x;
snakeParts[i].y = snakeParts[i-1].y;
}
}
// see if the head hit any part of the tail
function hitTail() {
for(var i:int=1;i<snakeParts.length;i++) {
if ((snakeParts[0].x == snakeParts[i].x) && (snakeParts[0].y == snakeParts[i].y)) {
return true;
}
}
return false;
}
// stop game
function gameOver() {
stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyDownFunction);
gotoAndStop (3); //Once the user has died on the game, then this will take them to the end frame which is the third frame
gameTimer.stop();
removeChild (gameSprite) ; //This was added so that when the game does restart no food was shown from the previous games and doesn't cause any distractions
}