Jump to content
  • Advertisement
Sign in to follow this  
Harmonics

Object collision detection

This topic is 2576 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

I am currently making a flash based rpg game, and started working on collision detection (I'm fairly new to flash).
I have a (somewhat) working 8 movement character with walking frames, and various blocks for collision testing.
Right now i managed to get the borders to work with "limitStageBoarder", it's the objects within i have troubles with.

The objects i have for collision testing detect my character hitting it, but many problems occur, and i can't figure out why.
Lets say i pressed the "Up" key to move up towards the object. It will hit it, but it will vibrate on it.
Not only that, but if i pressed the down key, it will go through the wall.

before you look at the code, you should know that ball=player.
Here is the code:


ball.stop();
ball.walking.stop();

var rightArrow:Boolean;
var leftArrow:Boolean;
var upArrow:Boolean;
var downArrow:Boolean;
var speed:int=5;

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
stage.addEventListener(Event.ENTER_FRAME, everyFrame);


function keyPressed(event:KeyboardEvent):void {
ball.walking.play();
if (event.keyCode==Keyboard.RIGHT) {
ball.gotoAndStop('walking');
rightArrow=true;
ball.scaleX=-1;
}
if (event.keyCode==Keyboard.LEFT) {
ball.gotoAndStop('walking');

leftArrow=true;
ball.scaleX=1;
}
if (event.keyCode==Keyboard.UP) {
ball.gotoAndStop('back');
upArrow=true;
}
if (event.keyCode==Keyboard.DOWN) {
ball.gotoAndStop('front');
downArrow=true;
}
}
function keyReleased(event:KeyboardEvent):void {
ball.walking.stop();
if (event.keyCode==Keyboard.RIGHT) {
rightArrow=false;
}
if (event.keyCode==Keyboard.LEFT) {
leftArrow=false;
}
if (event.keyCode==Keyboard.UP) {
upArrow=false;
}
if (event.keyCode==Keyboard.DOWN) {
downArrow=false;
}
}
function everyFrame(event:Event):void {

var oldx=ball.x;
var oldy=ball.y;
limitStageBorder(ball);
if (! ball.hitTestObject(box)) {

if (rightArrow) {
ball.x+=speed;
}
if (leftArrow) {
ball.x-=speed;
}
if (upArrow) {
ball.y-=speed;
}
if (downArrow) {
ball.y+=speed;
}



}else {
if (rightArrow) {
ball.x-=speed;
}
if (leftArrow) {
ball.x+=speed;
}
if (upArrow) {
ball.y+=speed;
}
if (downArrow) {
ball.y-=speed;





}
}
}



function limitStageBorder(object:MovieClip) {

var objectHalfWidth:uint=object.width/2;
var objectHalfHeight:uint=object.height/2;


if (object.x+objectHalfWidth>stage.stageWidth) {
object.x=stage.stageWidth-objectHalfWidth;
} else if (object.x - objectHalfWidth <0) {
object.x=0+objectHalfWidth;
}


if (object.y-objectHalfHeight<0) {
object.y=0+objectHalfHeight;
} else if (object.y + objectHalfHeight > stage.stageHeight) {
object.y=stage.stageHeight-objectHalfHeight;
}
}


Any help is appreciated =).

Share this post


Link to post
Share on other sites
Advertisement
Well, you first move your ball, then you check for collision and if it hits the target you'll set it back and this will loop as long as you move in that direction. That is what causing the vibration probably.

Try do the collision check before you move the ball, but now you probably have open space between the ball and the object it collides into so you have to find that length and move the ball right next to the collidable object. You could also try to match the ball to the object it collides too. Like if you move up and collide into a object move the balls top to the objects bottom.

Good luck!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!