Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 Platformer游戏命中测试_Actionscript 3_Flash_Actionscript_Flash Cs6 - Fatal编程技术网

Actionscript 3 Platformer游戏命中测试

Actionscript 3 Platformer游戏命中测试,actionscript-3,flash,actionscript,flash-cs6,Actionscript 3,Flash,Actionscript,Flash Cs6,好的,我在舞台上有一个物体在“世界”电影唇上移动。我正努力让你在正确的方向上前进。如果称为hitD的移动电影剪辑(“dude”)中的电影剪辑与世界上的墙壁发生碰撞,则该剪辑停止前进 屏幕截图,如果可能有帮助的话。 选定的一般舞台都德对象 除了球,世界就是一切 希特 如果任何人有任何方法可以修改这些碰撞物理,因为我当前的代码非常粗略,欢迎所有建议和想法 var started:Boolean; const NUMLEVELS = 3; var status:String; stage.focus

好的,我在舞台上有一个物体在“世界”电影唇上移动。我正努力让你在正确的方向上前进。如果称为hitD的移动电影剪辑(“dude”)中的电影剪辑与世界上的墙壁发生碰撞,则该剪辑停止前进

屏幕截图,如果可能有帮助的话。 选定的一般舞台都德对象 除了球,世界就是一切

希特

如果任何人有任何方法可以修改这些碰撞物理,因为我当前的代码非常粗略,欢迎所有建议和想法

var started:Boolean;
const NUMLEVELS = 3;
var status:String;
stage.focus = stage;
if (! started)
{// Only ever do this once!
    status = "falling";
    started = true;
    var speedX:Number = 5;
    var speedY:Number = 0;
    var topSpeedY:Number = 50;
    var start_x:Number = dude.x;
    var start_y:Number = dude.y;
    var keysDown:Object = new Object();
    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
    stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
    stage.addEventListener( Event.DEACTIVATE, appDeactivate );
    dude.addEventListener(Event.ENTER_FRAME, moveDude);
    var W:Number = 15;
    var snows:Array = new Array();

}
for (var b:int = 0; b < 50; b++)
{
    var snow:Snow = new Snow();

    snows.push(snow);
    addChild(snow);
}


function cleanup()
{
    stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
    stage.removeEventListener(KeyboardEvent.KEY_UP, keyReleased);
    stage.removeEventListener( Event.DEACTIVATE, appDeactivate );
    dude.removeEventListener(Event.ENTER_FRAME, moveDude);
}
function keyIsDown(key:uint):Boolean
{
    return Boolean(key in keysDown);
}
function keyPressed(e:KeyboardEvent):void
{
    keysDown[e.keyCode] = true;
}
function keyReleased(e:KeyboardEvent):void
{
    delete keysDown[e.keyCode];
}
function appDeactivate( event:Event ):void
{
    // Get rid of all keypress info when app loses focus
    keysDown=new Object();
}

