The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.


Results 1 to 3 of 3

Thread: AS3: clear a single line graphics from its multiple instance upon click

  1. #1

    AS3: clear a single line graphics from its multiple instance upon click

    hello Everybody,
    I am trying to delete a single line using graphics.clear() method but it clears all the instance of lines created by lineTo() method. I want to remove a single line upon click from bunch of its multiple instance.
    Any advices, suggestions are welcomed.

  2. #2
    I think the only way to do this is to store each drawn line into an Array and call the graphics.clear on a specific Array index
    As3 / JS/jQuery / HTML5 / CSS(3) / PHP (intermediate)

  3. #3
    1,391
    posts
    Registered User
    right MJ, but that would only work if each line were on independent DOs

    so another approach might be to store the vertex pairs of each segment an array, clear all, remove/add a segment, then redraw all segments contained in the array

    BUT... we also now have this type of functionality with GraphicsPath and graphics.drawGraphicsData()

    so you could create a Vector of IGraphicsData Objects, push your line segments as GraphicsPaths, then clear and redraw using the method - something like:
    Code:
    import flash.geom.*
    
    var data:Vector.<IGraphicsData> = new Vector.<IGraphicsData>();
    
    function addLine( a:Point, b:Point ):int {
        var line:GraphicsPath = new GraphicsPath( new Vector.<int>(), new Vector.<Number>() );
        line.commands.push( 1,2 );
        line.data.push( a.x, a.y, b.x, b.y );
        return data.push( line ) - 1;
    }
    
    function removeLine( id:int ):void {
        data.splice( id, 1 );
    }
    
    function removeSegment( a:Point, b:Point ):void {
        for( var n:int=0; n<data.length; n++ ) {
            var pdata:Vector.<Number> = GraphicsPath(data[n]).data;
            if( a.x==pdata[0] && a.y==pdata[1] && b.x==pdata[2] && b.y==pdata[3] ) removeLine(n);
        }
    }
    
    function render():void {
        graphics.clear();
        graphics.lineStyle( 1, 0x000000 );
        graphics.drawGraphicsData( data );
    }
    note that the addLine method returns the index of the segment - you could track each line drawn to point at it by id -OR- use the other method for matching by vertex (although i think that quicky method could be improved)
    Last edited by cbeech; April 13th, 2012 at 11:54 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012