Skip to main content
August 12, 2013
Question

create object from AS3

  • August 12, 2013
  • 1 reply
  • 1847 views

im trying to create objects based on number i input on a textbox

here is the image and code :

// Image

// codes

    

import flash.events.MouseEvent;

var circle:Shape = new Shape();

var i:int;

var j:int;

myBtn.label = "Show";

var a:Number = Number(text1.text);

myBtn.addEventListener (MouseEvent.CLICK,createCircle);

function createCircle (event:MouseEvent):void{

    for (i = 1;i<=a;i++){

        for (j = 1;j<=a;j++){

            circle.graphics.beginFill(0xFF0000, 1);

            circle.graphics.lineStyle(5, 0x000000);

            circle.graphics.drawCircle(j*50 + j*20, i*50 + i*20, 25);

            circle.graphics.endFill();

            addChild(circle);

        }

    }   

   

}

can someone help me ??

thanks

This topic has been closed for replies.

1 reply

Ned Murphy
Legend
August 12, 2013

What problem are you having?  As you have it, your input text field needs to have a value in it before you run the program or else it will not be of any use unless you extract the value of it inside the button's function.  Your double loops will cause the number of circles to be the squared value of the input text value.

August 12, 2013

so i cant create the circle based on what i input on the textbox when i run the program ??

the double loop i create is to set the circle i create not to only add to the right or below

Ned Murphy
Legend
August 12, 2013

You can have it create the circles based on the number you enter, but at the moment it will be the number you have entered before you run the program because you assign it immediately when you start the program via the line...

      var a:Number = Number(text1.text);

So have a value pre-entered and you will see the result I mentioned regarding the square of the value you entered.

If you want to enter the value after you start the program and have it use that value then you need to assign the value of a inside your CLICK event handler function before you start the loops...

      var a:int;  // do not assign the value here

     function createCircle (event:MouseEvent):void{

           a = int(text1.text);    // assign it here

           for (i = 1;i<=a;i++){

Since you are using int values for the loops it is better to use an int value for that 'a' variable'