I'm making a few game tests on flash recently, mostly with the purpose of improving my as3 skills.
Well, I've made a mouse-controlled character, a circle, which moves towards the direction the mouse is pointing when clicking. The character is fixed in the middle of the screen and it is the other environment that moves, like a kind of scrolling but with gravity.
Anyway, everything works fine except for the collision detection between the character and the walls, which kill him. The wall is a rectangle with tunnels inside (mostly a "maze") and the hero is in these tunnels, inside the walls' bounding box, so the traditional hitTest with bounding boxes won't work. With every method flash provides, the hitTest detection is constant, so the hero keeps dying again and again, resseting the values and making impossible to move around. So I used the Carey O'Neil CDK library, but the result is also a failure: whith any method I keep getting the same result.
I've debugged the code and the function works like it should, so I think it is only an issue of hitTesting. I haven't found anything even though I've looked for an answer for several hours, except for hitTest detection in nested movieclips and similar things which don't help very much.
Here's a fraction of my code in the Circle_mc class (the hero):
package{
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Sprite;
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
import flash.display.Stage;
import com.greensock.*;
import com.greensock.easing.*;
import com.coreyoneil.collision.CollisionList;
public class Circle_mc extends MovieClip{
/*Various variables declarations*/
var myCollisionList:CollisionList;
var collisions:Array;
public function Circle_mc(){
/*Various variables value assingment*/
addEventListener(Event.ENTER_FRAME, action);
addEventListener(Event.ADDED_TO_STAGE, init);
}
function init(e:Event){
myCollisionList = new CollisionList(this);
myCollisionList.addItem(Game.walls1);
collisions = myCollisionList.checkCollisions();
mySprite = new Sprite;
}
public function action(e:Event){
this.rotation += Level_walls.x_vel_a;
trailRestraint++;
if(trailRestraint > 5 && ((Level_walls.x_vel_a > 1.1 || Level_walls.x_vel_a < -1.1) || (Level_walls.y_vel_a > 1.1 || Level_walls.y_vel_a < -1.1))){
rotation_now = this.rotation;
duplicateTrail();
}
if(collisions.length > 0){
die();
}
}
function duplicateTrail() {
var xPosition:Number = this.x;
var yPosition:Number = this.y;
var scaleFactor:Number=1;
var trail_unit:Trail = new Trail();
stage.addChildAt(trail_unit, numChildren - 1);
trail_unit.x=xPosition;
trail_unit.y=yPosition;
trail_unit.scaleX=scaleFactor;
trail_unit.scaleY=scaleFactor;
trail_unit.rotation = rotation_now;
}
function die(){
Level_walls.x_vel_a = 0;
Level_walls.y_vel_a = 0;
Game.walls1.x = stage.stageWidth /2;
Game.walls1.y = stage.stageHeight /2;
}
}
}
Well, Game.as is the Document Class, where I call most of the addChilds in the flash, so walls1 and circle or others are just instance names.
So, has anyone com across a similar problem? Any help/hint/piece of information would be very appreciated.
Thanks in advance!