Be a Supporter!

[As3] wall collision

  • 726 Views
  • 3 Replies
New Topic Respond to this Topic
stickman8190
stickman8190
  • Member since: Mar. 7, 2012
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
[As3] wall collision 2012-08-28 11:37:49 Reply

I'm having a problem with the collision detection, I'm trying to make it that the player can't move through the wall, I've search for things like that and I couldn't find the one that works for the code I'm using, so can anybody help please?

here's the code:

import flash.events.Event;
import flash.events.KeyboardEvent;

var playerSpeed:Number = 10;
var playerSuperSpeed:Number = 20;
var movingUp:Boolean = false;
var movingDown:Boolean = false;
var movingLeft:Boolean = false;
var movingRight:Boolean = false;
player_mc.x = start_mc.x;
player_mc.y = start_mc.y;


stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyisDown);
stage.addEventListener(KeyboardEvent.KEY_UP, KeyisUp);

function enterFrameHandler(event:Event):void{
	if(movingLeft && !movingRight){
		player_mc.x -= playerSpeed;
		player_mc.rotation = 270;
	}
	if(movingRight && !movingLeft){
		player_mc.x += playerSpeed;
		player_mc.rotation = 90;
	}
	if(movingUp && !movingDown){
		player_mc.y -= playerSpeed;
		player_mc.rotation = 0;
	}
	if(movingDown && !movingUp){
		player_mc.y += playerSpeed;
		player_mc.rotation = 180;
	}
	if(movingLeft && movingUp && !movingRight && !movingDown){
		player_mc.rotation = 315;
	}
	if(movingRight && movingUp && !movingLeft && !movingDown){
		player_mc.rotation = 45;
	}
	if(movingLeft && movingDown && !movingRight && !movingUp){
		player_mc.rotation = 225;
	}
	if(movingRight && movingDown && !movingLeft && !movingUp){
		player_mc.rotation = 135;
	}
}

function KeyisDown(event:KeyboardEvent):void{
	switch(event.keyCode){
		case Keyboard.UP:
		movingUp = true;
		break;
		
		case Keyboard.DOWN:
		movingDown = true;
		break;
		
		case Keyboard.LEFT:
		movingLeft = true;
		break;
		
		case Keyboard.RIGHT:
		movingRight = true;
		break;
		
	}
}
function KeyisUp(event:KeyboardEvent):void{
	switch(event.keyCode){
		case Keyboard.UP:
		movingUp = false;
		break;
		
		case Keyboard.DOWN:
		movingDown = false;
		break;
		
		case Keyboard.LEFT:
		movingLeft = false;
		break;
		
		case Keyboard.RIGHT:
		movingRight = false;
		break;
	}
}

also click Here to view my profile at jiggmin.com

BBS Signature
MSGhero
MSGhero
  • Member since: Dec. 15, 2010
  • Offline.
Forum Stats
Supporter
Level 16
Game Developer
Response to [As3] wall collision 2012-08-28 12:13:31 Reply

Use hitTestObject or--better yet--math to see if the two are colliding. With math, you're looking for the player's left/rightmost point to be within the wall's right/leftmost point. Then you just push the player back by the overlap amount.

stickman8190
stickman8190
  • Member since: Mar. 7, 2012
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to [As3] wall collision 2012-08-29 12:53:07 Reply

At 8/28/12 12:13 PM, MSGhero wrote: Use hitTestObject or--better yet--math to see if the two are colliding. With math, you're looking for the player's left/rightmost point to be within the wall's right/leftmost point. Then you just push the player back by the overlap amount.

OK, but how do I make the player only touch it from the left/right? (sorry for the late reply)


also click Here to view my profile at jiggmin.com

BBS Signature
caseymacneil
caseymacneil
  • Member since: Nov. 19, 2008
  • Offline.
Forum Stats
Member
Level 06
Programmer
Response to [As3] wall collision 2012-08-29 20:27:11 Reply

At 8/29/12 12:53 PM, stickman8190 wrote:
At 8/28/12 12:13 PM, MSGhero wrote: Use hitTestObject or--better yet--math to see if the two are colliding. With math, you're looking for the player's left/rightmost point to be within the wall's right/leftmost point. Then you just push the player back by the overlap amount.
OK, but how do I make the player only touch it from the left/right? (sorry for the late reply)

ok dude, its not that hard. if the player collides and player's x position < object 2's x position, player is colliding with the left side of the box.