Skip to main content
Leux
Participating Frequently
February 11, 2020
Answered

Unable to get multiplayer section of pong game working despite proper code -- or so I think

  • February 11, 2020
  • 1 reply
  • 451 views

Hey everyone,

 

there is a problem that I am encountering on a test pong game that I was creating.

 

My "singleplayer" section of my pong game works perfectly fine 

However my "multiplayer" section, when clicked on, despite setting code to it (with new variables), does not work, I get the following error:

 

TypeError: Error #2007: Parameter text must be non-null.
	at flash.text::TextField/set text()
	at _6_Test_Pong_fla::MainTimeline/frame3()
	at flash.display::MovieClip/gotoAndStop()
	at _6_Test_Pong_fla::MainTimeline/fl_ClickToGoToAndStopAtFrame_4()

 

My code:

For frame 1:

/* Stop at This Frame
The Flash timeline will stop/pause at the frame where you insert this code.
Can also be used to stop/pause the timeline of movieclips.
*/
stop();

/* Click to Go to Frame and Stop
Clicking on the specified symbol instance moves the playhead to the specified frame in the timeline and stops the movie.
Can be used on the main timeline or on movie clip timelines.

Instructions:
1. Replace the number 5 in the code below with the frame number you would like the playhead to move to when the symbol instance is clicked.
*/

button_1.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame_2);

function fl_ClickToGoToAndStopAtFrame_2(event:MouseEvent):void
{
	gotoAndStop(2);
}

/* Click to Go to Frame and Stop
Clicking on the specified symbol instance moves the playhead to the specified frame in the timeline and stops the movie.
Can be used on the main timeline or on movie clip timelines.

Instructions:
1. Replace the number 5 in the code below with the frame number you would like the playhead to move to when the symbol instance is clicked.
*/

function fl_ClickToGoToAndStopAtFrame_3(event:MouseEvent):void
{
	gotoAndStop(2);
}

/* Click to Go to Frame and Stop
Clicking on the specified symbol instance moves the playhead to the specified frame in the timeline and stops the movie.
Can be used on the main timeline or on movie clip timelines.

Instructions:
1. Replace the number 5 in the code below with the frame number you would like the playhead to move to when the symbol instance is clicked.
*/

button_3.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame_4);

function fl_ClickToGoToAndStopAtFrame_4(event:MouseEvent):void
{
	gotoAndStop(3);
}

For frame 2:

/*  DECLARE VARIABLES  */  
var xMove = 5;    // variable for ball movement on x-axis 
var yMove = 5;    // variable for ball movement on y-axis
/* Enter Frame Event
Executes the function fl_EnterFrameHandler defined below each time the playhead moves into a new frame of the timeline.

Instructions:
1. Add your custom code on a new line after the line that says "// Start your custom code" below.
The code will execute when the playhead moves into a new timeline frame.
*/
var score = 0;     // variable for starting score
scoreBox.text = score;
addEventListener(Event.ENTER_FRAME, fl_EnterFrameHandler);

function fl_EnterFrameHandler(event:Event):void
{
	//Start your custom code
	// This example code displays the words "Entered frame" in the Output panel.
	ball.x += xMove;
ball.y += yMove;
// test wall hits 
	if  (ball.hitTestObject(wallRight)) {
		xMove *= -1; 
	} else if (ball.hitTestObject(wallBottom)){
		yMove *= -1; 
	} else if (ball.hitTestObject(wallLeft)){
		xMove *= -1; 
	}else if (ball.hitTestObject(wallTop)){
		yMove *= -1; 
	}  // end wall hits
// test paddle hits 
	if (ball.hitTestObject(paddle)) {
		yMove*= -1; 
		score +=1; 
	scoreBox.text = score;
	}  // end paddle hits 

	// End your custom code
}

/* Move with Keyboard Arrows
Allows the specified symbol instance to be moved with the keyboard arrows.

Instructions:
1. To increase or decrease the amount of movement, replace the number 5 below with the number of pixels you want the symbol instance to move with each key press.
Note the number 5 appears four times in the code below.
*/

