Flash drop and catch 游戏

Flash drop and catch game

我正在做一个 Flash 小游戏,角色需要接住从顶部掉落的物品。 我面临的问题是对随机选择的数组中的 6 个项目使用计时器,并使用随机 x 轴下拉。

var PointFood:Array = [Melon1,Honey1,Worm1,Clock1,Poison1,Sling1];
//this is the array of items to drop
time = new Timer(60);
//No idea the timer is with frame or second
time.addEventListener(TimerEvent.TIMER,GenerateItem);
time.start();
function GenerateItem(event:TimerEvent):void
{
axis = Math.round(Math.random()*709.7)+44.45;
//this is the random x axis with my scene width
PointFood[i].x = axis;
PointFood[i].y = 0;
    if(PointFood[i].y < 600.95)
    {
      PointFood[i].y += 10;
    }
}
//here assign the x and y axis
function ItemType(e:Event):Number
{
    i = Math.round(Math.random()*5);
    return i;
}
//this is the random for item in array

然而,一旦计算完成,x 轴将不断变化。即使是屏幕上已经存在的项目,它们的 x 轴也会随着计算不断变化。 对此有什么解决方案吗?

有不同的处理方法。 我假设 Melon1Honey1、... 是您自己的 类 的实例。 为每个添加一个public 属性 positioned,如:

public var positioned:Boolean=false;

目前,您的 ItemType 函数只是 returns 一个基于 PointFood 数组中对象数量的随机整数. 让我们集成检查随机对象是否已经定位使用它是新添加的定位 属性:

function ItemType(e:Event):Number
        {
            var found:Boolean;

            do
            {
                found = true;
                i = Math.round(Math.random() * 5);
                if (PointFood[i].positioned)
                {
                    found = false;
                }
            } while (!found);

            return i;
        }

最后修改 GenerateItem 函数以将 positioned 属性 考虑在内 - 因此如果它是 :

        function GenerateItem(event:TimerEvent):void
        {
            if (PointFood[i].positioned == false)
            {
            axis = Math.round(Math.random() * 709.7) + 44.45;
//this is the random x axis with my scene width
                PointFood[i].positioned = true;
                PointFood[i].x = axis;
                PointFood[i].y = 0;
            }
                if (PointFood[i].y < 600.95)
                {
                    PointFood[i].y += 10;
                }
        }

旁注:

time = new Timer(60);

表示您的计时器每 60 毫秒触发一次 - 这是预期的行为吗? 您的代码也可能存在一些逻辑问题。正如名称 GenerateItem 所暗示的那样,我想这个函数应该只是生成一个新对象并初始化它的位置。不幸的是,您似乎也在滥用此功能来执行主游戏循环。我建议将它分成两个单独的函数。