如何在ActionScript 3中制作角色无法移动的墙?

时间:2015-04-15 23:28:06

标签: actionscript-3 collision-detection hittest

我有一个2D口袋妖怪风格的游戏,我正在尝试创建一个培训师不应该穿过的墙(树木)。我如何使用hitTestObject来测试角色是否正在触摸树,然后让角色无法朝那个方向移动?

var upPressed:Boolean = false;
var downPressed:Boolean = false;
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
var facingDown:Boolean = false;
var noPressedU:Boolean = false;
var noPressedD:Boolean = false;
var noPressedL:Boolean = false;
var noPressedR:Boolean = false;


char.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey_2);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed_2);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed_2);
this.addEventListener( Event.ENTER_FRAME, handleCollision);

function fl_MoveInDirectionOfKey_2(event:Event)
{

if (upPressed && rightPressed)
{
    char.y -=  5;
    char.x +=  5;
    char.gotoAndPlay("mright");
}
else if (upPressed && leftPressed)
{
    char.y -=  5;
    char.x -=  5;
    char.gotoAndPlay("mleft");
}
else if (upPressed)
{
    char.y -=  5;
    char.gotoAndPlay("mup");
}
else if (downPressed && rightPressed)
{
    char.y +=  5;
    char.x +=  5;
    char.gotoAndPlay("mright");
}
else if (downPressed && leftPressed)
{
    char.y +=  5;
    char.x -=  5;
    char.gotoAndPlay("mleft");
}
else if (downPressed)
{
    char.y +=  5;
    char.gotoAndPlay("mdown");
}
else if (leftPressed)
{
    char.gotoAndPlay("mleft");
    char.x -=  5;
}
else if (rightPressed)
{
    char.gotoAndPlay("mright");
    char.x +=  5;
}
}

function fl_SetKeyPressed_2(event:KeyboardEvent):void
{
switch (event.keyCode)
{
    case Keyboard.UP :
        {
            upPressed = true;

            break;


        };
    case Keyboard.DOWN :
        {
            downPressed = true;
            break;


        };
    case Keyboard.LEFT :
        {
            leftPressed = true;
            break;


        };
    case Keyboard.RIGHT :
        {
            rightPressed = true;
            break;


    }

}
}

function fl_UnsetKeyPressed_2(event:KeyboardEvent):void
{
switch (event.keyCode)
{
    case Keyboard.UP :
        {
            upPressed = false;
            noPressedU = true;
            lastPressed = 1;
            break;


        };
    case Keyboard.DOWN :
        {
            downPressed = false;
            noPressedD = true;
            lastPressed = 2;
            break;


        };
    case Keyboard.LEFT :
        {
            leftPressed = false;
            noPressedL = true;
            lastPressed = 3;
            break;


        };
    case Keyboard.RIGHT :
        {
            rightPressed = false;
            noPressedR = true;
            lastPressed = 4;
            break;


        };
    case Keyboard.RIGHT && Keyboard.UP :
        {
            rightPressed = false;
            break;


    }
}
}

请原谅凌乱的编码。 ; - ;

0 个答案:

没有答案