# Senocular 3d tut question

In the beginning of sen’s 3d tutorial, there’s an example of 3 guys moving back and forth and resizing so they look as if they are 3d…they move back and fourth with this function:

[AS]

backAndForth = function(){
this.z += speed*this.dir;
if (this.z > 500){
this.z = 500;
this.dir = -1;
}else if (this.z < 0){
this.z = 0;
this.dir = 1;
}

var scaleRatio = focalLength/(focalLength + this.z);
this._x = origin.x + this.x * scaleRatio;
this._y = origin.y + this.y * scaleRatio;
this._xscale = this._yscale = 100 * scaleRatio;
this.swapDepths(-this.z);

};

[/AS]

then they are moved with this:

[AS]
figureA.onEnterFrame = backAndForth;
figureB.onEnterFrame = backAndForth;
figureC.onEnterFrame = backAndForth;
[/AS]

what i want to do is change the movement function into 2 seperate parts, back and forth, and set it up so you have to click a guy to get him to come foward, then click him again to send him back?

i’ve tried…but it’s not working out, any help?

the this.dir variable controls the direction of movement. You can see that the z variable (this.z) gets incremented based on dir.

this.z += speed*this.dir;

so if dir is positive, z goes up by a factor of speed. If it’s negative, z goes down by a factor of speed.

So what you need is to do is add a click behavior that changes that dir variable. This will probably consist of something along the lines of

onPress = function(){ this.dir = -this.dir; }

so that it will reverse or change over the current directional variable of dir. If its positive now, clicking will make it negative and change directions. If negative, it will become positive and, again, change directions.

The only thing left is to keep the clip from continuously going in that same direction indefinitely, i.e. if dir is negative, dont keep moving z back and back and back forever. This can be handled with a simple if condition making sure that z doesnt go greater than or less than its supposed to. This is actually already done in the code you posted with 0 and 500. You just need to take out where dir is changed. That will be handled in your onPress action.

hmm, i’m not quite sure, i think i understand what your saying, and this is what i have now…but they aren’t moving…even though the z factor should be changing on press

you didnt assign the onEnterFrames

would i add that to the main function, or to the individual on key presses?

no the ones you deleted and posted in your first post. Those dont go away.

Thanks a ton

start their z at any of the maxes. (make sure you set the dir property to go with that)

i figured that out right after i posted, hehe