【问题标题】:Simple boat in ActionScriptActionScript 中的简单船
【发布时间】:2011-04-26 15:24:13
【问题描述】:

我正在 Actionscript 中创建一个简单的可控船,并让它前进,左转和右转,但无法让它转身。我希望船朝着它所指的方向前进。

function moveBoat(event:Event):void
{
    if(rightKeyIsDown)
    {
        player_mc.x += speed;
        player_mc.rotationZ += speed;
    }
    if(leftKeyIsDown)
    {
        player_mc.x -= speed;
        player_mc.rotationZ -= speed;

    }
    if(upKeyIsDown)
    {
        player_mc.y -= speed;
    }
}

提前感谢任何可以告诉我我做错了什么的人。

编辑:

function moveBoat(event:Event):void
{
    if(rightKeyIsDown)
    {
        player_mc.rotationZ += turnFactor;
    }
    if(leftKeyIsDown)
    {
        player_mc.rotationZ -= turnFactor;
    }
    if(upKeyIsDown)
    {
        player_mc.x += speed * Math.cos(player_mc.rotationZ * Math.PI / 180);
        player_mc.y -= speed * Math.sin(player_mc.rotationZ * Math.PI / 180);
    }
}

编辑

function moveBoat(event:Event):void
{
    if(rightKeyIsDown)
    {
        player_mc.rotation += turnFactor;
    }
    if(leftKeyIsDown)
    {
        player_mc.rotation -= turnFactor;
    }
    if( upKeyIsDown )
    {
        // convert our rotation to radians first
        var rads:Number = player_mc.rotation * ( Math.PI / 180.0 );
        player_mc.x += speed * Math.cos( rads );
        player_mc.y += speed * Math.sin( rads );
    }
}

【问题讨论】:

  • 而不是player_mc.y -= ...,设置player_mc.y += ... 就可以了。就像我在回答中提到的那样......

标签: flash actionscript


【解决方案1】:

尝试类似:

function moveBoat(event:Event):void
{
    if(rightKeyIsDown)
    {
        player_mc.rotationZ -= turnFactor;
    }
    if(leftKeyIsDown)
    {
        player_mc.rotationZ += turnFactor;
    }
    if(upKeyIsDown)
    {
        player_mc.x += speed * Math.cos(player_mc.rotationZ * Math.PI / 180);
        player_mc.y -= speed * Math.sin(player_mc.rotationZ * Math.PI / 180);
    }
}

我现在无法测试代码,但这应该足以解释这个想法。我所做的不同之处在于使用左键和右键来操纵船(通过改变它的角度)并在点击向上键时将它移动到它所指向的方向。

移动飞船时,不能简单地将其在 X 和 Y 轴上移动一定量,还需要考虑飞船的方向。为此,请使用正弦和余弦函数。

【讨论】:

  • 谢谢。越来越近,但现在按下向上时,船只会向左或向右移动,具体取决于它旋转了多远。
  • rotationZ 属性使用度数,数学函数使用弧度。调用数学函数时,请务必将度数转换为弧度 (player_mc.rotationZ * Math.PI / 180)。如果这不起作用,请发布您使用的确切代码。
  • 进行了一些编辑,但向上键仍然在 x 轴上移动船
  • 你有一个错字(写了“Math(”而不是“Math.cos(”)。
  • 那个错字只是在这里,我在我的代码中修复了它但忘记在这里修复它。
【解决方案2】:

使用一些简单的三角函数来计算对象在 X 轴和 Y 轴上的移动量。创建一个直角三角形,船为 A 点,速度为 h,船的方向为角度 A:

现在使用正弦和余弦计算边 a 和 b 的长度。这就是在 X 和 Y 轴上移动的距离。

【讨论】:

    【解决方案3】:

    TokPhobia 几乎是对的

    private function _moveBoat(event:Event):void
    {
        if( rightKeyIsDown )
            this.m_player.rotation += turnFactor;
    
        if ( leftKeyIsDown )
            this.m_player.rotation -= turnFactor;
    
        if( upKeyIsDown )
        {
            // convert our rotation to radians first
            var rads:Number = this.m_player.rotation * ( Math.PI / 180.0 );
            this.m_player.x += speed * Math.cos( rads );
            this.m_player.y += speed * Math.sin( rads );
        }
    }
    

    顺便说一句,除非您在 3D 中执行此操作,否则 rotation 属性可以正常工作,您不需要使用 rotationZ

    【讨论】:

    • 它确实有效,你只需要改变你的船图像的旋转。 Flash 朝右旋转 0,而您的船设计为朝上(Flash 旋转为 -90)。只需将您的船舶图像编辑到正确的位置,您就可以了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多