# Cropping an image and assigning it to a movie clip

**URL:** https://forum.kirupa.com/t/cropping-an-image-and-assigning-it-to-a-movie-clip/59589
**Category:** Uncategorized
**Created:** [August 2, 2004, 8:22am UTC](https://forum.kirupa.com/t/cropping-an-image-and-assigning-it-to-a-movie-clip/59589 "2004-08-02T08:22:05Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![kensapp](https://avatars.discourse-cdn.com/v4/letter/k/3da27b/32.png) [@kensapp](https://forum.kirupa.com/u/kensapp)
#### Post date: [August 2, 2004, 8:22am UTC](https://forum.kirupa.com/t/cropping-an-image-and-assigning-it-to-a-movie-clip/59589/1 "2004-08-02T08:22:05Z")

</div>

I have created the following code. I want to load an external image. And break it into a specified number of rows and columns. How do I crop an image before assigning it to a movie clip? It currently works at generating the necessary size of each piece and placing the image where I want it. Trace statements show all calculations to be correct. i have the offsets in x,y coordinates of the portion of the larger image i want as well. Any ideas how to take just the pertion of the jpg I want and assign it to the movieclips? Then, how can I assign an action script to each movie clip it creates. Action script would be the same for each one… “on (click) {DO SOMETHING};”

Any help would be greatly appreciated… I want to do this to create a variety of different puzzles…including a sliding puzzle.

//set variables  
puzzlepic=“puz1.jpg”; //name of image  
imagewidth=300; //width of original image  
imageheight=300; //height of original image  
xstart=0; //start x coordinate  
ystart=0; //start y position  
rowmax=3; //number of rows  
colmax=3; //number of columns  
xsize=imagewidth/colmax; //calculate width of each cell  
trace(“cell width=”+xsize);  
ysize=imageheight/rowmax; //calculate height of each cell  
trace(“cell height=”+ysize);//  
//End of variables

//This function creates a grid of cells of the image.  
//Each cell will have the same action script attached  
function loadPuzzle(puz) {  
id = 0; //piece number  
xposition=xstart; //starting location on stage - x-coordinate  
yposition=ystart; //starting location on stage - y-coordinate  
for (var row = 0; row\<rowmax; row++) {  
for (var col = 0; col\<colmax; col++) {  
id++; //increment piece number  
trace ("");  
trace (“id=”+id);  
container=“piece”+id; //Assign name of container for piece  
trace (“container=”+container);  
createEmptyMovieClip(container, id);//Create container for piece at level ID  
loadMovie(puz, container); //Load image into container  
//put container at current x,y position  
setProperty (container, \_x, xposition);  
setProperty (container, \_y, yposition);  
//Calculate portion of image needed  
trace (“Crop x coordinates are:”+xposition+" to “+(xposition+xsize));  
trace (“Crop y coordinates are:”+yposition+” to "+(yposition+ysize));  
trace (“position of clip is “+xposition+”,”+yposition);  
xposition=xposition+xsize; //move right the width of one cell  
}  
xposition=xstart;  
yposition=yposition+ysize;  
}  
}  
loadPuzzle(puzzlepic);
