# Gravity and Collision in CS4

**URL:** <https://forum.kirupa.com/t/gravity-and-collision-in-cs4/296420>\
**Category:** flash\
**Created:** [September 14, 2009, 10:05pm UTC](https://forum.kirupa.com/t/gravity-and-collision-in-cs4/296420 "2009-09-14T22:05:45Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Me2LoveIt2](https://avatars.discourse-cdn.com/v4/letter/m/a9adbd/32.png) [@Me2LoveIt2](https://forum.kirupa.com/u/Me2LoveIt2)\
**Post date:** [September 14, 2009, 10:05pm UTC](https://forum.kirupa.com/t/gravity-and-collision-in-cs4/296420/1 "2009-09-14T22:05:45Z")

</div>

I worked on a new Flash project, but I cant seem to find a good solution for how I want my objects to collide.

In my project I have: a Ball(moving object) and a Floor(not moving).

The ball is positioned over the floor and drops onto the floor through the gravity that is applied with the code.

Code:

```auto

var gy:Number = 0;
var gravity:Number = .2

function ballMovement(event:Event):void{
	if(ball_mc.hitTestObject(floor_mc)){
		ball_mc.y = floor_mc.y-23 //the -23 is because the ball has a diameter of 46 and seems to me calculating from //the middle, i figured this out through trial and error
		gy=0 //set the gravity to zero once it is on the floor, so it stops moving
	} else { //this part does the gravity
		gy+=gravity;
		ball_mc.y+=gy;
	}
};

this.addEventListener(Event.ENTER_FRAME, ballMovement);

```

My problems are these.

First: the -23 in the code is what I don’t like.

Second: every single object that deals with collisions would need to be put into the if statement.

Bottom line:  
\*\*I Don’t like this code!

Is there a way to have a global gravity in flash AS3. In which you could choose what objects you want to be affected by it?

Is there a way to have a block of code that separates moving from still objects and will not let any objects collide(not overlap)?\*\*
