Search code examples
actionscript-3flash

Flash drop and catch game


I am doing a flash mini game that a character need to catch item drop down from top. The problems i am facing are using timer for those 6 items in an array randomly chose and drop down with random x-axis.

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

However the x-axis will be keep changing once the calculation done. Even the items that already exist on screen, their x-axis will also keep changing with the calculation. Any Solution for this?


Solution

  • There are different ways of dealing with this. I assume Melon1, Honey1, ... are instances of your own classes. Add a public property positioned to each of them, like:

    public var positioned:Boolean=false;
    

    At the moment your ItemType function just returns a random integer based on the number of objects inside the PointFood array. Let's integrate a check if the random object is already positioned using it's freshly added positioned property:

    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;
            }
    

    Finally modify the GenerateItem function to take the positioned property into account - thus just randomize the position if it's 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;
                    }
            }
    

    As a side note:

    time = new Timer(60);
    

    means your timer fires every 60 milliseconds - is that the expected behaviour? Also there might be a little logic problem with your code. As the name GenerateItem implies, I guess this function should just spawn a new object and initialize it's position. Unfortunately it looks like you're abusing this function to do your main game loop as well. I'd recommend splitting this up into two separate functions.