A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: [RESOLVED] AS3 to AS2, convertion problem

  1. #1
    Senior Member somlemeg's Avatar
    Join Date
    Aug 2000
    Posts
    171

    resolved [RESOLVED] AS3 to AS2, convertion problem

    I want to create a basic drawing utility as a part of my project. Since the drawing needs to be saved I wanted to convert it to a bitmap. I searched for help on google and found this example.

    However since the rest of my program is programmed in AS2, I'm getting errors these errors:
    The class or interface 'flash.display.Sprite' could not be loaded. // source: var canvas:Sprite = new Image();
    The class or interface 'flash.utils.ByteArray' could not be loaded. // source: var bytes:ByteArray;
    The class or interface 'flash.display.Shape' could not be loaded. // source: var child:Shape = new Shape();
    The class or interface 'MouseEvent' could not be loaded. // source: function mouseDownHandler (event:MouseEvent)
    The class or interface 'MouseEvent' could not be loaded. // source: function mouseDownHandler (event:MouseEvent)
    The class or interface 'MouseEvent' could not be loaded. // source: function mouseMoveHandler (event:MouseEvent)
    Here in the script:

    PHP Code:
    // http://www.flashscript.biz/flashas3/SaveImage/SaveImage.html
    // We start with importing several classes. 
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.utils.ByteArray;
    import flash.geom.Rectangle;
    import flash.net.SharedObject;
    import flash.events.*;
    import flash.display.DisplayObject;
    import flash.display.Graphics;
    import flash.display.Shape;
    import flash.display.Sprite;

    // We create a variable canvas and add it to the stage 
    var canvas:Sprite = new Image();
    canvas.10;
    canvas.10;
    stage.addChild(canvas);

    // We declare several other variables, which we will later need. 

    var myBitmapData:BitmapData = new BitmapData (canvas.widthcanvas.heightfalse0x00FFFFFF);
    var 
    bytes:ByteArray;
    var 
    child:Shape = new Shape();
    var 
    isDrawing:Boolean false;


    // Then we give canvas some mouse functions, since we need to draw an image. 

    canvas.stage.addEventListener(MouseEvent.MOUSE_DOWNmouseDownHandler);
    canvas.stage.addEventListener(MouseEvent.MOUSE_MOVEmouseMoveHandler);
    canvas.stage.addEventListener(MouseEvent.MOUSE_UPmouseUpHandler);


    // We sart with a function when the mouse is pressed.
    // We enter the point where the drawing is supposed to start. 

    function mouseDownHandler (event:MouseEvent)
    {
        
    isDrawing true;
        if( 
    isDrawing)
        {
            
    child.graphics.lineStyle (20xFF0000);
            
    child.graphics.moveTo (mouseXmouseY);
        }
    }

    // The next function contains all the script to draw an image, when the mouse pressed is moving.
    // We set a limit to the drawing, since we want the drawing only in a certain space.

    function mouseMoveHandler (event:MouseEvent)
    {
        if (
    isDrawing)
        {
            
    child.graphics.lineTo (mouseXmouseY);
        }
        if(
    mouseX canvas.stage.getBounds(this).|| mouseX >295)
        {
            
    isDrawing false;
        }
        if(
    mouseY canvas.stage.getBounds(this).|| mouseY >145)
        {
            
    isDrawing false;
        }                                               
    }


    // When we don't want drawing to occur we do not press the mouse.
    // However, a number of other events are then happening.
    // First of all the drawing (child) is saved by using addChild.
    // We then convert the drawing to a bitmap.
    // The bitmapdata will be stored in the byteArray object.

    function mouseUpHandler (event:MouseEvent)
    {
        
    canvas.addChild (child);
        
    isDrawing false;
        var 
    rect:Rectangle = new Rectangle(00canvas.widthcanvas.height);
        
    myBitmapData.draw (canvas);
        
    bytes myBitmapData.getPixels(rect);
        
    bytes.position 0;

    I have not used classes before and is only used to programming in AS2.

    Merry Christmas everyone!

  2. #2
    Senior Member somlemeg's Avatar
    Join Date
    Aug 2000
    Posts
    171
    Can't quite find a replacement for the Shape object.

    I'll think I'll try converting the hole project to AS3 instead, anyone having some good articles on that topic?
    My code is about 600 lines long, plus code on buttons. Any help to getting stated would be appreciated.
    Last edited by somlemeg; 12-30-2009 at 09:35 AM.

  3. #3
    Junior Member
    Join Date
    Dec 2009
    Posts
    11
    Hi,

    Try this


    Hi,

    var drawingFlag:Boolean=false;
    drawingPad.onMouseDown=function(){
    this.lineStyle(1,0xFF0000,100)
    drawingFlag=true
    this.moveTo(this._xmouse,this._ymouse)
    }
    drawingPad.onMouseMove=function(){
    if(drawingFlag){
    this.lineTo(this._xmouse,this._ymouse)
    }
    }
    drawingPad.onMouseUp=function(){
    drawingFlag=false
    }

    function createBitmap(){
    var bitmapData:BitmapData=new BitmapData(drawingPad._width, drawingPad._height, true, 0xFFFFFFFF);
    bitmapData.draw(drawingPad)
    }

  4. #4
    Senior Member somlemeg's Avatar
    Join Date
    Aug 2000
    Posts
    171

    Thanks

    Thanks Johnny2528,

    I'v made some minor changes to your script and now it works.

    PHP Code:
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.utils.ByteArray;
    import flash.geom.Rectangle;
    import flash.net.SharedObject;
    import flash.events.*;
    import flash.display.DisplayObject;
    import flash.display.Graphics;
    import flash.display.Shape;
    import flash.display.Sprite;

    createEmptyMovieClip("drawingPad",1);
    drawingPad.createEmptyMovieClip("drawing",1);

    var 
    drawingFlag:Boolean false;
    drawingPad.onMouseDown = function() {
        
    drawingPad.drawing.lineStyle(5,0xFF0000,100);
        
    drawingFlag true;
        
    drawingPad.drawing.moveTo(this._xmouse,this._ymouse);
    };
    drawingPad.onMouseMove = function() {
        if (
    drawingFlag) {
            
    drawingPad.drawing.lineTo(this._xmouse,this._ymouse);
        }
    };
    drawingPad.onMouseUp = function() {
        
    drawingFlag false;
    };

    function 
    createBitmap() {
        var 
    bitmapData:BitmapData = new BitmapData(drawingPad._widthdrawingPad._heighttrue0xFFFFFFFF);
        
    bitmapData.draw(drawingPad);


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