ActionScript 3 蛇
ActionScript 3 Snake
我正在尝试获得贪吃蛇的双人版本 运行 但我无法让第二条贪吃蛇工作,玩家 1 使用 w、a、s 和 d,而玩家 2 使用方向键.带 w、a、s 和 d 的玩家 1 正在工作,带箭头的玩家 2 没有。相反,箭头键控制播放器 1 以及 w、a、s 和 d.I 认为它与 onEnterFrame 函数有关,但我已经盯着它看了好几天了,但我什么也看不到。我对 ActionScript 也很陌生,所以我只需要一些提示来指引我正确的方向,谢谢!
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 = 5;//speed of the snake
var vx:int;
var vy:int;
var gFood:Food;
var gFood2:Food2;
var head:SnakePart;
var head2:SnakePart2;
var SnakeDirection:String;
var Snake2Direction:String;
var snake:Array;
var snake2:Array;
public function Main(){
init();
}
function init():void {
//Initialize everything!
vx = 1; vy = 0;
snake = new Array();
snake2 = new Array();
SnakeDirection = "";
Snake2Direction = "";
//add food to the stage
addFood();
addFood2();
//add snakes head to the stage
head = new SnakePart();
head.x = stage.stageWidth/2;
head.y = stage.stageHeight/2;
snake.push(head);
addChild(head);
head2 = new SnakePart2();
head2.x = stage.stageWidth/14;
head2.y = stage.stageHeight/25;
snake2.push(head2);
addChild(head2);
stage.addEventListener(KeyboardEvent.KEY_UP , onKeyUp);
stage.addEventListener(KeyboardEvent.KEY_DOWN , onKeyDown);
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.x = 50 + Math.random()*(stage.stageWidth-100);
gFood.y = 50 + Math.random()*(stage.stageHeight-100);
addChild(gFood);
}
function addFood2():void {
gFood2 = new Food2();
gFood2.x = 50 + Math.random()*(stage.stageWidth-100);
gFood2.y = 50 + Math.random()*(stage.stageHeight-100);
addChild(gFood2);
}
//this function will reset the game
function reset():void {
removeChild(gFood);
addFood();
head.x = stage.stageWidth/2;
head.y = stage.stageHeight/2;
vx = 1;vy = 0;
removeChild(gFood2);
addFood2();
head.x = stage.stageWidth/4;
head.y = stage.stageHeight/4;
vx = 1;vy = 0;
for(var i = snake.length-1;i>0;--i){
removeChild(snake[i]);
snake.splice(i,1);
}
}
function onKeyDown(event : KeyboardEvent) : void {
//handle player 1
if (event.keyCode == Keyboard.A) {
SnakeDirection = "a";
} else if (event.keyCode == Keyboard.D) {
SnakeDirection = "d";
} else if (event.keyCode == Keyboard.W) {
SnakeDirection = "w";
} else if (event.keyCode == Keyboard.S) {
SnakeDirection = "s";
}
//handle player 2
if (event.keyCode == Keyboard.LEFT) {
Snake2Direction = "left";
} else if (event.keyCode == Keyboard.RIGHT) {
Snake2Direction = "right";
} else if (event.keyCode == Keyboard.UP) {
Snake2Direction = "up";
} else if (event.keyCode == Keyboard.DOWN) {
Snake2Direction = "down";
}
}
function onKeyUp(event : KeyboardEvent) : void {
//handle player 1
if (event.keyCode == Keyboard.A || event.keyCode == Keyboard.D || event.keyCode == Keyboard.W || event.keyCode == Keyboard.S) {
SnakeDirection = "";
}
//handle player 2
if (event.keyCode == Keyboard.LEFT ||event.keyCode == Keyboard.RIGHT || event.keyCode == Keyboard.UP || event.keyCode == Keyboard.DOWN) {
Snake2Direction = "";
}
}
function onEnterFrame(event:Event):void {
//setting direction of velocity
if(SnakeDirection == "a" && vx != 1) {
vx = -1;
vy = 0;
}else if(SnakeDirection == "d" && vx != -1) {
vx = 1;
vy = 0;
}else if(SnakeDirection == "w" && vy != 1) {
vx = 0;
vy = -1;
}else if(SnakeDirection == "s" && vy != -1) {
vx = 0;
vy = 1;
}
//setting direction of velocity
if(Snake2Direction == "left" && vx != 1) {
vx = -1;
vy = 0;
}else if(Snake2Direction == "right" && vx != -1) {
vx = 1;
vy = 0;
}else if(Snake2Direction == "up" && vy != 1) {
vx = 0;
vy = -1;
}else if(Snake2Direction == "down" && vy != -1) {
vx = 0;
vy = 1;
}
//collison with stage
if(head.x - head.width/2 <= 0){
reset();
}
if(head.x + head.width/2 >= stage.stageWidth){
reset();
}
if(head.y - head.height/2 <= 0){
reset();
}
if(head.y + head.height/2 >= stage.stageHeight){
reset();
}
//move body of the snake
for(var i = snake.length-1;i>0;--i){
snake[i].x = snake[i-1].x;
snake[i].y = snake[i-1].y;
}
for(var i = snake2.length-1;i>0;--i){
snake2[i].x = snake2[i-1].x;
snake2[i].y = snake2[i-1].y;
}
//changing the position of snake's head
head.x += vx*speed;
head.y += vy*speed;
//collision with tail
for(var i = snake.length-1;i>=1;--i){
if(snake[0].x == snake[i].x && snake[0].y == snake[i].y){
reset();
break;
}
}
for(var i = snake2.length-1;i>=1;--i){
if(snake2[0].x == snake2[i].x && snake2[0].y == snake2[i].y){
reset();
break;
}
}
//collision with food player 1
if(head.hitTestObject(gFood)){
removeChild(gFood);
addFood();
var bodyPart = new SnakePart();
bodyPart.x = snake[snake.length - 1].x;
bodyPart.y = snake[snake.length - 1].y;
snake.push(bodyPart);
addChild(bodyPart);
}
if(head.hitTestObject(gFood2)){
removeChild(gFood2);
addFood2();
var bodyPart = new SnakePart();
bodyPart.x = snake[snake.length - 1].x;
bodyPart.y = snake[snake.length - 1].y;
snake.push(bodyPart);
addChild(bodyPart);
}
//collision with food player 2
if(head.hitTestObject(gFood)){
removeChild(gFood);
addFood();
var bodyPart = new SnakePart2();
bodyPart.x = snake2[snake2.length - 1].x;
bodyPart.y = snake2[snake2.length - 1].y;
snake2.push(bodyPart);
addChild(bodyPart);
}
if(head.hitTestObject(gFood2)){
removeChild(gFood2);
addFood2();
var bodyPart = new SnakePart2();
bodyPart.x = snake2[snake2.length - 1].x;
bodyPart.y = snake2[snake2.length - 1].y;
snake2.push(bodyPart);
addChild(bodyPart);
}
}
}
}
首先,让我解释一下您在这段代码中的错误:
1.You 两条蛇的 vx
和 vy
相同!!这意味着两条蛇应该平行并朝相同的方向走!!
2.and 问题 1 不会发生,因为你为另一条蛇设置了 head
.x 和 y 而不是 head2
!
只是因为粗心...:)
所以:
1.add 变量 vx2
和 vy2
用于可怜的第二条蛇!
2.replace 第二个 setting direction of velocity
用这个:
if(Snake2Direction == "left" && vx2 != 1) {
vx2 = -1;
vy2 = 0;
}else if(Snake2Direction == "right" && vx2 != -1) {
vx2 = 1;
vy2 = 0;
}else if(Snake2Direction == "up" && vy2 != 1) {
vx2 = 0;
vy2 = -1;
}else if(Snake2Direction == "down" && vy2 != -1) {
vx2 = 0;
vy2 = 1;
}
3.do 你为第二条蛇留下的任何东西。(head2.x 和 y,第二条蛇的碰撞等)
我 H☺P E 这有帮助!
我正在尝试获得贪吃蛇的双人版本 运行 但我无法让第二条贪吃蛇工作,玩家 1 使用 w、a、s 和 d,而玩家 2 使用方向键.带 w、a、s 和 d 的玩家 1 正在工作,带箭头的玩家 2 没有。相反,箭头键控制播放器 1 以及 w、a、s 和 d.I 认为它与 onEnterFrame 函数有关,但我已经盯着它看了好几天了,但我什么也看不到。我对 ActionScript 也很陌生,所以我只需要一些提示来指引我正确的方向,谢谢!
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 = 5;//speed of the snake
var vx:int;
var vy:int;
var gFood:Food;
var gFood2:Food2;
var head:SnakePart;
var head2:SnakePart2;
var SnakeDirection:String;
var Snake2Direction:String;
var snake:Array;
var snake2:Array;
public function Main(){
init();
}
function init():void {
//Initialize everything!
vx = 1; vy = 0;
snake = new Array();
snake2 = new Array();
SnakeDirection = "";
Snake2Direction = "";
//add food to the stage
addFood();
addFood2();
//add snakes head to the stage
head = new SnakePart();
head.x = stage.stageWidth/2;
head.y = stage.stageHeight/2;
snake.push(head);
addChild(head);
head2 = new SnakePart2();
head2.x = stage.stageWidth/14;
head2.y = stage.stageHeight/25;
snake2.push(head2);
addChild(head2);
stage.addEventListener(KeyboardEvent.KEY_UP , onKeyUp);
stage.addEventListener(KeyboardEvent.KEY_DOWN , onKeyDown);
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.x = 50 + Math.random()*(stage.stageWidth-100);
gFood.y = 50 + Math.random()*(stage.stageHeight-100);
addChild(gFood);
}
function addFood2():void {
gFood2 = new Food2();
gFood2.x = 50 + Math.random()*(stage.stageWidth-100);
gFood2.y = 50 + Math.random()*(stage.stageHeight-100);
addChild(gFood2);
}
//this function will reset the game
function reset():void {
removeChild(gFood);
addFood();
head.x = stage.stageWidth/2;
head.y = stage.stageHeight/2;
vx = 1;vy = 0;
removeChild(gFood2);
addFood2();
head.x = stage.stageWidth/4;
head.y = stage.stageHeight/4;
vx = 1;vy = 0;
for(var i = snake.length-1;i>0;--i){
removeChild(snake[i]);
snake.splice(i,1);
}
}
function onKeyDown(event : KeyboardEvent) : void {
//handle player 1
if (event.keyCode == Keyboard.A) {
SnakeDirection = "a";
} else if (event.keyCode == Keyboard.D) {
SnakeDirection = "d";
} else if (event.keyCode == Keyboard.W) {
SnakeDirection = "w";
} else if (event.keyCode == Keyboard.S) {
SnakeDirection = "s";
}
//handle player 2
if (event.keyCode == Keyboard.LEFT) {
Snake2Direction = "left";
} else if (event.keyCode == Keyboard.RIGHT) {
Snake2Direction = "right";
} else if (event.keyCode == Keyboard.UP) {
Snake2Direction = "up";
} else if (event.keyCode == Keyboard.DOWN) {
Snake2Direction = "down";
}
}
function onKeyUp(event : KeyboardEvent) : void {
//handle player 1
if (event.keyCode == Keyboard.A || event.keyCode == Keyboard.D || event.keyCode == Keyboard.W || event.keyCode == Keyboard.S) {
SnakeDirection = "";
}
//handle player 2
if (event.keyCode == Keyboard.LEFT ||event.keyCode == Keyboard.RIGHT || event.keyCode == Keyboard.UP || event.keyCode == Keyboard.DOWN) {
Snake2Direction = "";
}
}
function onEnterFrame(event:Event):void {
//setting direction of velocity
if(SnakeDirection == "a" && vx != 1) {
vx = -1;
vy = 0;
}else if(SnakeDirection == "d" && vx != -1) {
vx = 1;
vy = 0;
}else if(SnakeDirection == "w" && vy != 1) {
vx = 0;
vy = -1;
}else if(SnakeDirection == "s" && vy != -1) {
vx = 0;
vy = 1;
}
//setting direction of velocity
if(Snake2Direction == "left" && vx != 1) {
vx = -1;
vy = 0;
}else if(Snake2Direction == "right" && vx != -1) {
vx = 1;
vy = 0;
}else if(Snake2Direction == "up" && vy != 1) {
vx = 0;
vy = -1;
}else if(Snake2Direction == "down" && vy != -1) {
vx = 0;
vy = 1;
}
//collison with stage
if(head.x - head.width/2 <= 0){
reset();
}
if(head.x + head.width/2 >= stage.stageWidth){
reset();
}
if(head.y - head.height/2 <= 0){
reset();
}
if(head.y + head.height/2 >= stage.stageHeight){
reset();
}
//move body of the snake
for(var i = snake.length-1;i>0;--i){
snake[i].x = snake[i-1].x;
snake[i].y = snake[i-1].y;
}
for(var i = snake2.length-1;i>0;--i){
snake2[i].x = snake2[i-1].x;
snake2[i].y = snake2[i-1].y;
}
//changing the position of snake's head
head.x += vx*speed;
head.y += vy*speed;
//collision with tail
for(var i = snake.length-1;i>=1;--i){
if(snake[0].x == snake[i].x && snake[0].y == snake[i].y){
reset();
break;
}
}
for(var i = snake2.length-1;i>=1;--i){
if(snake2[0].x == snake2[i].x && snake2[0].y == snake2[i].y){
reset();
break;
}
}
//collision with food player 1
if(head.hitTestObject(gFood)){
removeChild(gFood);
addFood();
var bodyPart = new SnakePart();
bodyPart.x = snake[snake.length - 1].x;
bodyPart.y = snake[snake.length - 1].y;
snake.push(bodyPart);
addChild(bodyPart);
}
if(head.hitTestObject(gFood2)){
removeChild(gFood2);
addFood2();
var bodyPart = new SnakePart();
bodyPart.x = snake[snake.length - 1].x;
bodyPart.y = snake[snake.length - 1].y;
snake.push(bodyPart);
addChild(bodyPart);
}
//collision with food player 2
if(head.hitTestObject(gFood)){
removeChild(gFood);
addFood();
var bodyPart = new SnakePart2();
bodyPart.x = snake2[snake2.length - 1].x;
bodyPart.y = snake2[snake2.length - 1].y;
snake2.push(bodyPart);
addChild(bodyPart);
}
if(head.hitTestObject(gFood2)){
removeChild(gFood2);
addFood2();
var bodyPart = new SnakePart2();
bodyPart.x = snake2[snake2.length - 1].x;
bodyPart.y = snake2[snake2.length - 1].y;
snake2.push(bodyPart);
addChild(bodyPart);
}
}
}
}
首先,让我解释一下您在这段代码中的错误:
1.You 两条蛇的 vx
和 vy
相同!!这意味着两条蛇应该平行并朝相同的方向走!!
2.and 问题 1 不会发生,因为你为另一条蛇设置了 head
.x 和 y 而不是 head2
!
只是因为粗心...:)
所以:
1.add 变量 vx2
和 vy2
用于可怜的第二条蛇!
2.replace 第二个 setting direction of velocity
用这个:
if(Snake2Direction == "left" && vx2 != 1) {
vx2 = -1;
vy2 = 0;
}else if(Snake2Direction == "right" && vx2 != -1) {
vx2 = 1;
vy2 = 0;
}else if(Snake2Direction == "up" && vy2 != 1) {
vx2 = 0;
vy2 = -1;
}else if(Snake2Direction == "down" && vy2 != -1) {
vx2 = 0;
vy2 = 1;
}
3.do 你为第二条蛇留下的任何东西。(head2.x 和 y,第二条蛇的碰撞等)
我 H☺P E 这有帮助!