# Bounce script help

10 replies to this topic

### #1 jmejay

jmejay

GMC Member

• New Member
• 18 posts

Posted 20 August 2011 - 11:15 AM

Hi GMC. My first topic!

Ok so I'm new to game maker but I've read the manual and I'm using GML. I would say I'm almost of an intermediate skill. I use gm 8.1 standard.
Basically for my first game I'm working on a small game, a basic breakout clone.
I'm looking for advice on making the script Bounce(direction); which is called from the balls step event.
My ball moves in a direction with a speed.
Here's how I'm hoping for the script to work:

If the ball hits the bottom or top of an object, Bounce is called. i.e. Bounce(90) would set the balls direction to 270. Bounce(91) would return / set 269.

Bounce(91) would change the direction to 89 if it hit the side of an object.

Hopefully I've made sense and you catch my drift, anyone know how to help? and thanks in advance!

-J

Edited by jmejay, 20 August 2011 - 11:22 PM.

• 0

### #2 Uniquebum

Uniquebum

GMC Member

• New Member
• 317 posts

Posted 20 August 2011 - 11:55 AM

```//Bounce script
var a;
if ( argument0 >= 180 ) { a = argument0-180; }
else { a = argument0+180; }
return a;
```

```//Call bounce
Bounce(direction);
```

Try that... might work.
• 0

### #3 jmejay

jmejay

GMC Member

• New Member
• 18 posts

Posted 20 August 2011 - 12:03 PM

Thanks unique - but,
It needs to be dependent on which side of an object it hits. So that wouldn't work, also I think you mean a directional change of 90 not 180.
• 0

### #4 Uniquebum

Uniquebum

GMC Member

• New Member
• 317 posts

Posted 20 August 2011 - 12:17 PM

You're quite correct. Let's try again.

```//Bounce script argument0 = direction
var a, b;
b = x-xprevious;
if ( b > 0 )
{
a = 90+argument0;
}
else
{
a = 90-argument0;
}
return a;

```
```//Call bounce
Bounce(direction);```

Maybe that works!

Edited by Uniquebum, 20 August 2011 - 12:19 PM.

• 0

### #5 jmejay

jmejay

GMC Member

• New Member
• 18 posts

Posted 20 August 2011 - 08:53 PM

Sadly not. I'm fairly certain it needs to check the side of another object. Is this possible?

Think about it, The speed change (you've shown there as a and would still have different effects if the ball hit the side or hit the bottom.

Does anyone know how to check if you hit the bottom or side of an object?
• 0

### #6 jmejay

jmejay

GMC Member

• New Member
• 18 posts

Posted 20 August 2011 - 11:03 PM

Alright I think I may be getting somewhere, but I've hit an error.

Ball Step Event:
```w = sprite_get_width(0); h = sprite_get_height(0);
bSpd = 4;
if(mouse_check_button_pressed(mb_left) || keyboard_check_pressed(vk_space)){connected = false;}
if(connected == true){x = objBat.x; y = objBat.y-16;}
else
{
speed = bSpd;
hitcube = collision_point(x,y,cube,false,true);
if(hitcube > 1)
{
direction = Bounce(direction, x, y, hitcube.x, hitcube.y, w, h);
hitcube = instance_destroy(); // or remove hp
score += 1;
}

hitbat = collision_point(x,y,objBat,false,true);

if(hitbat > 1)
{
direction = Bounce(direction, x, y, hitbat.x, hitbat.y, w, h);
}

}```

Bounce Script:
```dir = argument0; //direction
xx = argument1; //ball x
yy = argument2; //ball y
othx = argument3;// other obj x
othy = argument4;// other obj y
w = argument6;// ball w
h = argument7;// ball h
hw = w / 2;
hh = h / 2;
// other objs width is assumed to be 16 hence othx + 8

if(xx + hw < othx - 8) // if hits left side
{
retdir = 90 - dir; retdir *= 2;
dir += retdir;
return dir;
}

if(xx - hw > othx + 8) // if hits right side
{
retdir = 90 - dir; retdir *= 2;
dir += retdir;
return dir;
}

if(yy - hh > othy + 8)// if hits from below
{
retdir = 90 - dir; retdir += 270;
dir = retdir;
return dir;
}

if(yy + hh < othy - 8)// if hits from above
{
retdir = 90 - dir; retdir += 270;
dir = retdir;
return dir;
}

```

Error message:
ERROR in
action number 1
of Step Event
for object objBall:

Illegal argument count calling script "Bounce".
Script requires 8 arguments, 7 have been supplied.

Anyone? Think i'll need to sleep on it again...
• 0

### #7 Big J

Big J

GMC Member

• GMC Member
• 2818 posts
• Version:GM8.1

Posted 21 August 2011 - 12:06 AM

Am I missing something, or can you just use Game Maker's built-in bounce functionality?

For example, this could be a Collision Event with the breakout blocks:
```move_bounce_all(false);
with (other) instance_destroy();```
The argument in the move_bounce_* functions is "advanced" which is whether or not to use precise bouncing based on an object's shape. I think advanced bounce is not needed when all the objects are rectangles/squares. Hope that helps.

EDIT: As for your argument error, you "skipped" using argument5. You accessed 0123467 when it should have been 0123456.

Edited by Big J, 21 August 2011 - 12:13 AM.

• 1

### #8 jmejay

jmejay

GMC Member

• New Member
• 18 posts

Posted 21 August 2011 - 01:15 AM

As for your argument error, you "skipped" using argument5. You accessed 0123467 when it should have been 0123456.

Thanks man. Silly mistakes -.-

I just tried your move_bounce code and it works although is very limited as it only reverses the ball whereas I would like to refract it.
Not sure if thats the right terminology but hopefully you could understand via my bounce script what im trying.

Thanks a lot anyway!
• 0

### #9 Big J

Big J

GMC Member

• GMC Member
• 2818 posts
• Version:GM8.1

Posted 21 August 2011 - 01:21 AM

In this case, Game Maker's new argument check saved you a lot of headache. In older versions of Game Maker, you get no error and the script would simply do something unexpected since it would be reading the wrong arguments. So that error you got is actually very helpful information.
• 0

### #10 IceMetalPunk

IceMetalPunk

InfiniteIMPerfection

• Retired Staff
• 9259 posts
• Version:Unknown

Posted 21 August 2011 - 03:06 AM

jmejay, if you set the argument to move_bounce() to true, it will reflect correctly. Meaning, use move_bounce_all(true) instead of false. That should suit your needs, unless I've missed something specific about this case.

-IMP
• 1

### #11 jmejay

jmejay

GMC Member

• New Member
• 18 posts

Posted 21 August 2011 - 04:59 AM

jmejay, if you set the argument to move_bounce() to true, it will reflect correctly. Meaning, use move_bounce_all(true) instead of false. That should suit your needs, unless I've missed something specific about this case.

-IMP

Thanks.

Edited by jmejay, 21 August 2011 - 05:06 AM.

• 0

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users