New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Creating the CollisionManager class

From: Building Flash Games with Starling

Video: Creating the CollisionManager class

So like I showed you in the last movie we are going to be using distance-based collision detection for our game. Now I could do that collision detection directly in the Play state, but again, it's nice if we create a CollisionManager class which is going to handle that for us. So I am going to right-click on my managers package, New > ActionScript Class and this one is going to be called CollisionManager, and to the constructor we are going to send in our Play state. So I am going to create an argument called play, it's going to be of type Play, and we'll create that as an instance variable, so this.play = play.

Creating the CollisionManager class

So like I showed you in the last movie we are going to be using distance-based collision detection for our game. Now I could do that collision detection directly in the Play state, but again, it's nice if we create a CollisionManager class which is going to handle that for us. So I am going to right-click on my managers package, New > ActionScript Class and this one is going to be called CollisionManager, and to the constructor we are going to send in our Play state. So I am going to create an argument called play, it's going to be of type Play, and we'll create that as an instance variable, so this.play = play.

And we are going to create a public update function which we are going to call from our play state. So public function update, and we are not going to return anything here. And in this update method we are going to call two different functions and they are going to check collisions between the bullets and the aliens, and then also we are going to check collisions between our hero ship and the aliens, because essentially in our game it's pretty simple; the game is going to be over if an alien crashes into you essentially.

So we are going to first call the bulletsAndAliens function, so we'll just define that here and we can do a quick fix to actually create that function. Then we are going to be doing the heroAndAliens check and I'll create that method. So we are going to start with the bulletsAndAliens function. So now what we need to do is to get a reference to the bullets array and the aliens array which are in those manager classes.

But remember, by default, we create all of our class properties as private so we need to now expose those as public. So I am going to go into my BulletManager class and this bullets:Array, we are going to change its access modifier to public, and then also in our Play method we need to make sure that our bulletManager and our alienManager class is also exposed as public, so that we can access it from our CollisionManager.

And then I also need to go into the AlienManager and make sure that aliens:Array is also set to public. Okay, so I am going to get references to both of those, so I am going to create a local variable called ba for bullets array and this is going to be of type Array, is equal to play.bulletManager.bullets. And we are going to do the same thing for the aliens:Array, so var aa = play.alienManager.aliens.

Now we are going to create two local variables as we loop through to store the references to the bullet and to the alien, so var b is going to be of type Bullet and var a is going to be of type Alien. So what we need to do is to create two nested for loops. So we are going to loop through all of the bullets and for each bullet we are then going to test whether it's touching or close enough to any other aliens that are currently in play.

So I'm going to create a for loop, var i:int, and remember that we are going to want to loop backwards through these, so is equal ba.length-1; i>=0; i--). Now I am going to copy this line right here because we are going to create an inner for loop, and I am going to change the variable to j, and then this is going to be aa.length-1; j>=0; j--).

What I want to do inside of this first for loop is get a reference to the current bullet, so I am going use that b variable and I am going to go into ba and go to the ith element of that array like that. And now inside of the second for loop, I am going to set that alien local variable, to aaj. So now we need to test the distance between the alien and the bullet, and to do that we are going to use the point class. So we are essentially going to create two point objects that contain the X and Y of both the bullet and the alien.

Now we could create those in here but it's a lot better performance if we create those at the top as class properties. So we are going to say private var p1, is of type Point, and we are going to instantiate it right here, new Point, and we are going to do the same thing for the second point. p2, type Point = new Point. And then all we are going to do inside of this loop is just adjust the X and the Y position of that point, that way we were not instantiating a new point on every frame.

So let's first get the position of our bullet and we are going to use that p1 point. So we are going to set p1.x = b.x, p1.y = b.y and now we'll set the second point, the x property to the aliens x and we'll set the y property to the alien y. Well now we can use a static function on the Point class called Distance, which will give us the distance between these two objects.

So we are going to test if the difference is small enough and then we are going to determine that a collision has occurred. So we are going to use an if statement. We are going to say if Point.distance, and here we pass in those two point objects, p1 and p2, if it's less than the radius of both the alien and the bullet. So if it's less than a, and we are going to use the pivotY of it, because remember these things are actually coming down vertically up and down so we need to get the center vertically of each of these objects.

If it's less than a.pivotY + b.pivotY, then we are going to go ahead and determine that a collision has occurred, and in that case, we want to destroy both the bullet and the alien. So we are going to say play. alienManager.destroyAlien(a); and then play.bulletManager.destroyBullet(b).

And if you want to make sure you have these set correctly, if we go into our Alien class and our Bullet class, here we are setting the pivotX and pivotY to make sure it's in the center of our alien. If we go to the Bullet class this actually should not be image, so let's go ahead and remove that to make sure it's in the center of our actual bullet sprite itself. So now we are going to determine that a collision has occurred here. So now if we actually go and create our CollisionManager now in our Play class, let's just create it right under our alienManager and we'll call it collisionManager = new CollisionManager, and we are going to pass in this, and create that as an instance variable.

