Denis Nemytov's Solution
Denis is one of my students who decided "there has to be an easier way" and then went out and found it.
Here is Denis' approach to the project and the commented code:
Flash 1: Click a button to see the sliding effect.
The code:
stop();
var picWidth:Number =300; //the width of pictures I use for this project
var numPic:Number =6; //number of picture I use
var i:Number = 0; //loop variable
var newHolder:MovieClip; //used to store duplicates of imageHolder_mc
var newX:Number; //used to keep last imageStrip_mc x value
var speed:Number; //image strip speed
In this loop I create a strip of pictures I have in the same folder where my .swf file is located.
How this is accomplished is really quite simple. Denys creates a changing value (i) and ensures that is is less than the number in the name of the image.Then he simply duplicates the movieclip where the pictures are located, places them in a row (newHolder._x = picWidth * i) and loads the image into the duplicated movieclip.
for (i;i<numPic;i++){
newHolder = _root.imageStrip_mc.imageHolder_mc.duplicateMovieClip("imageHolder"+i+"_mc",i);
newHolder._x = picWidth*i;
newHolder._y = 0;
newHolder.loadMovie("pic"+i+".jpg");
};
//I need variable i to be 0 again because I use it again for another purpose
i=0;
//this is what happens when I press next button
_root.next_btn.onRelease = function(){
_root.imageStrip_mc._x = -picWidth*i;
Here I make sure that the current image is at its appropriate position
if (i<(numPic-1)) {i +=1;};
Here I increase value of "i" in case it is not bigger than number of pictures i use
newX = _root.imageStrip_mc._x;
Here I generate newX variable that I need for speed calculations
_root.imageStrip_mc.onEnterFrame = function(){
if (this._x > -(i*picWidth)) {
In the next 3 lines I generate the movment of the strip on a calculated speed.
This is where Denys really gets elegant in his coding. He calculates the speed using the image width instead of an arbitrary number.
speed = Math.round(-(newX-picWidth-this._x)/2);
speed = Math.Round {-(
this._x -= speed;
}
}
}
This is what happens when I press the previous button
_root.previous_btn.onRelease = function(){
_root.imageStrip_mc._x = -picWidth*i;
if (i>0) {i -=1;};
newX = _root.imageStrip_mc._x;
_root.imageStrip_mc.onEnterFrame = function(){
if (this._x < -(i*picWidth)) {
speed = Math.round((picWidth-(this._x-newX))/2);
this._x +=speed;
}
}
}
![]()
