Thursday, May 10, 2012

[Action Script] Why I can't remove the Sprite in stage May,2012

I have created a test.as for test.fla. I add a button user interface from Adobe Flash CS5.5 and set the instance name as button1.

I want to make the result is by clicking button1 in stage a random number will be showed on stage with a TextField. And if I click button1 again, I want to remove the last number and add a new random number to be there.

The code of test.as is below:
ActionScript Code: package  {    import flash.display.Sprite;    import flash.events.MouseEvent;    import flash.text.TextField;    import flash.events.Event;        public class test extends Sprite{        public function test() {            init();        }                public function init(){            var score1:Number=0;            button1.addEventListener(MouseEvent.MOUSE_UP,getnumber1);                            function getnumber1(evt:MouseEvent):void{                    if(score1!=0){                    //I want to remove the field1 Sprite by those if(), but it doesn't work.                        removeChild(field1);                    }                    trace(score1=Math.random());                    var field1:Sprite=shownum1();                    addChild(field1);                }                                function shownum1():Sprite{                    var label:TextField=new TextField();                    label.width=20;                    label.textColor=0x0000ff;                    label.text=String(score1);                    var sp:Sprite=new Sprite();                    sp.x=180;                    sp.y=300;                    sp.addChild(label);                    return sp;                }        }        }}
Why I can't remove the Sprite in stage

Related Post



0 comments: