PDA

View Full Version : Collision Issues



WhyidiE
May 6th, 2010, 11:21 AM
I know there are a few of these already but looking at some of them and trying them aren't working out very well.

I simply want to apply a collision box on a movieclip i.e. a box, on each of the frames, or for pretty much anything that can be seen as a "physical object". At the moment the character just walks across everthing, my team member created the character and implemented this code:

_root.attachMovie("character","char",_root.getNextHighestDepth());
char.xSpeed = 0;
char.ySpeed = 0;
char.onEnterFrame = function() {
if (Key.isDown(Key.UP)) {
trace("UP");
this.ySpeed = -2;
} else if (Key.isDown(Key.DOWN)) {
trace("DOWN");
this.ySpeed = 2;
} else {
this.ySpeed = 0;
}
if (Key.isDown(Key.LEFT)) {
trace("LEFT");
this.xSpeed = -3;
} else if (Key.isDown(Key.RIGHT)) {
trace("RIGHT");
this.xSpeed = 3;
} else {
this.xSpeed = 0;
}
if (this.xSpeed == 0 && this.ySpeed == 0) {
this.walk.gotoAndStop("stand");
}
if (this.xSpeed>0) {
char.gotoAndStop("right");
} else if (this.xSpeed<0) {
char.gotoAndStop("left");
} else if (this.ySpeed>0) {
char.gotoAndStop("down");
} else if (this.ySpeed<0) {
char.gotoAndStop("up");
}
this._x += this.xSpeed;
this._y += this.ySpeed;

}

Do i place the collision code on the character, if so where is the correct place, or do i place the code in the objects actions??

Lou
May 6th, 2010, 09:10 PM
You could look up hittesting, which is built into flash for simple collision detection. I would recommend reading over Tony Pa's tile based tutorials for a better solution.

http://www.kirupa.com/developer/actionscript/hittest.htm

http://www.tonypa.pri.ee/tbw/start.html