游戏旋转碰撞问题

时间:2013-12-19 16:38:35

标签: actionscript-3 flash rotation maze

我试图制作一个轻微扭曲的基本迷宫游戏。在设定的时间间隔(由计时器事件设定),迷宫旋转90%。我遇到的问题是关于hitTestPoint。击中测试在迷宫旋转之前工作并且在完全旋转360%之后工作但在90%,180%和270%旋转点期间停止工作。在AS3中,我已经耗尽了我所有的知识(限制为5个月的AS3程序员)以解决这个问题并且我的智慧结束了。

迷宫在一个容器中并且容器在旋转,这使得迷宫实际上具有二次移动枢轴点,该点继续跟随玩家在迷宫周围。另外,容器在按键上移动而不是播放器。

任何人都可以通过解释造成问题的原因,如何解决问题以及如果可能的话向我展示我应该使用的代码示例来打扰我。

这是我到目前为止所拥有的。

stop();

import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.sensors.Accelerometer;

var speed:Number = 5;

var northSpeed = speed;
var southSpeed = speed;
var eastSpeed = speed;
var westSpeed = speed;

var upPressed:Boolean = false;
var downPressed:Boolean = false;
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
var Orientation:int = 0;

var count:int = 0;
var timer:Timer = new Timer(1000,60);
var time = 60;

player.addEventListener(Event.ENTER_FRAME, MovePlayer);
containBox.maze.addEventListener(Event.ENTER_FRAME, hitWalls);
stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, KeyDepressed);
containBox.maze.addEventListener(Event.ENTER_FRAME, spin);

timer.start();
timer.addEventListener(TimerEvent.TIMER, timerHandle);

function timerHandle(e:TimerEvent):void
{
    txt_time.text = time;
    time--;
}

function hitWalls(event:Event):void
{
    if (upPressed==true && containBox.maze.hitTestPoint(player.x,player.y,true))
    {
        northSpeed = 0;
        player.y = player.y+=2;
    }
    else
    {
        northSpeed = speed;
    }
}


function spin(event:Event):void
{
    if (time <= 0)
    {
        txt_time.text = "TIMES UP!";
    }
    if (time <= 54)
    {
        containBox.rotation = 90;
        Orientation = 1;
    }
    if (time <= 50)
    {
        containBox.rotation = 180;
        Orientation = 2;
    }
        if (time <= 48)
    {
        containBox.rotation = 270;
        Orientation = 3;
    }
        if (time <= 46)
    {
        containBox.rotation = 0;
        Orientation = 0;
    }

}

function MovePlayer(event:Event):void
{
    if (Orientation == 0)
    {
        if (upPressed)
        {
            containBox.maze.y +=  northSpeed;
        }
        if (downPressed)
        {
            containBox.maze.y -=  southSpeed;
        }
        if (leftPressed)
        {
            containBox.maze.x +=  westSpeed;
        }
        if (rightPressed)
        {
            containBox.maze.x -=  eastSpeed;
        }
    }
    else if (Orientation == 1)
    {
        if (upPressed)
        {
            containBox.maze.x +=  5;
        }
        if (downPressed)
        {
            containBox.maze.x -=  5;
        }
        if (leftPressed)
        {
            containBox.maze.y -=  5;
        }
        if (rightPressed)
        {
            containBox.maze.y +=  5;
        }
    }
    else if (Orientation == 2)
    {
        if (upPressed)
        {
            containBox.maze.y -=  5;
        }
        if (downPressed)
        {
            containBox.maze.y +=  5;
        }
        if (leftPressed)
        {
            containBox.maze.x -=  5;
        }
        if (rightPressed)
        {
            containBox.maze.x +=  5;
        }
    }
    else if (Orientation == 3)
    {
        if (upPressed)
        {
            containBox.maze.y +=  5;
        }
        if (downPressed)
        {
            containBox.maze.y -=  5;
        }
        if (leftPressed)
        {
            containBox.maze.x +=  5;
        }
        if (rightPressed)
        {
            containBox.maze.x -=  5;
        }
    }
}

function KeyPressed(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 KeyDepressed(event:KeyboardEvent):void
{
    switch (event.keyCode)
    {
        case Keyboard.UP :
            {
                upPressed = false;
                break;
            };
        case Keyboard.DOWN :
            {
                downPressed = false;
                break;
            };
        case Keyboard.LEFT :
            {
                leftPressed = false;
                break;
            };
        case Keyboard.RIGHT :
            {
                rightPressed = false;
                break;
        }
    }
}

非常感谢, 里斯。

1 个答案:

答案 0 :(得分:0)

您需要进行2D坐标系转换才能进行适当的比较。请参阅,播放器基本上位于固定点上,因此(player.x, player.y)是固定的,hitTestPoint()使用作为this对象的本地提供的坐标,在您的情况下是迷宫。要获得迷宫系统中玩家的坐标,首先需要获得全局位置,然后导出本地位置,分别有localToGlobal()globalToLocal()的函数。

function hitWalls(event:Event):void
{
    var p:Point=new Point(player.x,player.y);
    var dp:Point=containBox.maze.globalToLocal(player.parent.localToGlobal(p));
    // now test vs dp instead of player.
    if (upPressed==true && containBox.maze.hitTestPoint(dp.x,dp.y,true))
    {
        northSpeed = 0;
        player.y = player.y+=2;
    }
    else
    {
        northSpeed = speed;
    }
}