空格键播放/暂停游戏或动画闪光

时间:2014-11-16 07:17:29

标签: actionscript-3 flash animation

我是闪光灯的新手,我真的是首发,因为我们的教授没有教我们如何编写闪光灯

我做了一个Pong游戏,我从互联网上学到了,它已经从一开始就开始了 我想先停止它,然后当我按空格键时它会播放,如果我再次按空格键它会暂停。 如果有人帮助我,我将不胜感激:)

我正在使用AS3 btw

这是代码

var ballSpeedX:int = -3;
var ballSpeedY:int = -2;
var cpuPaddleSpeed:int = 3;
var playerScore:int = 0;
var cpuScore:int = 0;

init();

function init():void
{
	stage.addEventListener(Event.ENTER_FRAME, loop);
}

function calculateBallAngle(paddleY:Number, ballY:Number):Number
{
	var ySpeed:Number = 5 * ( (ballY-paddleY) / 25 );
	//trace(ySpeed);
	
	return ySpeed;
}

function updateTextFields():void
{
	playerScoreText.text = ("Player Score: " + playerScore);
	cpuScoreText.text = ("CPU Score: " + cpuScore);
}

function loop(e:Event):void
{
	if( playerPaddle.hitTestObject(ball) == true ){
		if(ballSpeedX < 0){
			ballSpeedX *= -1;
			ballSpeedY = calculateBallAngle(playerPaddle.y, ball.y);
		}
		
	} else if(cpuPaddle.hitTestObject(ball) == true ){
		if(ballSpeedX > 0){
			ballSpeedX *= -1;
			ballSpeedY = calculateBallAngle(cpuPaddle.y, ball.y);
		}
		
	}
	
	if(cpuPaddle.y < ball.y - 10){
		cpuPaddle.y += cpuPaddleSpeed;
		
	} else if(cpuPaddle.y > ball.y + 10){
		cpuPaddle.y -= cpuPaddleSpeed;
	}
		
	
	playerPaddle.y = mouseY;
	
	//check if top of paddle is above top of screen
	if(playerPaddle.y - playerPaddle.height/2 < 0){ 
		playerPaddle.y = playerPaddle.height/2;
	
	//check if bottom of paddle is below bottom of screen
	} else if(playerPaddle.y + playerPaddle.height/2 > stage.stageHeight){
		playerPaddle.y = stage.stageHeight - playerPaddle.height/2;
	}
	
	ball.x += ballSpeedX;
	ball.y += ballSpeedY;
	
	//because the ball's position is measured by where its CENTER is...
	//...we need add or subtract half of its width or height to see if that SIDE is hitting a wall
	
	//first check the left and right boundaries
	if(ball.x <= ball.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
		ball.x = ball.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
		cpuScore ++; //increase cpuScore by 1
		updateTextFields();
	} else if(ball.x >= stage.stageWidth-ball.width/2){ //check to see if the x position of it's right side is greater than or equal to the right side of the screen, which would be 550
		ball.x = stage.stageWidth-ball.width/2; //and set the x position to that, in case it already moved too far of the right side of the screen
		ballSpeedX *= -1; //multiply the x speed by -1 so that the ball is now moving left
		playerScore++; //increase playerScore by 1
		updateTextFields();	
	}
	
	//now we do the same with the top and bottom of the screen
	if(ball.y <= ball.height/2){ //if the y position of the top of the ball is less than or equal to the top of the screen
		ball.y = ball.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
		
	} else if(ball.y >= stage.stageHeight-ball.height/2){ //if the bottom of the ball is lower than the bottom of the screen
		ball.y = stage.stageHeight-ball.height/2; //reposition it
		ballSpeedY *= -1; //and reverse its y speec so that it is moving up now
		
	}
}

1 个答案:

答案 0 :(得分:0)

我不知道你的播放器输入代码是什么样的,但你也可以知道,当它暂停时你可以设置一个暂停标志,然后根据你的键或鼠标中的那个标志忽略任何输入输入事件处理程序。

对于实际的游戏逻辑,看起来你的ENTER_FRAME事件处理程序处理它的大部分,所以你可以在决定执行其余逻辑之前添加一个暂停滞后检查。

另一种方法是完全删除您的ENTER_FRAME事件处理程序以阻止游戏运行,然后在您想要恢复时将其重新添加。