AS3: Delete specific shape
A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: AS3: Delete specific shape

  1. #1
    Junior Member
    Join Date
    Feb 2009
    Posts
    4

    Question AS3: Delete specific shape

    Hi sharks..

    I've made a functions which creates rectangles:

    function makeSquare(sgu:String,clo:uint):void {
    var squ:Sprite = new Sprite();
    addChild(squ);
    squ.graphics.lineStyle(1,0x000000);
    squ.graphics.beginFill(clo);
    squ.graphics.drawRect(0,0,100,100);
    squ.graphics.endFill();
    squ.x = Math.round(Math.random() * 550);
    squ.y = Math.round(Math.random() * 400);
    }

    Now my problem is; that i'm from another function would like to make a call, that deletes a specific rectangle previously generated by the above function. I just can't fingure out how - it's properly really simple, but i'm still a bit of a rockiee..

    Hope someone can give me the answere.

    Best regards from Denmark

  2. #2
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,046
    You need to return the Sprite from that function to be able to use it access it later:
    PHP Code:
    function makeSquare(sgu:String,clo:uint):Sprite {
        var 
    squ:Sprite = new Sprite();
        
    squ.graphics.lineStyle(1,0x000000);
        
    squ.graphics.beginFill(clo);
        
    squ.graphics.drawRect(0,0,100,100);
        
    squ.graphics.endFill();
        
    squ.Math.round(Math.random() * 550);
        
    squ.Math.round(Math.random() * 400);
        
        return 
    squ;
    }

    var 
    mySquare:Sprite makeSquare("Something"0);
    addChild(mySquare);
    removeChild(mySquare); 

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    2

    Deleting multiple shapes on stage or button click

    Hello,
    I used your method in a similar way to draw a triangle pointing to buttons as I pick each.
    I would like the fill shape to clear or disappear as I pick on stage or another button, but I get an errors about null ref.
    I'm still fair new at as3.

    I got it to work somewhat, but not smooth. I have about 100 buttons on stage, each calling the function "onClick". It only works if I roll out of the fill. Roll_Out was the best event I thought.

    Code:
    staff099.addEventListener(MouseEvent.CLICK, onClick);
                    function onClick(e:MouseEvent):void {
    			//tell me what button was picked and get its stage xy.
    		var buttonPicked:String = e.target.name;
    		var buttonPickLocX = e.stageX;
    		var buttonPickLocY = e.stageY;
    		trace( "x location: " + buttonPickLocX + "  y= " + buttonPickLocY);
                    //sub function
    		function makeFillPointer(sgu:String, clo:uint):Sprite {
    			
    			var myFillPointer:Sprite = new Sprite();
    			myFillPointer.graphics.lineStyle(1, 0x996666, 100);
    			myFillPointer.graphics.beginFill(0xD6D6D6, 0.6);
    			myFillPointer.graphics.moveTo(288, 50);
    			myFillPointer.graphics.lineTo(buttonPickLocX, buttonPickLocY);
    			myFillPointer.graphics.lineTo(288, 360);
    			myFillPointer.graphics.endFill();
    			return myFillPointer;
    		}
    		var myPointer:Sprite = makeFillPointer("Something", 0);
    		addChild(myPointer);
    		
    		myPointer.addEventListener(MouseEvent.ROLL_OUT, onFillOut);
    		function onFillOut(e:MouseEvent):void {
    			removeChild(myPointer);  
    		}
                   }
    It might be a case of private vs public function, but I have't got a good handle on mixing the two.

    Thanks for your help!

  4. #4
    :
    Join Date
    Dec 2002
    Posts
    3,511
    Code:
     var myPointer:Sprite;
     var btns:Array = ["staff099","staff098"];
    
     stage.addEventListener(MouseEvent.CLICK, onClick);
    
     function onClick(e:MouseEvent):void {
             if (btns.indexOf(e.target.name) > -1) {
                     clearPointer(e);
                     var buttonPicked:String = e.target.name;
                     var buttonPickLocX:int = e.stageX;
                     var buttonPickLocY:int = e.stageY;
                     trace( "x location: " + buttonPickLocX + "  y= " + buttonPickLocY);
                     myPointer = makeFillPointer("Something",buttonPickLocX,buttonPickLocY,0);
                     addChild(myPointer);
                     e.target.addEventListener(MouseEvent.ROLL_OUT, clearPointer, false, 0, true);
             }
     }
     function makeFillPointer(sgu:String, locX:int, locY:int, clo:uint):Sprite {
             var myFillPointer:Sprite = new Sprite();
             myFillPointer.graphics.lineStyle(1, 0x996666, 100);
             myFillPointer.graphics.beginFill(0xD6D6D6, 0.6);
             myFillPointer.graphics.moveTo(288, 50);
             myFillPointer.graphics.lineTo(locX, locY);
             myFillPointer.graphics.lineTo(288, 360);
             myFillPointer.graphics.endFill();
             myFillPointer.mouseEnabled = false;
             return myFillPointer;
     }
     function clearPointer(e:MouseEvent):void {
             if (myPointer) {
                     e.target.removeEventListener(MouseEvent.ROLL_OUT, clearPointer);
                     myPointer.parent.removeChild(myPointer);
                     myPointer = null;
             }
     }

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    2
    Quote Originally Posted by dawsonk View Post
    Code:
     var myPointer:Sprite;
     var btns:Array = ["staff099","staff098"];
    Thanks for the extra tip Dawsonk. This works perfect.
    Appreciate it!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width


HTML5 Development Center

Poll by Flashkit.com