How to make housie / bingo game in Flash AS3

93 Views Asked by At

It shows an error when written this script has a package that cannot be nested how can I resolve the problem. If not can anyone give me a new code so that I can try to make the new file? I have this file in previous stack AS3 Bingo ticket generator

but i couldnt understan how to use it.

package  {
import flash.display.Sprite;
import flash.text.TextField;

public class Main extends Sprite{

    public var boards:Array = new Array();
    private static const AMAUNT_BOARDS:uint = 6;
    private static const NUMBER_FIELD_SIZE:uint = 20;

    public function Main() {


        for(var i:uint = 0; i < AMAUNT_BOARDS; i++)
        {
            var numbers:Array = genNumbers();
            numbers = deleteFields(numbers);
            var board:Sprite = getBoard(numbers);
            board.y = NUMBER_FIELD_SIZE * 4 * i;
            boards.push(board);
            addChild(board);
        }           
    }

    //generates a 2 Dimensional Array (3x9) with TextFields
    //populates the according Numbers and returns a board Sprite
    private function getBoard(n:Array):Sprite
    {
        var s:Sprite = new Sprite();
        var a:Array = new Array();
        for(var i:uint = 0; i < 3; i++)
        {
            var b:Array = new Array();
            for(var k:uint = 0; k < 9; k++)
            {
                //create TextFields
                var tf:TextField = new TextField();
                tf.x = k * NUMBER_FIELD_SIZE;
                tf.y = i * NUMBER_FIELD_SIZE;
                tf.border = true;
                tf.width = NUMBER_FIELD_SIZE;
                tf.height = NUMBER_FIELD_SIZE;                  

                if(n[k][i] != 0) // adds the number if the value isn't 0
                    tf.text = n[k][i]; // Note that i am switching k & i because the number Array is 9x3

                b.push(tf);
                s.addChild(tf);
            }           
        }       
        return s;
    }

    // Turns 4 random numbers out of the lines into 0 And returns the Array
    private function deleteFields(a:Array):Array
    {
        for(var i:uint = 0; i < 3; i++)
        {
            var r:RandomPlus = new RandomPlus(8,0);
            for(var k:uint = 0; k < 4; k++)
            {
                    var t:uint = r.getNum();
                    a[t][i] = 0;
            }
        }           
        return a;
    }

    // Generates and returns a 2 Dimensional Array (9x3) with random numbers
    private function genNumbers():Array
    {
        var a:Array = new Array();
        var st:uint = 1;
        var en:uint = 9;
        for(var i:uint = 0; i < 9; i++)
        {
            var line:Array = new Array();
            var ran:RandomPlus = new RandomPlus(en,st);

            if(i == 0)//accounting for the number differnenz at start
                st--;

            if(i==7)//accounting for the number differnenz at end
                en ++;

            st += 10;
            en += 10;

            for(var e:uint = 0; e < 3; e++)
                line[e] = ran.getNum();

            a.push(line);               
        }
        return a;
    }
}

}

1

There are 1 best solutions below

0
Jyreeil On

TL;DR: "Main" in the class definition may have to be changed conflict of already existing name. Extends sprite may need to be changed to extends movieclip. RandomPlus either needs an import you don't have or needs to be made into a symbol and exported to Actionscript.

Ok, so a few things:

If you are really putting the code into the stage code area, then you can't use the private identifier.

Unless RandomPlus is an object that you have defined in the stage it is probably either not a method you can use or you don't have the right imports for it. Look up what imports you need for RandomPlus or if it is an object defined in the stage then you may need to turn it into a symbol or if you have already export it to flash ActionScript I think. To do this you have to check the checkbox in the middle of the symbol creation page. It will always give you the error when you do this, but don't worry it is fine that way.

Third thing is that I never extend sprite in the class definition, I always do extends movieclip(not sure of the capitalization, but you can look that up). You may also be running into an error from using "Main" as the name because it may be a conflict with a name or method already defined in flash in general.

One last thing is for the variable declaration(mostly just for making the code more readable). While it is good to not declare variables as global when you don't have to, I tend to like having most of the variables up at the top, because I like to be able to see most of the declarations in one space. It's not necessary, really just a personal opinion, and I know a lot of experienced coders will say to do what you did, but if you want to use the arrays in multiple functions then sometimes it is easier to just declare them globally rather than having to pass a million things in the function call and then having to figure out later where all the variable declarations are called and where they are being passed as arguments. Again it's more of a coder's choice, but you can also just do whatever you feel more comfortable with rather than just following the already laid out rules by people who have more experience coding.

Another optional fix to the organization might be to name variables something meaningful so as to not forget what they are all doing. I am bad at naming as well, but I think it's really better to name them something better than just a single letter or two.

Another thing that can help is placing trace(code or "text"); in different places to see what's going wrong once you have the compiler happy.