AS3中的对象障碍?

时间:2015-06-28 01:19:03

标签: actionscript-3 flash

我是编码的初学者,但我正在尝试制作一个flash rpg类型的游戏。我在比赛中有一个障碍和一个行走的人(manmc)。我一直在观看有关如何制作对象障碍的视频/阅读文章,但我似乎无法在不出错的情况下正确编码。我试图让他的角色在他击中一棵树时停下来。树被编码为(treer1_MC)。我只是尝试在下压时对其进行编码,但它没有做任何事情,导致角色无法向后走。

import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Stage;

manmc.gotoAndStop ("Stand Front Frame");

var rightPressed:Boolean = new Boolean(false);
var leftPressed:Boolean = new Boolean(false);
var upPressed:Boolean = new Boolean(false);
var downPressed:Boolean = new Boolean(false);
var manSpeed:Number = 3;

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME, gameLoop);

function keyDownHandler (keyEvent:KeyboardEvent):void
{
    if(keyEvent.keyCode == Keyboard.RIGHT)
    {
        rightPressed = true;
    }
    else if(keyEvent.keyCode == Keyboard.LEFT)
    {
        leftPressed = true;
    }
    else if(keyEvent.keyCode == Keyboard.UP)
    {
        upPressed = true;
    }
    else if(keyEvent.keyCode == Keyboard.DOWN)
    {
        downPressed = true;
    }
}

function keyUpHandler (keyEvent:KeyboardEvent):void
{
    if(keyEvent.keyCode == Keyboard.RIGHT)
    {
        rightPressed = false;
        manmc.gotoAndStop("Stand Right Frame");
    }
    else if(keyEvent.keyCode == Keyboard.LEFT)
    {
        leftPressed = false;
        manmc.gotoAndStop("Stand Left Frame");
    }
    else if(keyEvent.keyCode == Keyboard.UP)
    {
        upPressed = false;
        manmc.gotoAndStop("Stand Back Frame");
    }
    else if(keyEvent.keyCode == Keyboard.DOWN)
    {
        downPressed = false;
        manmc.gotoAndStop("Stand Front Frame");
    }
}

function gameLoop(loopEvent:Event):void
{
    if(rightPressed)
    {
        if(manmc.x < 1050)
        {
            manmc.x += manSpeed;
        }
        manmc.gotoAndStop("Walk Right Frame");
    }
    else if(leftPressed)
    {
        if (manmc.x > 145)
        {
            manmc.x -= manSpeed;
        }
        manmc.gotoAndStop("Walk Left Frame");
    } 
    else if(downPressed)
    {
        if(manmc.y < 780)
        {
        manmc.y += manSpeed;
        }
        manmc.gotoAndStop("Walk Front Frame");
    }
        if (manmc.hitTest(treer1_MC))
    {
            trace("leftHit");
            manmc._x += 3;

    }
    else if(upPressed)
    {
        if(manmc.y > 145)
        {
            manmc.y -= manSpeed;
        }
        manmc.gotoAndStop("Walk Back Frame");
    }
}

这是教程说要使用的编码。有什么建议吗?

if (manmc.hitTest(treer1_MC))
        trace("leftHit");
        manmc._x += 3;

1 个答案:

答案 0 :(得分:1)

这只是因为碰撞条件的声明超出了你的“downPress”条件:

 else if(downPressed)
{
    if(manmc.y < 780)
    {
    manmc.y += manSpeed;
    }
    manmc.gotoAndStop("Walk Front Frame");
}
    if (manmc.hitTest(treer1_MC))
{
        trace("leftHit");
        manmc._x += 3;

}

缩进问题:),正确的方法应该是:

 else if(downPressed)
{
    if(manmc.y < 780)
    {
    manmc.y += manSpeed;
    }
    manmc.gotoAndStop("Walk Front Frame");

    if (manmc.hitTest(treer1_MC))
    {
        trace("leftHit");
        manmc._x += 3;
    }
}

我希望它有所帮助;)

编辑:

manmc.hitTest(treer1_MC)

应该是:

<强> manmc.hitTestObject(treer1_MC)

因为函数:hitTest()是一个AS2函数,在AS3上有:hitTestPoint()和hitTestObject();)