Actionscript 3 为什么可以';我不能重新设置这个游戏吗?我试着重置变量等
我有一个用AS3编码的flash游戏。第一轮有效。当你死后,程序会将其循环回游戏帧,在该帧中会出现一对或多个错误。一个。上一轮中死去的角色仍然是静止的。两个。每次速度都加倍。为什么 我创建了一个Actionscript 3 为什么可以';我不能重新设置这个游戏吗?我试着重置变量等,actionscript-3,flash,Actionscript 3,Flash,我有一个用AS3编码的flash游戏。第一轮有效。当你死后,程序会将其循环回游戏帧,在该帧中会出现一对或多个错误。一个。上一轮中死去的角色仍然是静止的。两个。每次速度都加倍。为什么 我创建了一个resetEverything()函数来重置所有变量、删除事件侦听器和一个clear.graphics() 为什么什么都不起作用 这是我的密码 stop(); var leftBorder:verticalwall = new verticalwall(); // defining a variable
resetEverything()
函数来重置所有变量、删除事件侦听器和一个clear.graphics()
为什么什么都不起作用
这是我的密码
stop();
var leftBorder:verticalwall = new verticalwall(); // defining a variable to hold the left wall
addChild(leftBorder); // adding the left wall to the stage
var rightBorder:verticalwall = new verticalwall(); // defining a variable to hold the left wall
rightBorder.x = 790; // pushing the right wall to the edge of the stage
addChild(rightBorder); // adding the right wall to the stage
var topBorder:horizontalwall = new horizontalwall(); // defining a variable to hold the left wall
addChild(topBorder); // adding the top wall to the stage
var bottomBorder:horizontalwall = new horizontalwall(); // defining a variable to hold the bottom wall
bottomBorder.y = 790; // pushing the bottom wall to the base of the stage
addChild(bottomBorder); // adding the bottom wall to the stage
var P1positions:Array = new Array(); //defining a new variable to hold the poistions of Player 1
var P2positions:Array = new Array(); //defining a new variable to hold the poistions of Player 2
graphics.beginFill( 0x000000 ); // defining a colour for the background
graphics.drawRect( 0, 0, 800, 800); // drawing a rectangle for background
graphics.endFill(); // ending the background creating process
stage.addEventListener(Event.ENTER_FRAME, checkPlayersSpeed);
function checkPlayersSpeed(event:Event)
{
if(P1speed == 0 || P2speed == 0){
P1speed = 0;
P2speed = 0;
gotoAndStop(1, "Conclusion");
}
}
//Player 1
var player1col = "0xFF0000";
var Player1:Shape = new Shape(); //defining a variable for Player 1
Player1.graphics.lineStyle(10,0xffff00); //defining the colour of the style
Player1.graphics.beginFill(0xffff00); //begin filling the shape
Player1.graphics.drawRoundRect(0,0,3,3,360);
Player1.graphics.drawCircle(Player1.x, Player1.x, 2.4) //draw a circle
Player1.graphics.endFill(); //finish the filling process
addChild(Player1); //add player 1 to stage
Player1.x = 250;
Player1.y = 250;
var P1leftPressed:Boolean = false; //boolean to check whether the left key for Player 1 was pressed
var P1rightPressed:Boolean = false; //boolean to check whether the right key for Player 1 was pressed
var P1speed = 3.5; //variable to store the speed of which player 1 moves
var P1Dir = 45; //variable containing the direction in which player 1 moves
var P1position, P2position;
Player1.addEventListener(Event.ENTER_FRAME, P1fl_MoveInP1DirectionOfKey); //adding a listener to the player
stage.addEventListener(KeyboardEvent.KEY_DOWN, P1fl_SetKeyPressed); //listener for a key to be pressed
stage.addEventListener(KeyboardEvent.KEY_UP, P1fl_UnsetKeyPressed); // listener for a key to be released
function P1fl_MoveInP1DirectionOfKey(event:Event) //Moves the player depedning on what key was pressed
{
if(Player1.hitTestObject(leftBorder) || Player1.hitTestObject(rightBorder) || Player1.hitTestObject(topBorder) || Player1.hitTestObject(bottomBorder)){ //checking to see whether Player 1 has hit the wall
P1speed = 0; //stopping Player 1 from moving
Player1.removeEventListener(Event.ENTER_FRAME, P1fl_MoveInP1DirectionOfKey); //adding a listener to the player
stage.removeEventListener(KeyboardEvent.KEY_DOWN, P1fl_SetKeyPressed); //listener for a key to be pressed
stage.removeEventListener(KeyboardEvent.KEY_UP, P1fl_UnsetKeyPressed); // listener for a key to be released
}
if (P1leftPressed)
{
P1Dir -= 0.1; //changes the direction to make Player 1 rotate
}
if (P1rightPressed)
{
P1Dir += 0.1; //changes the direction to make Player 1 rotate
}
P1position = [Player1.x, Player1.y]; //defining a variable for Player 1's constant positions
P1positions.push(P1position); //pushes every position of Player 1 to the array
for (var i = 0; i < P1positions.length - 10; i++) { //a loop that opperates for as long as the array is receiving positions
var P1x = P1positions[i][0]; //saving x positions into array with a unique identifier
var P1y = P1positions[i][1]; //saving y positions into array with a unique identifier
if (distanceBetween(P1x, P1y, Player1.x, Player1.y) < 15) { //checking distance between Player 1 and its trail
P1speed = 0;
}
}
Player1.x += P1speed * Math.cos(P1Dir); //this makes player 1 move forard
Player1.y += P1speed * Math.sin(P1Dir); //this makes player 2 move forward
var P1trail:Shape = new Shape; //defining a variable for player 1's trail
graphics.lineStyle(8, player1col); //setting the format for the trail
graphics.drawCircle(Player1.x, Player1.y, 1.4); //drawing the circles within the trail
addChild(P1trail); //adding the circles to the stage
}
function P1fl_SetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.LEFT:
{
P1leftPressed = true; //tells the computer that left has been pressed
break;
}
case Keyboard.RIGHT:
{
P1rightPressed = true; //tells the computer that right has been pressed
break;
}
}
}
function P1fl_UnsetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.LEFT:
{
P1leftPressed = false; //tells the computer that left has been released
break;
}
case Keyboard.RIGHT:
{
P1rightPressed = false; //tells the computer that left has been released
break;
}
}
}
function distanceBetween (x1:Number, y1:Number, x2:Number, y2:Number) { // creating a function
// return d = Math.sqrt(x2 - x1)^2 +(y2 - y1)^2);
var diffX = x2 - x1; // creating variable to tidy up the pythagoras line below
var diffY = y2 - y1; // creating variable to tidy up the pythagoras line below
return Math.sqrt(diffX * diffX + diffY * diffY); // using pythagras theorem
}
// Player 2
var player2col = "0x0066CC";
var Player2:Shape = new Shape(); //defining a variable for Player 1
Player2.graphics.lineStyle(10,0xffff00); //defining the colour of the style
Player2.graphics.beginFill(0xffff00); //begin filling the shape
Player2.graphics.drawRoundRect(0,0,3,3,360);
Player2.graphics.drawCircle(Player2.x, Player2.x, 2.4) //draw a circle
Player2.graphics.endFill(); //finish the filling process
addChild(Player2); //add player 1 to stage
Player2.x = 500;
Player2.y = 500;
var P2leftPressed:Boolean = false;
var P2rightPressed:Boolean = false;
var P2speed = 3.5;
var P2Dir = 180;
Player2.addEventListener(Event.ENTER_FRAME, P2fl_MoveInP1DirectionOfKey);
stage.addEventListener(KeyboardEvent.KEY_DOWN, P2fl_SetKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, P2fl_UnsetKeyPressed);
function P2fl_MoveInP1DirectionOfKey(event:Event)
{
if(Player2.hitTestObject(leftBorder) || Player2.hitTestObject(rightBorder) || Player2.hitTestObject(topBorder) || Player2.hitTestObject(bottomBorder)){
P2speed = 0;
Player2.removeEventListener(Event.ENTER_FRAME, P2fl_MoveInP1DirectionOfKey);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, P2fl_SetKeyPressed);
stage.removeEventListener(KeyboardEvent.KEY_UP, P2fl_UnsetKeyPressed);
}
if (P2leftPressed)
{
P2Dir -= 0.1;
}
if (P2rightPressed)
{
P2Dir += 0.1;
}
P2position = [Player2.x, Player2.y];
//trace(P2position);
P1positions.push(P2position);
for (var a = 0; a < P1positions.length - 10; a++) {
var P2x = P1positions[a][0];
var P2y = P1positions[a][1];
if (distanceBetween(P2x, P2y, Player2.x, Player2.y) < 15) {
P2speed = 0;
}
}
Player2.x += P2speed * Math.cos(P2Dir);
Player2.y += P2speed * Math.sin(P2Dir);
var P2trail:Shape = new Shape;
graphics.lineStyle(8, player2col);
graphics.drawCircle(Player2.x, Player2.y, 1.4);
addChild(P2trail);
}
function P2fl_SetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case event.keyCode = 90:
{
P2leftPressed = true;
break;
}
case event.keyCode = 67:
{
P2rightPressed = true;
break;
}
}
}
function P2fl_UnsetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case event.keyCode=90:
{
P2leftPressed = false;
break;
}
case event.keyCode=67:
{
P2rightPressed = false;
break;
}
}
}
stop();
var leftBorder:verticalwall=新的verticalwall();//定义用于固定左墙的变量
addChild(左边框);//将左侧墙添加到舞台
var rightBorder:verticalwall=新的verticalwall();//定义用于固定左墙的变量
rightBorder.x=790;//将右侧墙推到舞台边缘
addChild(右边框);//将右侧墙添加到舞台
变量topBorder:horizontalwall=新horizontalwall();//定义用于固定左墙的变量
addChild(上边框);//将顶墙添加到舞台
var bottomBorder:horizontalwall=新的horizontalwall();//定义用于固定底墙的变量
bottomBorder.y=790;//将底墙推到舞台底部
addChild(底部边框);//将底墙添加到舞台
变量p1位置:数组=新数组()//定义一个新变量来保存玩家1的位置
变量P2positions:数组=新数组()//定义一个新变量来保存玩家2的位置
graphics.beginll(0x000000);//为背景定义颜色
graphics.drawRect(0,0,800,800);//画一个矩形作为背景
graphics.endFill();//结束背景创建过程
stage.addEventListener(Event.ENTER_FRAME,CheckPlayerSpeed);
函数CheckPlayerSpeed(事件:事件)
{
如果(P1speed==0 | | P2speed==0){
p1速度=0;
p2速度=0;
gotoAndStop(1,“结论”);
}
}
//玩家1
var player1col=“0xFF0000”;
变量Player1:Shape=newshape()//为播放器1定义一个变量
Player1.图形.线条样式(10,0xffff00)//定义样式的颜色
Player1.graphics.beginll(0xffff00)//开始填充形状
Player1.graphics.drawRoundRect(0,0,3,3360);
Player1.graphics.drawCircle(Player1.x,Player1.x,2.4)//画一个圆
Player1.graphics.endFill()//完成填充过程
addChild(Player1)//将玩家1添加到舞台
Player1.x=250;
Player1.y=250;
变量P1leftPressed:Boolean=false//布尔值,用于检查是否按下了播放器1的左键
var P1rightPressed:Boolean=false//布尔值,用于检查是否按下了播放器1的右键
变量p1速度=3.5//变量来存储播放器1移动的速度
var P1Dir=45//包含播放器1移动方向的变量
变量p1位置,p2位置;
Player1.addEventListener(Event.ENTER_FRAME,P1fl_moveinp1键的方向)//向播放器添加侦听器
stage.addEventListener(KeyboardEvent.KEY_向下,P1fl_设置键按下)//要按下的键的侦听器
stage.addEventListener(KeyboardEvent.KEY_UP,P1fl_UnsetKeyPressed);//要释放的密钥的侦听器
函数P1fl\u MoveInP1DirectionOfKey(event:event)//根据按下的键移动播放器
{
如果(Player1.hitTestObject(左边框)| Player1.hitTestObject(右边框)| Player1.hitTestObject(上边框)| Player1.hitTestObject(下边框)){//检查玩家1是否撞到墙
P1speed=0;//阻止播放器1移动
Player1.removeEventListener(Event.ENTER_FRAME,P1fl_MoveInP1DirectionOfKey);//向播放器添加侦听器
stage.removeEventListener(KeyboardEvent.KEY_DOWN,P1fl_SetKeyPressed);//要按下的键的侦听器
stage.removeEventListener(KeyboardEvent.KEY_UP,P1fl_UnsetKeyPressed);//要释放的键的侦听器
}
如果(按p1左键)
{
P1Dir-=0.1;//更改方向以使播放器1旋转
}
如果(按P1RIGHT键)
{
P1Dir+=0.1;//更改方向以使播放器1旋转
}
P1position=[Player1.x,Player1.y];//为Player1的常量位置定义一个变量
P1positions.push(P1position);//将玩家1的每个位置推到数组中
对于(vari=0;i