针对关卡构建阵列进行测试

时间:2012-10-15 12:45:58

标签: arrays actionscript-3 sprite hittest

我正在构建一个自上而下(鸟瞰)游戏,一个相当简单的概念,一个早期的塞尔达传说或最终幻想游戏。我有一个内置的水平和角色添加和可移动,与滚动相机,但一个问题困扰我。 Hittesting。

使用数组创建关卡 - 简单[1,1,1];数组,它检查数组中的值是否匹配,然后创建一个简单的方形Sprite并将其添加到一个包含每个项目的更大的Sprite中。最后,然后将播放器添加到舞台并使用箭头键控制。我将它用作移动系统:

stage.addEventListener(KeyboardEvent.KEY_DOWN, checkDown);
stage.addEventListener(KeyboardEvent.KEY_UP, checkUp);
stage.addEventListener(Event.ENTER_FRAME, update);

function checkDown(e:KeyboardEvent):void {
   switch (e.keyCode) {
    case 65 :
        leftDown = true;
        break;
    case 37 :
        leftDown = true;
        break;
    case 68 :
        rightDown = true;
        break;
    case 39 :
        rightDown = true;
        break;
    case 87 :
        upDown = true;
        break;
    case 38 :
        upDown = true;
        break;
    case 83 :
        downDown = true;
        break;
    case 40 :
        downDown = true;
        break;
}
}

function checkUp(e:KeyboardEvent):void {
switch (e.keyCode) {
    case 65 :
        leftDown = false;
        break;
    case 37 :
        leftDown = false;
        break;
    case 68 :
        rightDown = false;
        break;
    case 39 :
        rightDown = false;
        break;
    case 87 :
        upDown = false;
        break;
    case 38 :
        upDown = false;
        break;
    case 83 :
        downDown = false;
        break;
    case 40 :
        downDown = false;
        break;
}
}

function update(e:Event):void {
if (upDown) {
    if (genericHolder.y >= boundariesLevelOne[(room*4) - 4]) {
        genericHolder.y = boundariesLevelOne[(room*4) - 4];
        Player.y-=power;
    }
    if (genericHolder.y < boundariesLevelOne[(room*4) - 4]) {
        genericHolder.y+=power;
    }
    Player.Stand.visible = false;
    Player.Forwards.visible = false;
    Player.Rights.visible = false;
    Player.Lefts.visible = false;
    Player.Backwards.visible = true;
    playerDirection = 2;
}
if (downDown) {
    if (genericHolder.y <= boundariesLevelOne[(room*4) - 3]) {
        genericHolder.y = boundariesLevelOne[(room*4) - 3];
        Player.y+= power;
    }
    if (genericHolder.y > boundariesLevelOne[(room*4) - 3]) {
        genericHolder.y-=power;
    }
    Player.Stand.visible = false;
    Player.Backwards.visible = false;
    Player.Rights.visible = false;
    Player.Lefts.visible = false;
    Player.Forwards.visible = true;
    playerDirection = 1;
}
if (leftDown) {
    if (genericHolder.x >= boundariesLevelOne[(room*4) - 2]) {
        genericHolder.x = boundariesLevelOne[(room*4) - 2];
        Player.x -= power;
    }
    if (genericHolder.x < boundariesLevelOne[(room*4) - 2]) {
        genericHolder.x+=power;
    }
    Player.Stand.visible = false;
    Player.Backwards.visible = false;
    Player.Forwards.visible = false;
    Player.Rights.visible = false;
    Player.Lefts.visible = true;
    playerDirection = 4;
}
if (rightDown) {
    if (genericHolder.x <= boundariesLevelOne[(room*4) - 1]) {
        genericHolder.x = boundariesLevelOne[(room*4) - 1];
        Player.x += power;
    }
    if (genericHolder.x > boundariesLevelOne[(room*4) - 1]) {
        genericHolder.x-=power;
    }
    Player.Stand.visible = false;
    Player.Backwards.visible = false;
    Player.Forwards.visible = false;
    Player.Lefts.visible = false;
    Player.Rights.visible = true;
    playerDirection = 3;
}
if (!upDown && !downDown && !leftDown && !rightDown) {
    Player.Backwards.visible = false;
    Player.Forwards.visible = false;
    Player.Rights.visible = false;
    Player.Lefts.visible = false;
    Player.Stand.visible = true;
    Player.Stand.gotoAndStop(playerDirection);
}
if (Player.Stand) {
    Player.Stand.gotoAndStop(playerDirection);
     }
}

TL; DR背景移动而不是玩家,移动由变量控制。

我怎样才能对付我添加的任何精灵?目前,如果我尝试Hittest一个特定的Sprite,它会再次测试整个Sprite持有者,而不是我想要的特定Sprite。非常感谢任何帮助,谢谢。

1 个答案:

答案 0 :(得分:0)

您使用的是最热门的方法吗?

http://www.actionscriptmoron.com/AS3Tutorials/hittest-hittestobject/

你的意思是它再次测试整个精灵持有者只是获取所包含对象的引用并将其传递给hittest方法。它应该工作。