目前我的游戏似乎运行良好,唯一的问题是构建器不会响应10次中的8次。问题是为什么它会停止工作,我该如何解决呢。
这是我的代码。
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
/**
* ...
* @author BLANK
*/
public class Main extends Sprite
{
public var start:StartScherm = new StartScherm;
public var button:Startbutton = new Startbutton;
public var balletje:Ball = new Ball;
public var player1:Player = new Player;
public var player2:Player2 = new Player2;
public var player3:Player3 = new Player3;
public var player4:Player4 = new Player4;
public var background:Background = new Background;
public var kader:Kader = new Kader;
public var scoreboard:Monkeys = new Monkeys;
public var leven1:Life = new Life;
public var leven2:Life2 = new Life2;
public var leven3:Life3 = new Life3;
public var leven4:Life4 = new Life4;
public var wall1:Walls = new Walls;
public var wall2:Wall2 = new Wall2;
public var wall3:Wall3 = new Wall3;
public var wall4:Wall4 = new Wall4;
public static var KEY_LEFT1:Boolean = false;
public static var KEY_LEFT2:Boolean = false;
public static var KEY_LEFT3:Boolean = false;
public static var KEY_LEFT4:Boolean = false;
public static var KEY_RIGHT1:Boolean = false;
public static var KEY_RIGHT2:Boolean = false;
public static var KEY_RIGHT3:Boolean = false;
public static var KEY_RIGHT4:Boolean = false;
public var GAME:Boolean = false;
public var RESET:Boolean = false;
public var ballSpeedX:int = -3;
public var ballSpeedY:int = -2;
public var LIFE1:Number = 10;
public var LIFE2:Number = 10;
public var LIFE3:Number = 10;
public var LIFE4:Number = 10;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
stage.addEventListener(Event.ENTER_FRAME, loop);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
addChild(start);
addChild(button);
}
private function gameStart():void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
if (stage) world();
else addEventListener(Event.ADDED_TO_STAGE, world);
}
private function gameReset():void
{
removeEventListener(Event.ADDED_TO_STAGE, world);
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function world(e:Event = null):void
{
addChild(background);
addChild(kader);
addChild(player1);
addChild(player2);
addChild(player3);
addChild(player4);
addChild(scoreboard);
addChild(leven1);
addChild(leven2);
addChild(leven3);
addChild(leven4);
addChild(balletje);
}
private function loop(e:Event):void
{
//balletje.update();
if (GAME == true)
{
removeEventListener(Event.ADDED_TO_STAGE, init);
removeChild(start);
removeChild(button);
gameStart();
}
if (RESET == true)
{
removeEventListener(Event.ADDED_TO_STAGE, world);
removeChild(background);
removeChild(kader);
removeChild(player1);
removeChild(player2);
removeChild(player3);
removeChild(player4);
removeChild(scoreboard);
removeChild(leven1);
removeChild(leven2);
removeChild(leven3);
removeChild(leven4);
removeChild(balletje);
removeChild(wall1);
removeChild(wall2);
removeChild(wall3);
removeChild(wall4);
LIFE1 = 3;
LIFE2 = 3;
LIFE3 = 3;
LIFE4 = 3;
gameReset();
}
if (LIFE1 == 0)
{
removeChild(leven1);
addChild(wall1);
}
if (LIFE1 == 1)
{
leven1.width = 10;
leven1.height = 10;
}
if (LIFE1 == 2)
{
leven1.width = 30;
leven1.height = 30;
}
if (LIFE2 == 0)
{
removeChild(leven2);
addChild(wall2);
}
if (LIFE2 == 1)
{
leven2.width = 10;
leven2.height = 10;
}
if (LIFE2 == 2)
{
leven2.width = 30;
leven2.height = 30;
}
if (LIFE3 == 0)
{
removeChild(leven3);
addChild(wall3);
}
if (LIFE3 == 1)
{
leven3.width = 10;
leven3.height = 10;
}
if (LIFE3 == 2)
{
leven3.width = 30;
leven3.height = 30;
}
if (LIFE4 == 0)
{
removeChild(leven4);
addChild(wall4);
}
if (LIFE4 == 1)
{
leven4.width = 10;
leven4.height = 10;
}
if (LIFE4 == 2)
{
leven4.width = 30;
leven4.height = 30;
}
balletje.x += ballSpeedX;
balletje.y += ballSpeedY;
if(balletje.x <= -350-balletje.width/2){ //check if the x position of the left side of the ball is less than or equal to the left side of the screen, which would be 0
balletje.x = -350-balletje.width/2; //then set the ball's x position to that point, in case it already moved off the screen
ballSpeedX *= -1; //and multiply the ball's x speed by -1, which will make it move right instead of left
LIFE2 -= 1;
} else if(balletje.x >= 350-balletje.width/2){ //check to see the right side of the ball is touching the right boundary, which would be 550
balletje.x = 350-balletje.width/2; //reposition it, just in case
ballSpeedX *= -1; //multiply the x speed by -1 (now moving left, not right)
LIFE4 -= 1;
}
//now we do the same with the top and bottom of the screen
if(balletje.y <= -350-balletje.height/2){ //if the y position of the top of the ball is less than or equal to the top of the screen
balletje.y = -350-balletje.height/2; //like we did before, set it to that y position...
ballSpeedY *= -1; //...and reverse its y speed so that it is now going down instead of up
LIFE3 -= 1;
} else if(balletje.y >= 350-balletje.height/2){ //if the bottom of the ball is lower than the bottom of the screen
balletje.y = 350-balletje.height/2; //reposition it
ballSpeedY *= -1; //and reverse its y speed so that it is moving up now
LIFE1 -= 1;
}
if( player1.hitTestObject(balletje) == true ){
if(ballSpeedX < 0){
ballSpeedY *= -1;
ballSpeedX = calculateBallAngle2(player1.x, balletje.x);
}
if(ballSpeedX > 0){
ballSpeedY *= 1;
ballSpeedX = calculateBallAngle2(player1.x, balletje.x);
}
} else if(player2.hitTestObject(balletje) == true ){
if(ballSpeedX > 0){
ballSpeedX *= -1;
ballSpeedY = calculateBallAngle(player2.y, balletje.y);
}
if(ballSpeedX < 0){
ballSpeedX *= 1;
ballSpeedY = calculateBallAngle(player2.y, balletje.y);
}
} else if(player3.hitTestObject(balletje) == true ){
if(ballSpeedX > 0){
ballSpeedY *= -1;
ballSpeedX = calculateBallAngle2(player3.x, balletje.x);
}
if(ballSpeedX < 0){
ballSpeedY *= 1;
ballSpeedX = calculateBallAngle2(player3.x, balletje.x);
}
}
else if(player4.hitTestObject(balletje) == true ){
if(ballSpeedX > 0){
ballSpeedX *= -1;
ballSpeedY = calculateBallAngle(player4.y, balletje.y);
}
if(ballSpeedX < 0){
ballSpeedX *= 1;
ballSpeedY = calculateBallAngle(player4.y, balletje.y);
}
}
if (KEY_LEFT1 == true)
{
player1.x -= 10;
}
if (KEY_LEFT2 == true)
{
player1.x += 10;
}
if (KEY_LEFT3 == true)
{
player3.x -= 10;
}
if (KEY_LEFT4 == true)
{
player3.x += 10;
}
if (KEY_RIGHT1 == true)
{
player2.y += 10;
}
if (KEY_RIGHT2 == true)
{
player2.y -= 10;
}
if (KEY_RIGHT3 == true)
{
player4.y += 10;
}
if (KEY_RIGHT4 == true)
{
player4.y -= 10;
}
}
private function keyDown(e:KeyboardEvent):void
{
if (e.keyCode == 37)
{
KEY_LEFT1 = true;
}
if (e.keyCode == 39)
{
KEY_LEFT2 = true;
}
if (e.keyCode == 67)
{
KEY_LEFT3 = true;
}
if (e.keyCode == 86)
{
KEY_LEFT4 = true;
}
if (e.keyCode == 65)
{
KEY_RIGHT1 = true;
}
if (e.keyCode == 81)
{
KEY_RIGHT2 = true;
}
if (e.keyCode == 102)
{
KEY_RIGHT3 = true;
}
if (e.keyCode == 105)
{
KEY_RIGHT4 = true;
}
if (e.keyCode == 13)
{
GAME = true;
}
if (e.keyCode == 8)
{
RESET = true;
}
}
private function keyUp(e:KeyboardEvent):void
{
if (e.keyCode == 37)
{
KEY_LEFT1 = false;
}
if (e.keyCode == 39)
{
KEY_LEFT2 = false;
}
if (e.keyCode == 67)
{
KEY_LEFT3 = false;
}
if (e.keyCode == 86)
{
KEY_LEFT4 = false;
}
if (e.keyCode == 65)
{
KEY_RIGHT1 = false;
}
if (e.keyCode == 81)
{
KEY_RIGHT2 = false;
}
if (e.keyCode == 102)
{
KEY_RIGHT3 = false;
}
if (e.keyCode == 105)
{
KEY_RIGHT4 = false;
}
if (e.keyCode == 13)
{
GAME = false;
}
if (e.keyCode == 8)
{
RESET = false;
}
}
public function calculateBallAngle(paddleY:Number, ballY:Number):Number
{
var ySpeed:Number = 1 * ( (ballY-paddleY) / 40 );
return ySpeed;
}
public function calculateBallAngle2(paddleX:Number, ballX:Number):Number
{
var xSpeed:Number = 1 * ( (ballX-paddleX) / 40 );
return xSpeed;
}
}
}
提前致谢。