var upPressed:Boolean = false;
var downPressed:Boolean = false;
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;

paddle.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed);

function fl_MoveInDirectionOfKey(event:Event)
{
	if (upPressed)
	{
		paddle.y -= 5;
	}
	if (downPressed)
	{
		paddle.y += 5;
	}
	if (leftPressed)
	{
		paddle.x -= 5;
	}
	if (rightPressed)
	{
		paddle.x += 5;
	}
}

function fl_SetKeyPressed(event:KeyboardEvent):void
{
	switch (event.keyCode)
	{
		case Keyboard.UP:
		{
			upPressed = true;
			break;
		}
		case Keyboard.DOWN:
		{
			downPressed = true;
			break;
		}
		case Keyboard.LEFT:
		{
			leftPressed = true;
			break;
		}
		case Keyboard.RIGHT:
		{
			rightPressed = true;
			break;
		}
	}
}

function fl_UnsetKeyPressed(event:KeyboardEvent):void
{
	switch (event.keyCode)
	{
		case Keyboard.UP:
		{
			upPressed = false;
			break;
		}
		case Keyboard.DOWN:
		{
			downPressed = false;
			break;
		}
		case Keyboard.LEFT:
		{
			leftPressed = false;
			break;
		}
		case Keyboard.RIGHT:
		{
			rightPressed = false;
			break;
		}
	}
}

For frame 3:

/*  DECLARE VARIABLES  */  
var xMove1 = 5;    // variable for ball movement on x-axis 
var yMove1 = 5;    // variable for ball movement on y-axis
/* Enter Frame Event
Executes the function fl_EnterFrameHandler defined below each time the playhead moves into a new frame of the timeline.

Instructions:
1. Add your custom code on a new line after the line that says "// Start your custom code" below.
The code will execute when the playhead moves into a new timeline frame.
*/
var score1 = 0;     // variable for starting score
scoreBox2.text = score;
addEventListener(Event.ENTER_FRAME, fl_EnterFrameHandler);


{
	//Start your custom code
	// This example code displays the words "Entered frame" in the Output panel.
	ball2.x += xMove1;
ball2.y += yMove1;
// test wall hits 
	if  (ball2.hitTestObject(wallRight2)) {
		xMove1 *= -1; 
	} else if (ball2.hitTestObject(wallBottom2)){
		yMove1 *= -1; 
	} else if (ball2.hitTestObject(wallLeft2)){
		xMove1 *= -1; 
	}else if (ball2.hitTestObject(wallTop2)){
		yMove1 *= -1; 
	}  // end wall hits
// test paddle hits 
	if (ball2.hitTestObject(paddle2)) {
		yMove1*= -1; 
		score +=1; 
	scoreBox2.text = score;
	}  // end paddle hits 

	// End your custom code
}

/* Move with Keyboard Arrows
Allows the specified symbol instance to be moved with the keyboard arrows.

Instructions:
1. To increase or decrease the amount of movement, replace the number 5 below with the number of pixels you want the symbol instance to move with each key press.
Note the number 5 appears four times in the code below.
*/

var upPressed1:Boolean = false;
var downPressed1:Boolean = false;
var leftPressed1:Boolean = false;
var rightPressed1:Boolean = false;

paddle2.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed);

function fl_MoveInDirectionOfKey1(event:Event)
{
	if (upPressed)
	{
		paddle2.y -= 5;
	}
	if (downPressed)
	{
		paddle2.y += 5;
	}
	if (leftPressed)
	{
		paddle2.x -= 5;
	}
	if (rightPressed)
	{
		paddle2.x += 5;
	}
}

function fl_SetKeyPressed1(event:KeyboardEvent):void
{
	switch (event.keyCode)
	{
		case Keyboard.UP:
		{
			upPressed = true;
			break;
		}
		case Keyboard.DOWN:
		{
			downPressed = true;
			break;
		}
		case Keyboard.LEFT:
		{
			leftPressed = true;
			break;
		}
		case Keyboard.RIGHT:
		{
			rightPressed = true;
			break;
		}
	}
}

