为什么我无法重置此游戏?我尝试重置变量等

时间:2015-09-08 05:29:49

标签: actionscript-3 flash

我有这个用AS3编码的flash游戏。它适用于第一轮。当你死了,程序会将它循环回到一对或错误发生的游戏框架。一。上轮死人仍然保持不变。二。速度每次都加倍。为什么????

我创建了一个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;
        }
    }
}

请帮忙!

0 个答案:

没有答案