Categories
Featured jobs
» More ActionScript, Flash and Flex jobs.
» Advertise a job for free
Our network
Advertisement

 »  Home  »  Tutorials  »  Flash  »  Intermediate  »  Jigsaw Puzzle

Jigsaw Puzzle

By Supuneet Bismil | Published 09/9/2005 | Intermediate | Rating:
Page 1 of 1
Tutorial details:
Written by: Supuneet Bismil
Difficulty Level: Intermediate
Requirements: FlashMX
Download FLA

OBJECT ORIENTED PROGRAMMING IN FLASH

1. The aim of the project is to demonstrate the object oriented capabilities of Flash. I have created a simple four piece jigsaw puzzle, programmed with ActionScript. The coding is done in such a way that new puzzles can be created using the same file, with very little effort.
2. To view the final product of this project, open the puzzle.fla file in Flash MX, and then go to File – Publish Settings and check the Windows projector box (if you are using Mac check the Mac Projector box). Then click on the publish button. Open the folder where you published the file and view the puzzle.exe file.
3. Now for the project. Open the file ‘puzzle.fla’ on the CD-ROM.
4. Now open the library (Window-Library or Ctrl+L). I have arranged all the graphics elements, buttons and sounds required for the project in folders.
5. If you look at the timeline, you’ll notice only two layers on the main timeline:
a) puzzle: this layer contains an instance of the movieclip symbol ‘container’.
b) anim: this layer contains an instance of the movieclip symbol ‘xmastree_anim’. I have added an action to this movieclip to make it invisible initially.

6. Go to the library, open the movie_clips folder and double click the ‘container’ movieclip. This opens the ‘container’ movieclip on the stage. I have organized the timeline layers into three folders and two separate layers.
7. Open the ‘static’ folder and you’ll see that it contains layers for the text elements, start button, a frame for the puzzle and also a layer for holding the movieclip ‘complete_puzzle’. I have given the ‘complete_puzzle’ the instance name ‘joined_mc’.
8. The ‘pieces’ folder contains layers one each for the pieces in the puzzle. The pieces are just dropped anywhere on the stage. I have given them instance names of p1 through p4.
9. For the ‘inv_pieces’ folder, I just copied the pieces from the ‘complete_puzzle’ movieclip and pasted them directly onto the ‘container’ movieclip stage and aligned them with ‘joined_mc’.
10. The ‘off-stage’ layer contains a text-box placed at a position so that it will be off stage and therefore invisible when the puzzle is played. I have made it into a dynamic text type box with the instance name of ‘no_of_pieces’.
11. The ‘axns’ layer contains some but not all the action! Two variables ‘n’ and ‘success’ have been initialized on this layer. The variable ‘n’ takes its value from the ‘no_of_pieces’ text-box. I have assigned random positions to the p1, p2, p3 and p4. The code is as follows:

n = parseInt(no_of_pieces.text);
success = 0;
for(i=1;i<= n;i++)
{
        this["p"+i]._x = Math.random()*180;
        this["p"+i]._y = Math.random()*200;
        this["p"+i]._xscale = 50;
        this["p"+i]._yscale = 50;
}

12. Click on the start button and go to the actions panel. The code attached is as follows:

on(release)
{
        for(i=1;i<=n;i++)
        {
                this.joined_mc["p"+i]._visible = false;
        }
}

Basically this code just makes ‘joined_mc’ disappear.

13. Click on p1 through p4 and you’ll notice that all of them have just on line of code
#include "ascript.as"

14. Now go to the project folder and open the ascript.as.txt file. In this file, I have stored all the code for handling the puzzle pieces. It looks a bit lengthy but the jist of the code is this:
a) When the user clicks on a piece, start dragging the piece.
b) When the user releases the mouse, stop dragging.
c) Check the target of the dropped piece. If it is the corresponding invisible piece, then make the corresponding piece of the joined_mc visible and increment the value of the variable ‘success’.
d) Else send the piece back to a random position.
e) If the value of success equals the value of variable n, make the ‘puzzle’ movieclip invisible and play the animation.
f) The ActionScript for the above pseudocode is as follows:

on(press)
{
        if(this.hitTest(_root._xmouse,_root._ymouse))
        {
                this.startDrag();
        }
}


on(release)
{
        stopDrag();
        name = this._name;
        int_name = name.substring(1);
        int_name = parseInt(int_name);
       
        if(eval(this._droptarget) == _parent["inv_p"+int_name] )
        {
                _parent.joined_mc["p"+ int_name]._visible = true;
                this._visible = false;
                snd = new Sound();
                snd.attachSound("snd_ok");
                snd.start();
                _parent.success++;
                if (_parent.success == _parent.n)
                {
                        _root.puzzle._visible = false;
                        _root.anim._visible = true;
                        music = new Sound(anim);
                        music.attachSound("jingle");
                        music.start();
                        _root.anim.play();
                       
                }
        }
        else
        {
                this._x = Math.random()*180;
                this._y = Math.random()*200;
                snd = new Sound();
                snd.attachSound("snd_fault");
                snd.start();
        }
}

That’s it. The puzzle is ready. To change the puzzle you just have to swap the instances of all movieclips in the container movieclip with the new instances, put the #include action on each puzzle piece, type the value into the ‘no_of_pieces’ text box and swap the anim movieclip on the main timeline with the new one.


How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent

Verification:
Enter the security code shown below:
img


Add comment

Spread The Word / Bookmark this content

Clesto Digg it! Reddit Furl del.icio.us Spurl Yahoo!

Related Articles
Comments
  • Comment #1 (Posted by Art Redhead - art.redhead at btinternet.com)
    Rating
    Learned from the article but when trying to use found that I was unable to use for over 4 pieces.
     
  • Comment #2 (Posted by jdougan273 - jdougan at hotmail.com)
    Rating
    Very nice tutorial. I was able to use more than 4 pieces when I tried it, it's just that you need to make sure to update the instance names, add new ones as needed foe new pieces, and for sanity sake try to keep everything on it's own layer (adding new ones for new pieces). Thanks!
     
Submit Comment



Search Entire Site
Add to Google
Advertisements
Article Options
Latest New Articles
Set up a simple IIS Server for Flash
by Peter McBride

Day 1 at FITC Toronto 2008
by Anthony Pace

Simple reflection effect with AS2
by Jean André Mas

ActionScript.org Meets Josh Tynjala (aka dr_zeus)
by ActionScript.org Staff

Rapidly Create Online Flash Movies to Help Users Market, Sell and Support Software and Hardware
by Sabrina F

mailing list
Enter your email address:
mailing list
Subscribe Unsubscribe
© 2000-2007 actionscript.org! All Rights Reserved.
Read our Privacy Statement and Terms of Use...
Our dedicated server is hosted and managed by WebScorpion Webhosting.