function fl_UnsetKeyPressed1(event:KeyboardEvent):void
{
	switch (event.keyCode)
	{
		case Keyboard.UP:
		{
			upPressed = false;
			break;
		}
		case Keyboard.DOWN:
		{
			downPressed = false;
			break;
		}
		case Keyboard.LEFT:
		{
			leftPressed = false;
			break;
		}
		case Keyboard.RIGHT:
		{
			rightPressed = false;
			break;
		}
	}
}

If needed, here is the link to my animate file:Uploadfiles.io link to my file 

Anyone have any idea how I fix this? Any help would be appreciated.

This topic has been closed for replies.
Correct answer JoãoCésar17023019

Hi.

 

It's because in frame 3, you're trying to assign the score variable to the text property of the scoreBox2 TextField instance. The problem is that when the player clicks to play the multiplayer mode, the timeline goes directly to frame 3 and the score variable is never declared. To fix this issue, you can declare the score variable in frame 1 or create two separate score variables for each mode. Also, don't forget to give types to your variables (e.g.: var score:uint = 0) and also to cast the variable value to a String when assigning it to a text property (scoreBox2.text = String(score););

 

Another problem is this line in frame 3:

addEventListener(Event.ENTER_FRAME, fl_EnterFrameHandler);

 

It will call the fl_EnterFrameHandler function on frame 2 but you'll get invalid reference errors because the instances in frame 2 won't be available in frame 3.

 

Please let us know if you have any further questions.

 

 

Regards,

JC 

1 reply

JoãoCésar17023019
Community Expert
JoãoCésar17023019Community ExpertCorrect answer
Community Expert
February 12, 2020

Hi.

 

It's because in frame 3, you're trying to assign the score variable to the text property of the scoreBox2 TextField instance. The problem is that when the player clicks to play the multiplayer mode, the timeline goes directly to frame 3 and the score variable is never declared. To fix this issue, you can declare the score variable in frame 1 or create two separate score variables for each mode. Also, don't forget to give types to your variables (e.g.: var score:uint = 0) and also to cast the variable value to a String when assigning it to a text property (scoreBox2.text = String(score););

 

Another problem is this line in frame 3:

addEventListener(Event.ENTER_FRAME, fl_EnterFrameHandler);

 

It will call the fl_EnterFrameHandler function on frame 2 but you'll get invalid reference errors because the instances in frame 2 won't be available in frame 3.

 

Please let us know if you have any further questions.

 

 

Regards,

JC 

Leux
LeuxAuthor
Participating Frequently
February 14, 2020

Thank you JoãoCésar for the help,

I now receive no errors, but my paddle bugs out whenever it hits my "ball"

Here is my new adjusted lines of code, thanks to your help.