And much like before, we are going to come to our update method and call the update method of the collisionManager. So now let's go ahead and test it and see what happens. So if I click, now you can see when I am shooting I am actually now killing aliens and both the bullets and the aliens are actually going away. Now we haven't got to adding particle effects or anything like that, but we can see the collision is now working. Now let's say we wanted to make it so the bullet had to be even closer to the alien, but we could come to the CollisionManager and actually update this.

So we can make it a little less than this if we wanted to, but this is actually fine for what we are doing now. Now lastly, we need to do the heroAndAliens because that's how we actually are going to die and the game is going to be over. So we are going to loop through the aliens, so I am going to copy this line from bulletsAndAliens, and then again, I want to get a local alien variable. So I'll just copy this, and I am going to go ahead and copy this for loop, because we are going to do a very similar thing here, and we are going to be looping through aa here.

And again, let's get that local reference to aai. And now essentially we are going to do the same thing, where we are setting this point object. So let's go ahead and copy that, and this point can say the same, but in this one we are actually going to be getting the x and y of our hero, so play, and we need to make sure the hero is actually public in our Play state, which it is. So here, if we say play.hero.x, play.hero.y. And again, we are going to do this if statement and we are going to check the distance between these two points. If it's less than a.pivotY and then we want play.hero.pivotY.

Then we are not going to worry about any of this because we are going to make it so that the game is actually over. So for that, we are going to say play, and we need to make sure that our game in our Play state is also public. So let's go ahead and modify that to public. So we'll do play.game.changeState and then we'll go into that Game class and get the GAME_OVER constant. Okay, so now we have added it. If we actually test this now, we can see that as I shoot, I am actually killing the aliens, but I'll let myself hit one of the aliens and we can see that everything is paused.

Now again, we haven't created the destroy method for our Play state yet and we don't have any visuals yet on the GameOver state. But you can see that our collision detection is actually working and you can see here where it actually stopped, that's where that collision has occurred. And you can see with that distance-based collision detection that's about the right location. So we can, again, adjust that, expand the numbers dependent on our case, but the collision detection is now working. In the next movie we are going to do all of our clean up and handle destroying of the Play state before we move to the GameOver state.

Show transcript

This video is part of

Image for Building Flash Games with Starling
Building Flash Games with Starling

46 video lessons · 9363 viewers

Lee Brimelow
Author

 
Expand all | Collapse all
  1. 2m 9s
    1. Welcome
      45s
    2. Using the exercise files
      36s
    3. What you should know
      48s
  2. 10m 21s
    1. Checking out the finished product
      1m 10s
    2. Downloading the Starling framework
      4m 13s
    3. Installing Flash Builder
      1m 44s
    4. Installing additional tools
      1m 30s
    5. Overview of the Starling framework
      1m 44s
  3. 13m 0s
    1. Creating the Flash Builder project
      2m 40s
    2. Setting up your Starling scene
      6m 7s
    3. Creating the main game class
      4m 13s
  4. 13m 52s
    1. Creating the IState interface
      2m 14s
    2. Creating the three game states
      3m 50s
    3. Building the state machine
      7m 48s
  5. 22m 14s
    1. Creating a static assets class
      2m 3s
    2. Adding images and creating textures
      3m 24s
    3. Creating sprite sheets with TexturePacker
      6m 26s
    4. Creating the main texture atlas
      3m 29s
    5. Creating and importing bitmap fonts
      6m 52s
  6. 14m 16s
    1. Creating the Background class
      5m 55s
    2. Adding the logo
      2m 58s
    3. Adding the play button
      3m 4s
    4. Implementing the destroy method
      2m 19s
  7. 56m 47s
    1. Adding the background
      1m 3s
    2. Creating the Hero class
      6m 14s
    3. Creating the Bullet class
      1m 44s
    4. Creating the BulletManager class
      4m 23s
    5. Using the StarlingPool class
      9m 24s
    6. Firing bullets with the mouse
      7m 24s
    7. Creating the Alien movie clip
      1m 58s
    8. Creating the AlienManager class
      8m 12s
    9. Overview of collision-detection options
      3m 10s
    10. Creating the CollisionManager class
      10m 51s
    11. Implementing the destroy method
      2m 24s
  8. 7m 59s
    1. Adding the background
      1m 10s
    2. Creating the "Game Over" text
      2m 43s
    3. Adding the Try Again button
      4m 6s
  9. 20m 5s
    1. Exploring particle-creation tools
      4m 52s
    2. Adding the smoke texture
      5m 3s
    3. Creating the Explosion class
      2m 6s
    4. Creating the ExplosionManager class
      8m 4s
  10. 11m 5s
    1. Creating sound effects with cfxr
      3m 5s
    2. Adding the sound effects
      3m 42s
    3. Creating the Score class
      4m 18s
  11. 6m 1s
    1. Starling optimization tips
      4m 8s
    2. Helpful Starling resources
      1m 53s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold

Are you sure you want to delete this note?

No

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.