PDA

View Full Version : Move in, then move out


tinyk
07-08-2004, 08:08 PM
Hello, I'm sure this is super easy, but I cannot figure it out. I would think I would use and if statement, but I'm sort of stuck.
I have a movieClip (we'll call it MC). I want MC to move forward 200 pixels, then once it reaches that point, move back to it's starting point.
My problem is that when I use an if statement, it gets stuck because both statements are true.

Code:
function moveBlock() {
_root.block1.onEnterFrame=function() {
if (this._x>=200) {
this._x+=10;
}
if (this._x<=400) {
this._x-=10;
}
}
}

I know why it's not working, but I can't think of anyway to do it other than this, which doesn't work because both if statements are always true. ???

tg
07-08-2004, 08:44 PM
try:

function moveBlock() {
_root.block1.onEnterFrame=function() {
if (this._x>=200 && movement=="forward") {
this._x+=10;
movement=this._x<400 ? "forward" : "backward";
}
if (this._x<=400 && movement=="backward") {
this._x-=10;
movement=this._x>200 ? "backward" : "forward";
}
}
}


not tested, but i think that will work (read my footer).

praufet
07-08-2004, 09:12 PM
So you want it to move from 200 to 400, right? Because even I'm a little confused by your if statements.

tg
07-08-2004, 09:36 PM
oops... for my code to work, you will need to initialize movement with 'forward' or 'backward' to get it to work.

binkyboo
07-08-2004, 09:44 PM
function moveBlock(){
speed = 20;
origPos = this.block1._x
destPos = this.block1._x + 200
block1.onEnterFrame = function(){
if(this._x < origPos || this._x > destPos){
speed *= -1;
}
this._x += speed;
}
}
moveBlock();

Since I spent like 20 minutes on this and don't want my efforts to go wasted, here's another piece of code that works.

@ tg - you're too quick! :)

tinyk
07-13-2004, 03:40 PM
Binkyboo! THANK YOU!!! I haven't had time to work on this in a few days and your function worked BEAUTIFULLY!!! You're the best!! Thanks!

CyanBlue
07-13-2004, 03:43 PM
Just a dumb thing to add... How come I don't see any of the delete onEnterFrame; lines??? :(