Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 我如何让吃豆人在按键时改变方向_Actionscript 3 - Fatal编程技术网

Actionscript 3 我如何让吃豆人在按键时改变方向

Actionscript 3 我如何让吃豆人在按键时改变方向,actionscript-3,Actionscript 3,因此,我创建了一个flash snake游戏,我可以让pacman snake四处移动,但是我如何修复这个代码,使面部在我单击“左”时转到左帧,即第2帧,然后在我单击键盘上的“下”时转到“下”,即第3帧,然后再转到“上”,即第4帧 import flash.display.*; import flash.events.*; import flash.text.*; import flash.utils.Timer; import flash.geom.Point; import flash.di

因此,我创建了一个flash snake游戏,我可以让pacman snake四处移动,但是我如何修复这个代码,使面部在我单击“左”时转到左帧,即第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
    }
导入flash.display.*;
导入flash.events.*;
导入flash.text.*;
导入flash.utils.Timer;
导入flash.geom.Point;
导入flash.display.Stage;
导入flash.ui.Keyboard;
导入flash.events.Event;
导入flash.events.KeyboardEvent;
var评分:int;
分数=-10;
阶段。焦点=阶段//这是编码,使箭头键在游戏屏幕上直接工作,无需点击
//常数
常数gridSize:int=20;
常量leftWall:int=100//当蛇死在左边墙上时,我已经改变了尺寸,使其适合我的图像边界,并使其适合
右墙常数:int=580//当蛇死在左边墙上时的尺寸,我已经改变了这些尺寸,以适应我图像的边界,并使其适合
顶墙常数:int=75//当蛇死在左边墙上时的尺寸,我已经改变了这些尺寸,以适应我图像的边界,并使其适合
const底墙:int=385//当蛇死在左边墙上时的尺寸,我已经改变了这些尺寸,以适应我图像的边界,并使其适合
常数起始速度:int=200//当蛇死在左边墙上时的尺寸,我已经改变了这些尺寸,以适应我图像的边界,并使其适合
常量起始点:点=新点(260180);
//游戏状态
var配子精灵:精灵;
变量食品:食品=新食品();
var-gameTimer:定时器;
var蛇零件:数组;
//蛇行速度
var snakeMoveX:编号=0;
var snakeMoveY:编号=0;
var nextMoveX:Number=1;
var nextMoveY:编号=0;
//创建游戏精灵
游戏精灵=新精灵();
addChild(游戏精灵);
//创建snake的第一部分
snakeParts=新数组();
var firstSnakePart=新蛇头()//我已经改变了这一点,使蛇的两个不同的部分,以便它适合我的游戏计划
firstSnakePart.x=起点.x;
firstSnakePart.y=起始点.y;
蛇形零件。推(第一个蛇形零件);
配子精灵。addChild(第一蛇部分);
//创造第一食物
配子精灵。addChild(食物);
placeFood();
//设置侦听器和计时器
stage.addEventListener(键盘事件、按键向下、按键向下功能);
游戏计时器=新计时器(开始速度);
gameTimer.addEventListener(TimerEvent.TIMER,moveSnake);
gameTimer.start();
//注册按键
功能keyDownFunction(事件:KeyboardEvent){
如果(event.keyCode==37){
如果(snakeMoveX!=1){
nextMoveX=-1;
nextMoveY=0;
蛇头2.蛇头(2);
}
}else if(event.keyCode==39){
如果(snakeMoveX!=-1){
nextMoveX=1;
nextMoveY=0;
蛇头2.蛇头(1);
}
}else if(event.keyCode==38){
如果(蛇移动!=1){
nextMoveY=-1;
nextMoveX=0;
蛇头2.蛇头(4);
}
}else if(event.keyCode==40){
如果(蛇移动!=-1){
nextMoveY=1;
nextMoveX=0;
蛇头2.蛇头(3);
}
}
}
//将蛇向正确方向移动一个空间
函数moveSnake(事件:TimerEvent){
snakeMoveX=下一个移动;
snakeMoveY=下一个移动;
var newX:Number=snakeParts[0]。x+snakeMoveX*gridSize;
变量newY:Number=snakeParts[0]。y+snakeMoveY*gridSize;
//检查有无碰撞
if((newX>右墙)| |(newX底墙)| |(newY0;i--){
蛇形零件[i].x=蛇形零件[i-1].x;
蛇类零件[i].y=蛇类零件[i-1].y;
}
}
//看看头是否碰到了尾巴的任何部分
函数hitTail(){
对于(变量i:int=1;iyou us