Recent posts by matthewlevine on Kongregate

Flag Post

Topic: Game Programming / Having a hard time with "play again" function

Hi, everyone. I’m relatively new to programming with AS3 (as you’ll be able to tell from my code), but I was looking for some advice in a project that I was working on.

I have no intent of distributing the game since some of it is someone else’s code that I was working with just for educational purposes. I’ve tried to add several functions on my own, but for some reason, “Play Again” is really giving me a hard time. Let me know what you guys think:

LeftPaddle.x = (20);
LeftPaddle.y = (200);
RightPaddle.x = (530);
RightPaddle.y = (200);
Ball.x = (518);
Ball.y = (200);
winnerText.x = (2000);
winnerText.y = (2000);
loserText.x = (2000);
loserText.y = (2000);
instruction1.x = (275);
instruction1.y = (100);
instruction2.x = (275);
instruction2.y = (166);
startButton.x = (275);
startButton.y = (367);
replayButton.x = (2000);
replayButton.y = (2000);

var ballSpeedX:int = -3;
var ballSpeedY:int = -2;

var playerScore:int = 0;
var cpuScore:int = 0;

var rightPaddleSpeed:int = 3;

startButton.addEventListener(MouseEvent.CLICK, gameStart);
replayButton.addEventListener(MouseEvent.CLICK, replayGame);

function replayGame(e:MouseEvent):void
{
	trace("1")
}

function winner():void
{
	winnerText.x = (275);
	winnerText.y = (125);
	ballSpeedX = 0;
	Ball.x = (518);
	Ball.y = (200);
	RightPaddle.y = (200);
	LeftPaddle.y = (200);
	replayButton.x = (275);
	replayButton.y = (367);
	replayGame(e:MouseEvent);
}

function loser():void
{
	loserText.x = (275);
	loserText.y = (125);
	ballSpeedX = 0;
	Ball.x = (518);
	Ball.y = (200);
	RightPaddle.y = (200);
	LeftPaddle.y = (200);
	replayButton.x = (275);
	replayButton.y = (367);
	replayGame(e:MouseEvent)
}

function gameStart(e:MouseEvent):void
{
	instruction1.x = (2000);
	instruction1.y = (2000);
	instruction2.x = (2000);
	instruction2.y = (2000);
	startButton.x = (2000);
	startButton.y = (2000);
	init();
}

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

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

function calculateBallAngle(PaddleY:Number, BallY:Number):Number
{
	var ySpeed:Number = 5 * ( (BallY - PaddleY) / 25);
	return ySpeed;
}


function loop(e:Event):void
{
	Ball.x = Ball.x + ballSpeedX
	Ball.y = Ball.y + ballSpeedY
	
	if (LeftPaddle.hitTestObject(Ball) == true)
	{
		if (ballSpeedX < 0)
		{
			ballSpeedX = ballSpeedX * -1;
			ballSpeedY = calculateBallAngle(LeftPaddle.y, Ball.y);
		}	
	}
	
	else if (RightPaddle.hitTestObject(Ball) == true)
	{
		if (ballSpeedX > 0)
		{
			ballSpeedX = ballSpeedX * -1
			ballSpeedY = calculateBallAngle(RightPaddle.y, Ball.y);
		}
	}
	
	LeftPaddle.y = mouseY;
	
	if (Ball.y - RightPaddle.y > 10)
	{
		RightPaddle.y = RightPaddle.y + rightPaddleSpeed
	}
	
	else if (RightPaddle.y - Ball.y > 10)
	{
		RightPaddle.y = RightPaddle.y - rightPaddleSpeed
	}
	
	if (LeftPaddle.y - LeftPaddle.height / 2 < 0)
	{
		LeftPaddle.y = LeftPaddle.height / 2;
	}
	
	else if (LeftPaddle.y + LeftPaddle.height / 2 > stage.stageHeight)
	{
		LeftPaddle.y = stage.stageHeight - LeftPaddle.height / 2;
	}
	
	if (Ball.x <= Ball.width / 2)
	{
		Ball.x = Ball.width / 2;
		ballSpeedX = ballSpeedX * -1;
		cpuScore = cpuScore + 1;
		updateTextFields();
	}
	else if (Ball.x >= stage.stageWidth - Ball.width / 2)
	{
		Ball.x = stage.stageWidth - Ball.width / 2;
		ballSpeedX = ballSpeedX * -1;
		playerScore = playerScore + 1;
		updateTextFields();
	}
	
	if (Ball.y <= Ball.height / 2)
	{
		Ball.y = Ball.height / 2
		ballSpeedY = ballSpeedY * - 1
	}
	else if (Ball.y >= stage.stageHeight - Ball.height / 2)
	{
		Ball.y = stage.stageHeight - Ball.height / 2
		ballSpeedY = ballSpeedY * -1
	}
	
	if (playerScore == 5)
	{
		winner();
	}
	else if (cpuScore == 5)
	{
		loser();
	}
}

I believe the best way to go about this given my minimal programming knowledge is to call the “replayGame” function every time you win or lose, giving the player a choice to click a button to play again.

At runtime, I’m getting an error saying:

Scene 1, Layer 'Layer 1', Frame 1, Line 47	1084: Syntax error: expecting rightparen before colon.

AND

Scene 1, Layer 'Layer 1', Frame 1, Line 61	1084: Syntax error: expecting rightparen before colon.

Thanks in advance for your help, everyone!