Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Noob needs help with this code ... ;)

Status
Not open for further replies.

michanagel

Technical User
Jun 23, 2007
34
US
Hi,

I found this code in a nice tutorial and I wanna make slight adjustments to the code.

Unfortunately my Action Script skills are very limited... ;)

This is the code for a 'sliding menue', depending on which button u pressed it will 'slide' to the appropriate picture.

Here's the code:


var currentPosition:Number = large_pics.pic1._x;
var startFlag:Boolean = false;
menuSlide = function (input:MovieClip) {

if (startFlag == false) {


startFlag = true;

var finalDestination:Number = input._x;
var distanceMoved:Number = 0;
var distanceToMove:Number = Math.abs(finalDestination-currentPosition);
var finalSpeed:Number = .2;
var currentSpeed:Number = 0;
var dir:Number = 1;

if (currentPosition<=finalDestination) {

dir = -1;

} else if (currentPosition>finalDestination) {

dir = 1;

}

this.onEnterFrame = function() {

currentSpeed = Math.round((distanceToMove-distanceMoved+1)*finalSpeed);
distanceMoved += currentSpeed;
large_pics._x += dir*currentSpeed;
if (Math.abs(distanceMoved-distanceToMove)<=1) {

large_pics._x = mask_pics._x-currentPosition+dir*distanceToMove;
currentPosition = input._x;
startFlag = false;
delete this.onEnterFrame;

}

};

}

};
b1.onRelease = function() {

menuSlide(large_pics.pic1);

};
b2.onRelease = function() {

menuSlide(large_pics.pic2);

};
b3.onRelease = function() {

menuSlide(large_pics.pic3);

};
b4.onRelease = function() {

menuSlide(large_pics.pic4);

};

I need to adjust five things in this code...

(1) I want this menue to slide vertically not horizontally.

I changed the 'x' values in the code to 'y' which I thought would make it move vertically, but it doesn't work...

(2) Is it possible that, whatever the distance is, the "sliding" time is always 2.2 sec ?

(3) I need to implement code that after the final position is reached, the timeline jumps to a certain movieclip to a certain label - depending on what button was pressed of course...

I tried to implement this code for button number two...

b2.onRelease = function() {

menuSlide(large_pics.pic2);

}

if (currentPosition = finalDestination) {

this.large_pics.pic2.gotoAndPlay("s1");

};

--> sliding still works but it doesn't jump to the appropriate label...

(4) I wanna add 'Next' & 'Previous' buttons to the slide show - what would be the code in this case scenario ?

My first thought was something like that Flash checks which 'pic' movieclip it is showing right now (pic1, pic2, pic3 etc.) and depending on what button u pressed u go to the y value of movieclip 'picX + 1' (Next button) or 'picX - 1' (Previous button)...

Is that possible ?

(5) After implementing the Next & Previous buttons I need to make sure that when it reached the last pic movieclip it will not go further on the y value - because there is no more pic movieclip.

Options are to either slide back to movieclip 'pic1' or simply do nothing any more on the next button...

I know this is probably Kindergarten for you, but I have only slight ideas how to do this and no code knowledge to back it up... haha

Thanx a lot for your help in advance !

Always a pleasure to learn from u guys... ;)

Mike
 
Hi,

I made some progress with the code thanx to the help of Simon, but there are still 2 things that need to be addressed...

(1) I want the sliding time always to be 2.2 sec...

here's my approach to it - just a theory but it might work:

we need a speed that changes dynamically depending on the distance we have to travel...

I don't know if that applies for Action Scrip but I recall from 6th grade, that...

speed = distance / time

--> we got the time (which is always 2.2 sec)
--> we got the disctance (currentposition-finaldestination)

--> this should automatically change the speed to the appropriate value

Unfortunately I have no clue how the action script would look like (like I said my action script skills are very limited)...

(2) Also, one other thing I need that is not implemented yet, is that when the final destination is reached it jumps to a certain label inside a certain movieclip - every time different for each button pressed - something like:

if (currentPosition = finalDestination) {

this.large_pics.pic2.gotoAndPlay("s1");

};

that statement just doesn't work when I put it right under the function for each button...

Thanx again for taking the time !!!

Mike

P.S.: Here's the updated code:

//vars *************************************
var currentPosition:Number = large_pics.pic1._y;
var menuPos:Number = large_pics._y;
var numPics:Number = 4;
var curPic:Number = 1;
var startFlag:Boolean = false;
//event handlers ***********************************
b1.onRelease = function() {
menuSlide(large_pics.pic1);
curPic = this.id;
};
b2.onRelease = function() {
menuSlide(large_pics.pic2);
curPic = this.id;
};
b3.onRelease = function() {
menuSlide(large_pics.pic3);
curPic = this.id;
};
b4.onRelease = function() {
menuSlide(large_pics.pic4);
curPic = this.id;
};
next_btn.onRelease = function() {
gotoNextPic();
};
prev_btn.onRelease = function() {
gotoPrevPic();
};
//functions *********************************
menuSlide = function (input:MovieClip) {
if (startFlag == false) {
//setting startFlag to express animation in progress
startFlag = true;
//destination position defined by the position of the image clicked
var finalDestination:Number = input._y;
//
var distanceMoved:Number = 0;
//how far does the menu have to move until image is in correct position
var distanceToMove:Number = Math.abs(finalDestination-currentPosition);
var finalSpeed:Number = .2;
var currentSpeed:Number = 0;
var dir:Number = 1;
if (currentPosition<=finalDestination) {
dir = -1;
} else if (currentPosition>finalDestination) {
dir = 1;
}
//animation script:
this.onEnterFrame = function() {
//how far to move menu this frame:
currentSpeed = (distanceToMove-distanceMoved)*finalSpeed;
//
distanceMoved += currentSpeed;
//moves menu up or down, depending on value of dir:
large_pics._y += dir*currentSpeed;
//menu has arrived to correct position:
if (Math.abs(distanceMoved-distanceToMove)<=1) {
large_pics._y = menuPos-currentPosition+dir*distanceToMove;
currentPosition = input._y;
startFlag = false;
delete this.onEnterFrame;
}
};
}
};
function gotoNextPic() {
if (startFlag == false) {
if (curPic<numPics) {
curPic += 1;
menuSlide(large_pics["pic"+curPic]);
}
}
}
function gotoPrevPic() {
if (startFlag == false) {
if (curPic>1) {
curPic -= 1;
menuSlide(large_pics["pic"+curPic]);
}
}
}
//run at once *********************************
for (var i = 1; i<=numPics; i++) {
this["b"+i].id = i;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top