Optimising on Collision Detection
Not seen anyone make this point yet so thought I would.
Not going to be of use all the time but will save a litle on unnecessary loops running constantly.
Say you are making a scrolling space shooter such as Project monochrome or Raiden X.
You have many enemies on screen at a time and need to detect if your ship or its bullets are making contact with each one.
This could mean you run a for loop every frame on each bullet, and the player ship
e.g
onClip(enterFrame){
for(var i:Number = 0;i < enemyNo;i++){
if (_root["enemy" + i].hitTest (this._x,this._y,true)){
//actions;
}
}
}
Instead of looping to check whether 10's of mcs are colliding wih eachother every frame, you can add all your "enemy"+i mc's into one single parent mc called "enemyParent".
This means each of your bullets, obstacles or player ship hit points can be tested against this single mc every framebefore looping through it to see which exact child mc it is touching , instead of being looped to check every frame by default.
e.g
onClipEvent(enterFrame){
if(_root.enemyParent_mc.hitTest(this._x,th
is._y,true)){
for (var i:Number = 0;i < enemyNumber;i++ ){
if(_root.enemyParent_mc["enemy" + i].hitTest(this._x,this._y,true)){
//actions;
}
}
}
}
This checks for detection with enemyParent_mc every frame, if collision evaluated to true, a loop is then run to check if each child mc of enemyParent_mc is collising with the _x _y co ordinates of your object. As the loop isnt run every frame, just when hitTest with the parent is true, processing power is saved.
Add your destruction/whatever actions in.
This is just the basic concept, you may have to mess with localToGlobal/globalToLocal to get hitTests to work on objects on different timelines depending on your game layout.
You may also use a for in loop to loop through each object in enemyParent_mc ,though I have yet to experiment with this technique.
AS:Loops
AS:Main
AS:For...in Loops
AS:Performance & Optimisation