-1

我正在制作一个随机生成的地板,但是当影片剪辑在其上行走时,它的行为似乎就像一块巨大的地板,影片剪辑在其上方行走,而不是跟随图像,这是代码:

import flash.display.Sprite;
import flash.display.MovieClip;
import flash.events.Event;

//variables

var h:Number = 360;
var floor:MovieClip=new MovieClip;
var guy:MovieClip=new MovieClip;
var gravity=10;

guy.graphics.beginFill(000000);
guy.graphics.drawRect(100,100,15,25);
guy.graphics.endFill();
addChild(guy);//guy creation


//floor generation
for (var i:int=0; i<600; i++)
{

    if (h==360)
    {
        if (Math.random() <= 0.75)
        {
            floor.graphics.beginFill(000000);
            floor.graphics.drawRect(i,h,40,40);
            floor.graphics.endFill();
            addChild(floor);
            i +=  39;
        }
        else
        {
            i +=  39;
        }
        if (i>550)
        {
            h = 320;
            i = 0;
        }
    }
    if (h==320)
    {
        if (Math.random() <= 0.75)
        {
            floor.graphics.beginFill(000000);
            floor.graphics.drawRect(i,h,40,40);
            floor.graphics.endFill();
            addChild(floor);
            i +=  39;
        }
        else
        {
            i +=  39;
        }
    }
}


//listeners doesnt matter
addEventListener(Event.ENTER_FRAME,misc);
addEventListener(KeyboardEvent.KEY_UP,checkup);
addEventListener(KeyboardEvent.KEY_DOWN,checkdown);


//functions

var leftarrow:Boolean=false;
var rightarrow:Boolean=false;

function checkdown(a:KeyboardEvent)
{
    if(a.keyCode==37)leftarrow=true;
    if(a.keyCode==39)rightarrow=true;
}

function checkup(a:KeyboardEvent)
{
    if(a.keyCode==37)leftarrow=false;
    if(a.keyCode==39)rightarrow=false;
}

function misc(a:Event)
{
    guy.y+=gravity;

    if(guy.hitTestObject(floor))
    {
        gravity=0;
    }else{gravity=10;}

    if(leftarrow)guy.x-=5;
    if(rightarrow)guy.x+=5;
}

提前致谢。

4

1 回答 1

1

该函数hitTestObject评估两个实例的边界框。DisplayObject您的角色滑过地板上的孔和坑,因为边界框封装了这些孔和坑。

检测凹面形状上的碰撞很困难。您可能会更轻松地用几个凸形(而不是单个凹形)构建地板并hitTestObject在每个上使用。

var blocks:Array = [ ];

for (var i:int = 0; i < 10; i++)
{
    // Add a convex floor block to blocks.
}

...

gravity = 10

for each (var block:DisplayObject in blocks)
{
    if (guy.hitTestObject(block))
    {
        gravity = 0;
        break;
    }
}
于 2012-10-23T22:10:35.593 回答