Frame 2:

 

 

	/*  DECLARE VARIABLES  */  
	var xMove = 5;    // variable for ball movement on x-axis 
	var yMove = 5;    // variable for ball movement on y-axis
	/* Enter Frame Event
	Executes the function fl_EnterFrameHandler defined below each time the playhead moves into a new frame of the timeline.

	Instructions:
	1. Add your custom code on a new line after the line that says "// Start your custom code" below.
	The code will execute when the playhead moves into a new timeline frame.
	*/
	var score:uint = 0     // variable for starting score
	scoreBox.text = String(score);
	addEventListener(Event.ENTER_FRAME, fl_EnterFrameHandler);

	function fl_EnterFrameHandler(event:Event):void
	{
		//Start your custom code
		// This example code displays the words "Entered frame" in the Output panel.
		ball.x += xMove;
	ball.y += yMove;
	// test wall hits 
		if  (ball.hitTestObject(wallRight)) {
			xMove *= -1; 
		} else if (ball.hitTestObject(wallBottom)){
			yMove *= -1; 
		} else if (ball.hitTestObject(wallLeft)){
			xMove *= -1; 
		}else if (ball.hitTestObject(wallTop)){
			yMove *= -1; 
		}  // end wall hits
	// test paddle hits 
		if (ball.hitTestObject(paddle)) {
			yMove*= -1; 
			score +=1; 
		scoreBox.text = String(score);
		}  // end paddle hits 

		// End your custom code
	}

	/* Move with Keyboard Arrows
	Allows the specified symbol instance to be moved with the keyboard arrows.

	Instructions:
	1. To increase or decrease the amount of movement, replace the number 5 below with the number of pixels you want the symbol instance to move with each key press.
	Note the number 5 appears four times in the code below.
	*/

	var upPressed:Boolean = false;
	var downPressed:Boolean = false;
	var leftPressed:Boolean = false;
	var rightPressed:Boolean = false;

	paddle.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey);
	stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed);
	stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed);

	function fl_MoveInDirectionOfKey(event:Event)
	{
		if (upPressed)
		{
			paddle.y -= 5;
		}
		if (downPressed)
		{
			paddle.y += 5;
		}
		if (leftPressed)
		{
			paddle.x -= 5;
		}
		if (rightPressed)
		{
			paddle.x += 5;
		}
	}

	function fl_SetKeyPressed(event:KeyboardEvent):void
	{
		switch (event.keyCode)
		{
			case Keyboard.UP:
			{
				upPressed = true;
				break;
			}
			case Keyboard.DOWN:
			{
				downPressed = true;
				break;
			}
			case Keyboard.LEFT:
			{
				leftPressed = true;
				break;
			}
			case Keyboard.RIGHT:
			{
				rightPressed = true;
				break;
			}
		}
	}

	function fl_UnsetKeyPressed(event:KeyboardEvent):void
	{
		switch (event.keyCode)
		{
			case Keyboard.UP:
			{
				upPressed = false;
				break;
			}
			case Keyboard.DOWN:
			{
				downPressed = false;
				break;
			}
			case Keyboard.LEFT:
			{
				leftPressed = false;
				break;
			}
			case Keyboard.RIGHT:
			{
				rightPressed = false;
				break;
			}
		}
	}

 

 

Frame 3:

 

 

/*  DECLARE VARIABLES  */  
var xMove1 = 5;    // variable for ball movement on x-axis 
var yMove1 = 5;    // variable for ball movement on y-axis
var yMove2 = 5;    // variable for ball movement on y-axis
/* Enter Frame Event
Executes the function fl_EnterFrameHandler defined below each time the playhead moves into a new frame of the timeline.

Instructions:
1. Add your custom code on a new line after the line that says "// Start your custom code" below.
The code will execute when the playhead moves into a new timeline frame.
*/
var score1:uint = 0     // variable for first player starting score
var score2:uint = 0     // variable for second player starting score
scoreBox2.text = String(score1);
scoreBox3.text = String(score2);
addEventListener(Event.ENTER_FRAME, fl_EnterFrameHandler_2);
function fl_EnterFrameHandler_2(event:Event):void

{
	//Start your custom code
	// This example code displays the words "Entered frame" in the Output panel.
	ball2.x += xMove1;
ball2.y += yMove1;
// test wall hits 
	if  (ball2.hitTestObject(wallRight2)) {
		xMove1 *= -1; 
	} else if (ball2.hitTestObject(wallBottom2)){
		yMove1 *= -1; 
	} else if (ball2.hitTestObject(wallLeft2)){
		xMove1 *= -1; 
	}else if (ball2.hitTestObject(wallTop2)){
		yMove1 *= -1; 
	}  // end wall hits
// test paddle hits 
	if (ball2.hitTestObject(paddle2)) {
		yMove1*= -1; 
		score1 +=1; 
	scoreBox2.text = String(score1);
	}  // end paddle hits 
	if (ball2.hitTestObject(paddle3)) {
		yMove1*= -1; 
		score2 +=1; 
	scoreBox3.text = String(score2);
	}  // end paddle hits 
	// End your custom code
}

