We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,344 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Pre-Collision Detection question

FYI - I'm coding in actionscript 3.0 but didn't see an area to post for that language.

I made a simple character and a block. I'm trying to get it so that if the character will touch the box, it wont let him move to imply a wall/collision.

I've got him moving but the problem is that it doesn't PREdetect the collision. It lets him overlap the block once, then when I try to move again, it wont go anywhere because NOW (that it's too late), he's overlapping and thus colliding. Anyone know how I can very simply edit this code so the player isn't allowed to overlap the block in the first place?

import flash.display.Sprite;
import flash.events.KeyboardEvent;

var block:Sprite = MovieClip(blockInst);
var player:Sprite = MovieClip(playerInst);
var speed:Number = 10;

stage.addEventListener(KeyboardEvent.KEY_DOWN, playerMovement);

function playerMovement(key:KeyboardEvent):void
{
	if (player.hitTestObject(block))
	{
		trace("blocked");
	}
	
	else
	{
		switch (key.keyCode)
		{
			case 37 ://Left
				player.x -=  speed;
				break;

			case 39 ://Right
				player.x +=  speed;
				break;

			case 38 ://Up
				player.y -=  speed;
				break;

			case 40 ://Down
				player.y +=  speed;
				break;
		}
	}

}
3
Contributors
1
Reply
1 Month
Discussion Span
1 Year Ago
Last Updated
2
Views
crapgarden
Junior Poster in Training
59 posts since Jul 2010
Reputation Points: 16
Solved Threads: 0
Skill Endorsements: 0

Yea so this is a problem of collision response. Once a collision is detected what do you do? Well in this case you should move your player back just before the collision point. So if there is a collision, shift back the player's position so that there is not a collision anymore. Get it?

firstPerson
Industrious Poster
4,046 posts since Dec 2008
Reputation Points: 851
Solved Threads: 626
Skill Endorsements: 15

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0575 seconds using 2.67MB