PDA

View Full Version : A problem with balls and bricks


NealJMD
04-07-2008, 10:37 PM
I have created a simple cannon and bricks puzzle game in which the player shoots a ball, which bounces all about. However, I can't make my ball appropriately bounce of the bricks because the brick is either designed to bounce it horizontally or vertically...

brick1.onEnterFrame = function (){
//odd numbered bricks are tall and thin, even are wide and flat.
bounceV(brick1);
bounceV(brick3);
bounceV(brick5);
bounceV(brick7);
bounceV(brick9);
bounceV(brick11);
bounceH(brick2);
bounceH(brick4);
bounceH(brick6);
bounceH(brick8);
}

function bounceV(brick){
if (brick.hitTest(bullet)){
bullet._x -= bullet.dx / 3;
bullet._y -= bullet.dy / 3;
bullet.dx *= -1;
}
}

function bounceH(brick){
if (brick.hitTest(bullet)){
bullet.dy *= -1;
}
}


There must be a trick for this. Thank you very much for your help.

Durnus
04-08-2008, 01:21 PM
Well, help me out. What is it supposed to do and what is it doing instead?

All I see is that

function bounceV(brick){
if (brick.hitTest(bullet)){
bullet._x -= bullet.dx / 3; //You shouldn't need these two lines.
bullet._y -= bullet.dy / 3;//What are they for anyway?
bullet.dx *= -1;//This is my edit.... lol, examine this line. Shouldn't you be flipping dy, not dx, for a vertical bounce?
}
}

ASWC
04-08-2008, 01:56 PM
It might not be the answer Durnus but you got a good point here.
NealJMD, it's important to understand that with games or motion in general you should always alter a variable and not the actual MC property, this allows you a better control and a easier code. consider this:
var speed:Number=5;
this.onEnterFrame= function(){
myMovie._x+=speed;
}
myButton.onRelease=function(){
speed=-5;
}
You set the myMovie property only once but by using a variable you can change the way the MC behave by just changing the variable instead. In this preceding example the movie changes its direction. Now if you were to try that by just setting the MC property, you would do that and fail:
this.onEnterFrame= function(){
myMovie._x+=5;
}
myButton.onRelease=function(){
myMovie._x-=5;
}
When you click the button the movie goes back 5 pixels and then keeps going the other way, I hope you see what we mean now.

rrh
04-08-2008, 03:17 PM
I think I understand the question; the trick is telling whether it has hit the brick on a horizontal edge of a vertical edge.
(This assumes your brick's registration point is in the centre. It will need to be changed if it's in the corner.)
if ((bullet._y>brick._y-brick._height)&&(bullet._y<brick._y+brick._height)) {
if ((bullet._x-bullet.dx<brick._x-brick._width/2)&&(bullet._x>=brick._x-brick._width/2)) {
//hit the left side
}
}
Then repeat for other sides.

Durnus
04-08-2008, 05:57 PM
Remember though, that the movement is symmetric, and you don't have to make one piece of code for each side, only one piece for two opposite sides. Since all you have to do is multiply the velocity by a negative number to make it "bounce," you can just make it detect which set of sides it is colliding with and then alter the X or Y velocity.


if( (bullet._y < brick._y + brick._height/2) && (bullet._y > brick._y - brick._height/2) )
{
if( (bullet._x > bullet._x + brick._width/2) && (bullet._x < bullet._x - brick._width/2) )
{
if( (bullet._x + bullet.dx <= bullet._x + brick._width/2) && (bullet._x + bullet.dx >= bullet._x - brick._width/2) )
{
bullet.dx *= -.98; //98% of movement speed preserved.
}
}
}

Repeat for vertical sides.

Just a warning, that code is guarenteed not to work. ;) Should give you the gist of it though.