/* Move with Keyboard Arrows
Allows the specified symbol instance to be moved with the keyboard arrows.

Instructions:
1. To increase or decrease the amount of movement, replace the number 5 below with the number of pixels you want the symbol instance to move with each key press.
Note the number 5 appears four times in the code below.
*/

var upPressed1:Boolean = false;
var downPressed1:Boolean = false;
var leftPressed1:Boolean = false;
var rightPressed1:Boolean = false;
var upPressed2:Boolean = false;
var downPressed2:Boolean = false;
var leftPressed2:Boolean = false;
var rightPressed2:Boolean = false;

paddle2.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey2);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed2);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed2);

function fl_MoveInDirectionOfKey2(event:Event)
{
	if (upPressed2)
	{
		paddle2.y -= 5;
	}
	if (downPressed2)
	{
		paddle2.y += 5;
	}
	if (leftPressed2)
	{
		paddle2.x -= 5;
	}
	if (rightPressed2)
	{
		paddle2.x += 5;
	}
}

function fl_SetKeyPressed2(event:KeyboardEvent):void
{
	switch (event.keyCode)
	{
		case Keyboard.W:
		{
			upPressed2 = true;
			break;
		}
		case Keyboard.S:
		{
			downPressed2 = true;
			break;
		}
		case Keyboard.A:
		{
			leftPressed2 = true;
			break;
		}
		case Keyboard.D:
		{
			rightPressed2 = true;
			break;
		}
	}
}

function fl_UnsetKeyPressed2(event:KeyboardEvent):void
{
	switch (event.keyCode)
	{
		case Keyboard.W:
		{
			upPressed2 = false;
			break;
		}
		case Keyboard.S:
		{
			downPressed2 = false;
			break;
		}
		case Keyboard.A:
		{
			leftPressed2 = false;
			break;
		}
		case Keyboard.D:
		{
			rightPressed2 = false;
			break;
		}
	}
}


paddle3.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey1);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed1);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed1);

function fl_MoveInDirectionOfKey1(event:Event)
{
	if (upPressed1)
	{
		paddle3.y -= 5;
	}
	if (downPressed1)
	{
		paddle3.y += 5;
	}
	if (leftPressed1)
	{
		paddle3.x -= 5;
	}
	if (rightPressed1)
	{
		paddle3.x += 5;
	}
}

function fl_SetKeyPressed1(event:KeyboardEvent):void
{
	switch (event.keyCode)
	{
		case Keyboard.UP:
		{
			upPressed1 = true;
			break;
		}
		case Keyboard.DOWN:
		{
			downPressed1 = true;
			break;
		}
		case Keyboard.LEFT:
		{
			leftPressed1 = true;
			break;
		}
		case Keyboard.RIGHT:
		{
			rightPressed1 = true;
			break;
		}
	}
}

function fl_UnsetKeyPressed1(event:KeyboardEvent):void
{
	switch (event.keyCode)
	{
		case Keyboard.UP:
		{
			upPressed1 = false;
			break;
		}
		case Keyboard.DOWN:
		{
			downPressed1 = false;
			break;
		}
		case Keyboard.LEFT:
		{
			leftPressed1 = false;
			break;
		}
		case Keyboard.RIGHT:
		{
			rightPressed1 = false;
			break;
		}
	}
}

 

 

 After I click "Singleplayer" in frame 1, and it takes me to my frame 2, where my single player content is, it works, but if I get too close with my paddle to the ball, then the ball starts bugging out as shown here:

Frame 2 ball bug problem 

When I click "Multiplayer" in frame 1, after it takes me to my frame 3, I programmed both paddles to work, but they dont react to the ball the way that a "pong" game is supposed to work at all, it bugs out much worse.

Frame 3 -- worse ball bug problem 

 

Do you have any idea how I could fix this JoãoCésar?

 

Thanks