function moveDude(e:Event):void
{

    var obj:Object = e.target; //setting dude as object
    // for now, if you get off the top of the screen you win
    if (obj.y < 0)
    {
        cleanup();
        nextScene();
        return;
    }
    // if character dies, restart
    if (obj.y > stage.stageHeight + 100)
    {
        gotoAndStop(1);
        obj.x = start_x;
        obj.y = start_y;
    }
    if (death!=null)
    {
        if (obj.hitTestObject(death))
        {
            trace("Dead");
        }
    }

    if (status=="falling")
    {
        speedY++;
        if (speedY>topSpeedY)
        {
            speedY = topSpeedY;
        }
        for (i = 0; i<2*speedY; i++)
        {
            obj.y++;
            if (world.hitTestPoint(obj.x - obj.width / 2,obj.y,true) || world.hitTestPoint(obj.x + obj.width / 2,obj.y,true))
            {
                status = "ground";
                break;
            }
        }

    }
    else if (status == "jumping")
    {
        speedY--;
        for (i = 0; i<2*speedY; i++)
        {
            obj.y--;
            if (world.hitTestPoint(obj.x - obj.width / 2,obj.y - obj.height,true) || world.hitTestPoint(obj.x + obj.width / 2,obj.y - obj.height,true))
            {
                speedY = 0;
                break;
            }
        }

        if (speedY==0)
        {
            status = "falling";
        }
    }
    else if (status == "ground")
    {
        if (! world.hitTestPoint(obj.x - 8,obj.y,true) && ! world.hitTestPoint(obj.x + 8,obj.y + 4,true))
        {
            speedY = 0;
            status = "falling";
        }

        if (keyIsDown(Keyboard.UP))
        {
            status = "jumping";
            speedY = 10;
        }
    }
    if (keyIsDown(Keyboard.DOWN)&&status=="ground")
    {
        dude.gotoAndStop("duck");
    }
    else
    {
        if (keyIsDown(Keyboard.SHIFT))
        {
            speedX = 10;
        }
        else
        {
            speedX = 5;
        }
        if (keyIsDown(Keyboard.LEFT))
        {
            for (i = 0; i<speedX; i++)
            {
                obj.x--;
                dude.ball.rotation--; //dude.ball is a movieclip similar to dude.hitD, it spins when you move.
                if (world.hitTestPoint(obj.x - obj.width / 2 + 4,obj.y - 8,true) || world.hitTestPoint(obj.x - obj.width / 2,obj.y - obj.height + 8,true))  
                {
                    dude.ball.rotation++;

                    obj.x++;
                    break;
                }
            }

        }
        else if (keyIsDown(Keyboard.RIGHT))
        {
            //dude.gotoAndStop("right");
            //obj.scaleX = 1;
            for (i = 0; i<speedX; i++)
            {
                obj.x++;
                dude.ball.rotation++;
                // The number in obj.y-4 affects the climbing ability
                if (status == "ground")
                {
                    //dude.height+= 0.1;
                    //dude.width += 0.1;
                }//so here I'm checking if it hits the lower corner or top right corner or hitD
                if (world.hitTestPoint(dude.hitD.x + obj.hitD.width/2 , obj.hitD.y,true) || world.hitTestPoint(obj.hitD.x + obj.hitD.width/2,obj.hitD.y - obj.hitD.height ,true))
                //if (world.hitTestObject(obj))
                {

                    dude.ball.rotation--;
                    obj.x--;
                    break;
                }
            }
        }
        dude.gotoAndStop(1);

    }
    while (status == "ground" && (world.hitTestPoint(obj.x-8, obj.y-1, true) || world.hitTestPoint(obj.x+8, obj.y-1, true)))
    {
        obj.y--;
    }

    const BORDER = 50;
    var diff:int;
    // Check right border:
    diff = obj.x + BORDER - stage.stageWidth;
    if (diff>0 && world.x>=stage.stageWidth-world.width)
    {
        obj.x -=  diff;
        world.x -=  diff;
        background1.x -=  diff;
        if (death != null)
        {
            death.x -=  diff;
        }
    }
    // Check left border:
    diff = obj.x - BORDER;
    if (diff<0 && world.x<=0)
    {
        obj.x -=  diff;
        world.x -=  diff;
        background1.x -=  diff;
        if (death != null)
        {
            death.x -=  diff;
        }
    }
    // Check bottom border:
    diff = obj.y + BORDER - stage.stageHeight;
    if (diff>0)
    {
        obj.y -=  diff;
        world.y -=  diff;
        background1.y -=  diff;
        if (death != null)
        {
            death.y -=  diff;
        }
    }
    // Check top border:
    diff = obj.y - BORDER;
    if (diff<0)
    {
        obj.y -=  diff;
        world.y -=  diff;
        background1.y -=  diff;
        if (death != null)
        {
            death.y -=  diff;
        }
    }
    if (obj.x > stage.stageWidth - 25)
    {
        if (currentFrame<NUMLEVELS)
        {
            gotoAndStop(currentFrame+1);
            obj.x = 25;
        }
        else
        {
            obj.x = stage.stageWidth - 25;
        }
    }
    else if (obj.x<25)
    {
        if (currentFrame>1)
        {
            gotoAndStop(currentFrame-1);
            obj.x = stage.stageWidth - 25;
        }
        else
        {
            obj.x = 25;
        }
    }

}
var开始:布尔值;
常数NUMLEVELS=3;
var状态:字符串;
阶段。焦点=阶段;
如果(!已启动)
{//只做一次!
状态=“下降”;
开始=真;
var speedX:Number=5;
变量:数字=0;
var topSpeedY:Number=50;
var start_x:Number=dude.x;
var start_y:Number=dude.y;
var keysDown:Object=新对象();
stage.addEventListener(KeyboardEvent.KEY_向下,按键);
stage.addEventListener(KeyboardEvent.KEY\u UP,keyReleased);
stage.addEventListener(Event.DEACTIVATE、appDeactivate);
dude.addEventListener(Event.ENTER_FRAME,moveDude);
变量W:数值=15;
var snows:Array=newarray();
}
对于(变量b:int=0;b<50;b++)
{
变量雪:雪=新雪();
雪。推(雪);
addChild(雪);
}
函数清理()
{
stage.removeEventListener(KeyboardEvent.KEY_向下,按键);
stage.removeEventListener(KeyboardEvent.KEY_UP,keyReleased);
stage.removeEventListener(Event.DEACTIVATE,appDeactivate);
dude.removeEventListener(Event.ENTER_FRAME,moveDude);
}
函数keyIsDown(键:uint):布尔值
{
返回布尔值(输入keysDown);
}
功能键按下(e:键盘事件):无效
{
keysDown[e.keyCode]=真;
}
功能键释放(e:键盘事件):无效
{
删除keysDown[e.keyCode];
}
函数appDeactivate(事件:事件):无效
{
//当应用程序失去焦点时,清除所有按键信息
keysDown=新对象();
}
函数moveDude(e:事件):无效
{
var obj:Object=e.target;//将dude设置为对象
//现在,如果你从屏幕顶部跳下来,你就赢了
如果(对象y<0)
{
清理();
nextScene();
返回;
}
//如果角色死亡,请重新启动
如果(目标y>阶段高度+100)
{
gotoAndStop(1);
obj.x=开始x;
obj.y=开始时间;
}
if(死亡!=null)
{
if(对象hitTestObject(死亡))
{
痕迹(“死亡”);
}
}
如果(状态=“下降”)
{
快速++;
如果(速度>最高速度)
{
速度=极快;
}

对于(i=0;i对于玩家物理,更清晰的方法是创建一个中心ENTER_FRAME处理函数,该函数仅用于计算要应用于玩家的转换

您仍然可以从外部获取信息,但您只需在那里处理最终输出。否则,这可能会有问题,尤其是当事情变得更加复杂时,以及当您想要添加更多功能时。(例如,想象一下,在未来,你想在地面上增加风扇,让空气沸腾,而球员在地面上必须站起来。你有很多需要改变的地方,可能还有很多问题

对于碰撞检测,此函数将向您提供以下信息:播放器无法使用变量“UpColl”、“DownColl”、“Right Coll”和“LeftColl”向哪个方向移动。 然后,您可以从将转换应用于播放器的主函数中引用此信息。下面我将给出示例

function collEveryFrame(event:Event):void
{
    // capture player positions
    player_x = WORLD.player.x;
    player_y = WORLD.player.y;

    // the movie clip object where you store your solid objects. Remember, ALL MovieClip objets inside this MovieClip will taken as solid objects in your game, and regardless their shape, their boundingBox will be taken as collison. So they will all be square.
    collContainer = WORLD.cW;
    // your player's collision object
    playerColl = WORLD.player;

    // RIGHT SQUARE COLLISION DETECTION
    for (var i:int; i < collContainer.numChildren; i++)
    {
        // Check if any collision object colliding with player
        if (playerColl.hitTestObject(collContainer.getChildAt(i)))
        {
        // One collision detected. Check 'from which side' does the player colliding with the object;
        if (collContainer.getChildAt(i).y > playerColl.y + playerColl.height - p1MoveSpeed)
        {
            playerColl.y = collContainer.getChildAt(i).y - playerColl.height;
            DownColl = true;
        }
        else if (collContainer.getChildAt(i).y + collContainer.getChildAt(i).height < playerColl.y + p1MoveSpeed)
        {
            playerColl.y = collContainer.getChildAt(i).y + collContainer.getChildAt(i).height;
            UpColl = true;
        }
        else if (collContainer.getChildAt(i).x + collContainer.getChildAt(i).width < playerColl.x + p1MoveSpeed)
        {
            playerColl.x =  +  collContainer.getChildAt(i).x + collContainer.getChildAt(i).width;
            LeftColl = true;
        }
        else if (collContainer.getChildAt(i).x > playerColl.x + playerColl.width - p1MoveSpeed)
        {
            playerColl.x =  +  collContainer.getChildAt(i).x - playerColl.width;
            RightColl = true;
        }
        }
    }
    // RIGHT SQUARE COLLISION DETECTION [End]
}
这样,所有关于物理的东西都很容易实现。 例如,在计算向下移动时,添加重力和跳跃等

function playerMovement(event:Event):void
{
    // (apply this for all sides)
    // if nothing keeps player from going right; 
    if (! RightColl)
    {
        // Apply everything currently pushing the player to right
        if (keyIsDown(Keyboard.RIGHT))
        {
            movement_Right = 15;
        }else{
            movement_Right = 0;
        }
        // example fictional wind function returns wind speed
        windSpeed = getWindSpeed();

        player.x +=  movement_Right + windSpeed + etc + etc;
        // say windSpeed is -5 (Wind is coming from right, so pushing the player to left)
        // so if user pressing right arrow key, player will move to right 10px for every frame, else 5px to left, etc.
    }
}