闪电下降和捕捉游戏

问题描述 投票:0回答:1

我正在做一个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轴也会随着计算而不断变化。任何解决方案?

actionscript-3 flash
1个回答
2
投票

有不同的方法来处理这个问题。我认为Melon1,Honey1,...是你自己的课程的实例。添加一个位于每个属性的公共属性,例如:

public var positioned:Boolean=false;

目前,您的ItemType函数只根据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函数以将locate属性考虑在内 - 因此,如果位置为false,则将其随机化:

        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所暗示的那样,我想这个函数应该只生成一个新对象并初始化它的位置。不幸的是,你似乎也滥用这个功能来完成主游戏循环。我建议将其拆分为两个独立的功能。

© www.soinside.com 2019 - 2024. All rights reserved.