Highlighted

Pong "paddle" bugs out when hits pong ball

Community Beginner ,
Feb 16, 2020

Copy link to clipboard

Copied

Hey everyone,

 

there's a strange problem that I encounter when I test a pong came I was creating, which I cant find the solution to.

 

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 

-- To fix this, I thought of making it so that the "paddle" can not entirely collide with my ball, but I dont know how to do that

 

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 

-- I have no idea how to fix this

 

Here is my code

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;
		}
	}
}

If needed, here is my animate file

Adobe Community Professional
Correct answer by ClayUUID | Adobe Community Professional

The "ball" is behaving that way because you've coded it to reverse direction when it's in contact with the "paddle". So it's doing exactly what you told it to. As long as it's inside the paddle, it's going to keep reversing.

 

To fix this, after each bounce you just need to ignore any further contact until the ball leaves the paddle. Something like this modification should work:

/*  DECLARE VARIABLES  */ 
var hitLast = false; // ball hit something last frame

// test paddle hits 
if (ball2.hitTestObject(paddle2)) {
	if (!hitLast) {
		yMove1 *= -1; 
		score1 +=1; 
		scoreBox2.text = String(score1);
		hitLast = true;
	}
}
else if (ball2.hitTestObject(paddle3)) {
	if (!hitLast) {
		yMove1 *= -1; 
		score2 +=1; 
		scoreBox3.text = String(score2);
		hitLast = true;
	}
}
else {
	hitLast = false;
}
TOPICS
ActionScript

Views

69

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Pong "paddle" bugs out when hits pong ball

Community Beginner ,
Feb 16, 2020

Copy link to clipboard

Copied

Hey everyone,

 

there's a strange problem that I encounter when I test a pong came I was creating, which I cant find the solution to.

 

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 

-- To fix this, I thought of making it so that the "paddle" can not entirely collide with my ball, but I dont know how to do that

 

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 

-- I have no idea how to fix this

 

Here is my code

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;
		}
	}
}

If needed, here is my animate file

Adobe Community Professional
Correct answer by ClayUUID | Adobe Community Professional

The "ball" is behaving that way because you've coded it to reverse direction when it's in contact with the "paddle". So it's doing exactly what you told it to. As long as it's inside the paddle, it's going to keep reversing.

 

To fix this, after each bounce you just need to ignore any further contact until the ball leaves the paddle. Something like this modification should work:

/*  DECLARE VARIABLES  */ 
var hitLast = false; // ball hit something last frame

// test paddle hits 
if (ball2.hitTestObject(paddle2)) {
	if (!hitLast) {
		yMove1 *= -1; 
		score1 +=1; 
		scoreBox2.text = String(score1);
		hitLast = true;
	}
}
else if (ball2.hitTestObject(paddle3)) {
	if (!hitLast) {
		yMove1 *= -1; 
		score2 +=1; 
		scoreBox3.text = String(score2);
		hitLast = true;
	}
}
else {
	hitLast = false;
}
TOPICS
ActionScript

Views

70

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 16, 2020 0
Adobe Community Professional ,
Feb 16, 2020

Copy link to clipboard

Copied

The "ball" is behaving that way because you've coded it to reverse direction when it's in contact with the "paddle". So it's doing exactly what you told it to. As long as it's inside the paddle, it's going to keep reversing.

 

To fix this, after each bounce you just need to ignore any further contact until the ball leaves the paddle. Something like this modification should work:

/*  DECLARE VARIABLES  */ 
var hitLast = false; // ball hit something last frame

// test paddle hits 
if (ball2.hitTestObject(paddle2)) {
	if (!hitLast) {
		yMove1 *= -1; 
		score1 +=1; 
		scoreBox2.text = String(score1);
		hitLast = true;
	}
}
else if (ball2.hitTestObject(paddle3)) {
	if (!hitLast) {
		yMove1 *= -1; 
		score2 +=1; 
		scoreBox3.text = String(score2);
		hitLast = true;
	}
}
else {
	hitLast = false;
}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 16, 2020 0