Go Back   CodingForums.com > :: Client side development > Flash & ActionScript

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-24-2009, 02:54 AM   PM User | #1
Cmptr_Prgrmr2B
New Coder

 
Join Date: Jan 2009
Posts: 15
Thanks: 1
Thanked 0 Times in 0 Posts
Cmptr_Prgrmr2B is an unknown quantity at this point
Angry My program exploded... I can has help?

I am trying to recreate a simple version of Pong. I am almost positive that I coded it correctly, though I apparently didn't because it's not working. I would be much appreciative if someone would take a look at my code and point me in the right direction. I'll put the code up, but since this is Flash, I'll also put the actual program file up as an attachment.

Thanks,
Cmptr_Prgrmr2B

Code:
player.onEnterFrame = function() {
	player._y = _root._ymouse;
}

opp.onEnterFrame = function() {
	if (ball._y < opp._y) {
		opp.dy = - opp.speed;
	} else {
		opp.dy = opp.speed;
	}
	
	opp._y += opp.dy;
}

init();

function init() {
	mouse.hide();
	
	ball.dx = 15;
	ball.dy = 5;
	
	opp.speed = 10;

	playerScore = 0;
	oppScore = 0;
}


ball.onEnterFrame = function() {
	ball.move();
	ball.checkBoundaries();
	ball.checkPaddles();
}

ball.move = function() {
	ball._x += ball.dx;
	ball._y += ball.dy;
}

ball.checkBoundaries = function() {
	if(ball._y < 0) {
		ball.dy = -ball.dy;
	}
	
	if(ball._y > Stage.height) {
		ball.dy = -ball.dy;
	}
	
	if(ball._x < 0) {
		oppScore++;
		
		ball._x = opp._x - 60;
		ball._y = opp_y;
		ball.dy = 0;
	}
	
	if(ball._x > Stage.width) {
		playerScore++
		
		ball._x = player._x + 50;
		ball._y = player._y;
		ball.dy = 0;
	}
}

ball.checkPaddles = function() {
	if (ball.hitTest(player)) {
	ball.dx = -ball.dx;
	ball.dy = getDy(player);
	}
	if (ball.hitTest(opp)) {
		ball.dx = -ball.dx;
		ball.dy = getDy(opp);
	}
}

function getDy(paddle) {
	relY = ball._y - paddle._y;
	relPerc - relY / paddle._height;
	trace ("relPerc");
	
	newDy = relPerc * 30;
	trace("NewDy");
	return newDy;
	
}
Attached Files
File Type: zip Pong.zip (5.1 KB, 68 views)
Cmptr_Prgrmr2B is offline   Reply With Quote
Old 03-24-2009, 09:59 AM   PM User | #2
gnomeontherun
Senior Coder

 
gnomeontherun's Avatar
 
Join Date: Sep 2007
Location: Houston
Posts: 2,846
Thanks: 10
Thanked 238 Times in 229 Posts
gnomeontherun will become famous soon enoughgnomeontherun will become famous soon enough
Well I would check your code at

Code:
	if(ball._x < 0) {
		oppScore++;
		
		ball._x = opp._x - 60;
		ball._y = opp_y;
		ball.dy = 0;
	}
because it doesn't reset the ball properly.

Then the ball doesn't really have any angle to its direction, so perhaps integrating some Math methods to give the ball a different direction based on where it hits the paddle.
__________________
jeremy - gnomeontherun
Educated questions often get educated answers, and simple questions often get simple answers.
gnomeontherun is offline   Reply With Quote
Old 03-24-2009, 01:32 PM   PM User | #3
Cmptr_Prgrmr2B
New Coder

 
Join Date: Jan 2009
Posts: 15
Thanks: 1
Thanked 0 Times in 0 Posts
Cmptr_Prgrmr2B is an unknown quantity at this point
I fixed part of my code like you posted, and went looking for a way to bounce the ball ad different angles. In my original code, this

Code:
ball.checkPaddles = function() {
	if (ball.hitTest(player)) {
	ball.dx = -ball.dx;
	ball.dy = getDy(player);
	}
	if (ball.hitTest(opp)) {
		ball.dx = -ball.dx;
		ball.dy = getDy(opp);
	}
}

function getDy(paddle) {
	relY = ball._y - paddle._y;
	relPerc - relY / paddle._height;
	trace ("relPerc");
	
	newDy = relPerc * 30;
	trace("NewDy");
	return newDy;
	
}
should have done it. For some reason it did not. Can you please look at this code as well?
Cmptr_Prgrmr2B is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:07 PM.


Advertisement
Log in to turn off these ads.