DiagonalCharacter Movement(BackGround Movement) using KEY and hitTestPoint

December 8th, 2010 by aabhaanjan

Diagonal Movement of Character in a specific area using keyboard command:

Here we are handling the diagonal movement of  the character using combination of keys.

[xml]

//On EnterFrame track the Key in Boolean values

//@ isKeyPressedUp:Boolean represents true or false value for KEY Press

//@ isKeyPressedLeft:Boolean represents true or false value for KEY Press

//@ isKeyPressedRight:Boolean represents true or false value for KEY Press

//@ isKeyPressedDown:Boolean represents true or false value for KEY Press

//function handleCharacterMovement() : Calling function for movement of player

private function HandleCharacterMovementOnEnterFrame(e:enterFrame):void {

if (_isKeyPressedUp&&_isKeyPressedLeft) {

handleCharacterMovement(“top-right”);

return;

}

if (_isKeyPressedUp&&_isKeyPressedRight) {

handleCharacterMovement(“top-left”);

return;

}

if (_isKeyPressedDown&&_isKeyPressedLeft) {

handleCharacterMovement(“bottom-right”);

return;

}

if (_isKeyPressedDown&&_isKeyPressedRight) {

handleCharacterMovement(“bottom-left”);

return;

}

}

//On EnterFrame track the Key in Boolean values

//@ handleTopLeftMovement():Calling function for top-left movement of player

//@ handleTopRightMovement():Calling function for top-right movement of player

//@ handleBottomLeftMovement():Calling function for bottom-left  movement of player

//@ handleBottomRightMovement():Calling function for bottom-right movement of player

private function handleCharacterMovement(__direction:String = “none”):void {

switch (__direction) {

case “none” :

break;

case “top-left” :

handleTopLeftMovement();

break;

case “top-right” :

handleTopRightMovement();

break;

case “bottom-left” :

handleBottomLeftMovement();

break;

case “bottom-right” :

handleBottomRightMovement();

break;

}

}

//On EnterFrame track the Key in Boolean values

//Movement is being handled by TweenLite.

//@ handleTopLeftMovement():Calling function for top-left movement of player

//#Checking if the character is on walk able area using hitTestPoint

//#Making the Character view according to the movement

private function handleTopLeftMovement():void {

if (mazeMC.hitTestPoint(character.x + (character.width – 5) / 2, character.y, true)) {

if (mazeMC.hitTestPoint(character.x, character.y – (character.height – 5) / 2, true)) {

TweenLite.to(wall, _tweenDuration, {x: wall.x + _10, y: wall.y + _10});

}

}

character.gotoAndStop(“top-left-pos”);

}

//@ handleTopRightMovement():Calling function for top-right movement of player

//#Checking if the character is on walkable area using hitTestPoint

//#Making the Character view according to the movement

private function handleTopRightMovement():void {

if (mazeMC.hitTestPoint(character.x + (character.width – 5) / 2, character.y, true)) {

if (mazeMC.hitTestPoint(character.x, character.y – (character.height – 5) / 2, true)) {

TweenLite.to(wall, _tweenDuration, {x: wall.x – _10, y: wall.y + _10});

}

}

character.gotoAndStop(“top-right-pos”);

}

//@ handleBottomLeftMovement():Calling function for bottom-left  movement of player

//#Checking if the character is on walkable area using hitTestPoint

//#Making the Character view according to the movement

private function handleBottomLeftMovement():void {

if (mazeMC.hitTestPoint(character.x + (character.width – 5) / 2, character.y, true)) {

if (mazeMC.hitTestPoint(character.x, character.y + (character.height – 5) / 2, true) == true)

{

TweenLite.to(wall, _tweenDuration, {x: wall.x + _10, y: wall.y – _10});

}

}

character.gotoAndStop(“left_bottom_pos”);

}

//@ handleBottomRightMovement():Calling function for bottom-right movement of player

//#Checking if the character is on walkable area using hitTestPoint

//#Making the Character view according to the movement

private function handleBottomRightMovement():void {

if (mazeMC.hitTestPoint(character.x + (character.width – 5) / 2, character.y, true)) {

if (mazeMC.hitTestPoint(character.x, character.y + (character.height – 5) / 2, true) == true) {

TweenLite.to(wall, _tweenDuration, {x: wall.x – _10, y: wall.y – _10});

}

}

character.gotoAndStop(“right_bottom_pos”);

}

[/xml]

Tags: , , , , , , , , , ,