31 January 2009

Virtual Trash: Flash Tutorials with Action Script

Use startDrag, stopDrag, _droptarget, onPress and onRelease functions to simulate virtual trash.




This tutorial is advanced, you have to understand basic commands of Flash Designer.


Download source project t1049.zip (86 kb)


  1. Launch Flash Designer and set movie dimensions 300 x 300.
  2. Create 4 blank frames with "Frame" > "New" so you have "Frame 1" to "Frame 5" in the frame list.
Frame 1
  1. Download t1049.zip file and extract trash images.

  2. Import image "pieceofpaper.gif" (File > Import Image) and move it outside visible area.
  3. Convert it to sprite (Edit > Convert to Sprite), name it "Garbage" and check "ActionScript" target (Item > Placement Properties)
  4. Import image "opentrash.gif" and move it to X:170, Y:118 (Edit > Move To) .
  5. Draw an ellipse over the hole of the trash. Set fill color to black, 1% opacity, no line
  6. Convert it to sprite, name it "Hole" and check "ActionScript" target
  7. Add the text "Put all papers into trash"
  8. Add Frame ActionScript (Frame > ActionScript):

    maxpapers=1;

Frame 2
  1. Add the text "GET READY!"
  2. Set background to "Frame 1"

Frame 3
  1. Set frame delay to "Stop"
  2. Set background to "Frame 1"
  3. Import "laser.wav" sound and set the frame sound
  4. Add Frame ActionScript:

    maxpapers = maxpapers * 2; // at each run increase the number of pieces
    collected = 0; // count collected items

    function checkifallcollected()
    {
    // check if all items are collected and go to congratulations frame
    if(collected==maxpapers) gotoAndPlay("Frame 4");
    }

    // this array will hold all pieces
    var garbages = new Array();

    // create maxpapers pieces
    for(i=0;i<maxpapers;i++)
    {
    // create new instance of the paper
    garbage[i] = Garbage.duplicateMovieClip("garbage"+i,100+i);

    // put it in random place and random rotation
    garbage[i]._x = 30+100*Math.random();
    garbage[i]._y = 30+200*Math.random();
    garbage[i]._rotation = 360*Math.random();

    // define mouse events
    garbage[i].onPress=function() { this.startDrag();};
    garbage[i].onRelease=function()
    {
    // check if this piece hits the hole
    if(this._droptarget=="/Hole") {this.removeMovieClip();collected++;}

    stopDrag();
    checkifallcollected();
    };

    }

Frame 4
  1. Import "elecshot.wav" sound and set the frame sound
  2. Import image "closedtrash.gif" and move it to X:174, Y:163.
Frame 5
  1. Set frame delay to 3 seconds
  2. Set background to "Frame 4"

  3. Set loop to frame 2 ("Frame" > "Loop" command)
  4. Import "clap.wav" sound and set the frame sound

No comments:

Post a Comment

Follow on Buzz