Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

create object from AS3

Guest
Aug 12, 2013 Aug 12, 2013

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

here is the image and code :

// Image

Untitled.png

// 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

TOPICS
ActionScript
1.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 12, 2013 Aug 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Aug 12, 2013 Aug 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 12, 2013 Aug 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'

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Aug 12, 2013 Aug 12, 2013

its worked alr

thanks

could i ask how to make the circle appear 1 by 1 ??

and can i give effect to the circle create with that code ??

like giving fade out effect or tween ??

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 12, 2013 Aug 12, 2013

To make them appear one after another such that you can see that happening you can use the Timer class.  You will need to change from using 'for' loops to using programmed loop simulation... basically using counting variables to control the sequencing.

You can use the built-in Tween class or a third party tweening engine such as TweenLite for transition effects.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Aug 12, 2013 Aug 12, 2013

can i ask other subject here ??

i can't figure out what wrong with code

// --- code :

var a:Number;

var Luas:Number;

var Kel:Number;

hitLuas.addEventListener (MouseEvent.CLICK, hitungLuas);

function hitungLuas (event:MouseEvent):void{

    a = Number(sisi.text);

    Luas = a * a;

    var myString4:String = "Luas Persegi =  " + a + " * " + a + "\n\nLuas Persegi = " + Luas + " " ;

   

    var myArray4:Array = myString4.split("");

    addEventListener(Event.ENTER_FRAME,frameHandler4);

   

    function frameHandler4(event:Event):void

    {

        if (myArray4.length>0)

        {

            mytext4.appendText(myArray4.shift());

            stage.frameRate = 10;

        }

        else

        {

            removeEventListener(Event.ENTER_FRAME,frameHandler4);

            stage.frameRate = 5;

        }

    }

}

it keep adding the text to the previous text rather than delete the previous one and add a new one

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 12, 2013 Aug 12, 2013

Probably because you are using the appendText() method, which adds to the text that is already in the textfield, instead of assigning the text to the textfield ( mytext4.text = "new text"; ).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Aug 13, 2013 Aug 13, 2013

umm

about applying tween to object i create in as3, i still cant figure it out

// the code i make until now is :

import flash.events.MouseEvent;

import flash.ui.Mouse;

var circle:Shape = new Shape();

var i:int = 1;

var a:int;

myBtn.label = "Show";

myBtn.addEventListener (MouseEvent.CLICK,timing);

function timing (event:MouseEvent):void{

    a = int(text1.text);

    var myTimer:Timer = new Timer(1000,a); // 1 second

    myTimer.addEventListener(TimerEvent.TIMER,  createCircle);

    myTimer.start();

}

function createCircle (event:Event):void{

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

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

    circle.graphics.drawCircle(200, 100 + i*60 , 25);

    circle.graphics.endFill();

    addChild(circle);

    i++;

}

i try searching info about tween class , is it only can apply to object created in a stage ??

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 13, 2013 Aug 13, 2013

A Tween can be applied to an object.  The only object I see in your code is the Shape called circle, and there is only one of them.  If you want each drawn circle to transition separately you will need to add each one to a unique object.  Try creating a movieclip for each in your createCircle function, assigning the graphics to that instead of circle, and apply the Tween to the movieclip.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Aug 13, 2013 Aug 13, 2013

i can apply the mc with tween already

but the circle create tweening when other circle create which i want it to stop when it finish and then the other circle tween

i try using array to the mc but i think i mess up in the code

can some1 help me ??

// here is the code

import flash.events.MouseEvent;

import flash.ui.Mouse;

import fl.transitions.Tween;

import fl.transitions.easing.Strong;

import fl.transitions.TweenEvent;

var i:int = 1;

var a:int;

var myMC:MovieClip = new MovieClip();

var myTween:Tween;

var myArray:Array = new Array();

myMC.name = "mc1";

myBtn.label = "Show";

myBtn.addEventListener (MouseEvent.CLICK,showCircle);

function timing (event:MouseEvent):void{

    a = int(text1.text);

    var myTimer:Timer = new Timer(500,a);

    myTimer.addEventListener(TimerEvent.TIMER,  createCircle);

    myTimer.start();

}

function createCircle (event:Event):void{

    a = int(text1.text);

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

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

    myMC.graphics.drawCircle(200, 150 + i*50 , 25);

    myMC.graphics.endFill();

    myArray.push(myMC);

    i++;

}

function showCircle (e:MouseEvent):void{

        a = int(text1.text);

        var myTimer:Timer = new Timer(500,a);

        myTimer.addEventListener(TimerEvent.TIMER,  createCircle);

        myTimer.start();

        addChild (myMC);

        myTween = new Tween(myMC, "x", Strong.easeOut, 50, 400, 2, true);

}

***

and the circle didnot restart when i input a new number to the inputbox

for example when i first input 3 in the textbox, it create up to 3 circle

when i change the number to 4 , the circle add 4 to the previous 3 circle rather than create new 4 circle

how do i solve this ??

thanks

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 13, 2013 Aug 13, 2013

You did not do what I described.  You should create a new movieclip to contain each new circle graphic and create a new tween for each movieclip.  This can all be done inside the createCircle function.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Aug 13, 2013 Aug 13, 2013

"You should create a new movieclip to contain each new circle graphic"

i dont know how to do it

is it using array function ??

and about

"and the circle didnot restart when i input a new number to the inputbox

for example when i first input 3 in the textbox, it create up to 3 circle

when i change the number to 4 , the circle add 4 to the previous 3 circle rather than create new 4 circle"

do i use removeChild to remove the circle when i enter a new number ??

where can i put it ??

here is my code until now

/// code

import flash.events.MouseEvent;

import flash.ui.Mouse;

import fl.transitions.Tween;

import fl.transitions.easing.Strong;

import fl.transitions.TweenEvent;

var i:int = 1;

var a:int;

var myMC:MovieClip = new MovieClip();

var myTween:Tween;

var myArray:Array = new Array();

myBtn.addEventListener (MouseEvent.CLICK,showCircle);

function showCircle (e:MouseEvent):void{

        a = int(text1.text);

        var myTimer:Timer = new Timer(500,a);

        myTimer.addEventListener(TimerEvent.TIMER,  createCircle);

        myTimer.start();

}

function createCircle (event:Event):void{

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

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

    myMC.graphics.drawCircle(200, 150 + i*50 , 25);

    myMC.graphics.endFill();

    addChild(myMC);

    myTween = new Tween(myMC, "x", Strong.easeOut, 50, 400, 2, true);

    i++;

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 14, 2013 Aug 14, 2013
LATEST

You create a new mc for each circle the same way you created myMC, just do it inside the createCircle function.  THen you apply the graphics to these new mc's instead of myMC.

If you add these new circles as children of myMC, then you just need to removeChild them from myMC to clear them when you want to draw a new set.

If you want to have the new set start from the beginning spot then you need to reset any variables you use to locate them back to their original value(s).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines