A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Making this AS2 code work in AS3 - please help!

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    4

    Making this AS2 code work in AS3 - please help!

    Hi everyone,

    I found this code on a forum which allows for an undo and erase button for a drawing application (and is perfect for what I need) but it's in AS2 and I can barely make sense of it:


    Code:
    _root.attachMovie("canvas", "canvas", 0);
    d = 0;
    _root.strokes = new Array();
    _root.canvas.onPress = function() {
    	if (deleted) {
    		_root.attachMovie("canvas", "canvas", 0);
    	}
    	d++;
    	_root.strokes.push("stroke"+d);
    	_root.canvas.createEmptyMovieClip("stroke"+d, d);
    	_root.canvas["stroke"+d].moveTo(_xmouse, _ymouse);
    	_root.canvas["stroke"+d].lineStyle(0, 0x000000, 100);
    	this.onEnterFrame = function() {
    		_root.canvas["stroke"+d].lineTo(_xmouse, _ymouse);
    	};
    };
    _root.canvas.onRelease = function() {
    	this.onEnterFrame = null;
    };
    _root.canvas.onReleaseOutside = function() {
    	this.onEnterFrame = null;
    };
    buttonUndo.onPress = function() {
    	if (d != 0) {
    		_root.canvas["stroke"+d].removeMovieClip();
    		d--;
    		_root.strokes.pop();
    	}
    };
    buttonErase.onPress = function() {
    	for (i=1; i<=_root.strokes.length; i++) {
    		_root.canvas["stroke"+i].removeMovieClip();
    	}
    };


    This bit in particular confuses me and I have no idea how to turn it into working AS3 code:

    Code:
     	_root.strokes.push("stroke"+d);
    	_root.canvas.createEmptyMovieClip("stroke"+d, d);
    	_root.canvas["stroke"+d].moveTo(_xmouse, _ymouse);
    	_root.canvas["stroke"+d].lineStyle(0, 0x000000, 100);

    I just don't understand how it's adding the lines being drawn to the strokes array. If anyone could help me get it working, I would really appreciate it, as I'll fail the university module if I can't get the array working

  2. #2
    Junior Member
    Join Date
    May 2010
    Posts
    4
    Ok, kinda worked it out, now new problem...

    Code:
       function startDraw(event:MouseEvent):void {
        if (inDrawingArea == true) { 
         noOfLines++; 
         var newLine_mc = new MovieClip();
         newLine_mc.name = "newLine_mc"+noOfLines;
         line.graphics.moveTo(mouseX, mouseY);   
         lines.push(newLine_mc);
         areDrawing = true;  
        }
       }   
       function doDraw(event:MouseEvent):void {
        // Only draw if mouse is down
        if(areDrawing == true) {
         newLine_mc.graphics.lineTo(mouseX, mouseY);
         addChild(newLine_mc);   
         setChildIndex(newLine_mc, 0);
        }
       }  
       function stopDraw(event:MouseEvent):void {
        areDrawing = false;   
       }   
    
       function undoAction(event:MouseEvent):void { 
        if (lines.length != 0) {   
         i = lines.length;  
         lines.pop();
         removeChild(lines[i]);  
        }
       }
    With this approach when I hit undo it seems like it can't find the movieclips that have been created because I get the error...

    Code:
    TypeError: Error #2007: Parameter child must be non-null.
     at flash.display::DisplayObjectContainer/removeChild()
    I put a load of traces in and I know it's adding them to the array where it's meant to, and the pop method is successfully removing them from the array but it's just not removing them from the stage. I can't work it out.

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You need to call removeChild before pop, or else save the result of pop to pass to removeChild. Once you pop the last item from lines, lines[i] is undefined.

  4. #4
    Junior Member
    Join Date
    May 2010
    Posts
    4
    Sadly, I noticed that one myself and tried it and still the same error exists.

  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    What exactly did you try?
    You know that lines[lines.length] is always undefined, right? Array indices start at 0, so if it has 5 elements in it the length is 5, but the last element is at 4.

    try this:
    Code:
       function undoAction(event:MouseEvent):void { 
        if (lines.length != 0) {   
         removeChild(lines.pop());  
        }
       }

  6. #6
    Junior Member
    Join Date
    May 2010
    Posts
    4
    I meant moving removeChild to before pop. That code works perfectly though, thank you very much! I'm not a coder really so I struggle with this kind of thing.

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