Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

getting an object to move back and forth

Guest
Oct 19, 2009 Oct 19, 2009

OK, at first this seemed like the easiest thing to do, but I've tried to make it work a number of different ways and nothing seems to work. I have a movie clip that I want to have constantly moving back and forth across the stage, basically "bounce" off the left and right sides. What's the best way to do this using AS2?

TOPICS
ActionScript
2.9K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Oct 19, 2009 Oct 19, 2009

if you want your object with left-edge reg point to move right initially:

yourobject.speed=3;

yourobject.onEnterFrame=function(){

if(!this.moveleft){

this._x+=this.speed;

if(this._x>=Stage.width-this._width){

this.moveleft=true;

}

} else {

this._x-=this.speed;

if(this._x<=0){

this.moveleft=false;

}

}

}

Translate
Community Expert ,
Oct 19, 2009 Oct 19, 2009

if you want your object with left-edge reg point to move right initially:

yourobject.speed=3;

yourobject.onEnterFrame=function(){

if(!this.moveleft){

this._x+=this.speed;

if(this._x>=Stage.width-this._width){

this.moveleft=true;

}

} else {

this._x-=this.speed;

if(this._x<=0){

this.moveleft=false;

}

}

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 20, 2009 Oct 20, 2009

This works perfectly, thank you. However, it seems to have messed up another movie clip. I have a second movie clip that uses a motion tewwn to get bigger and bigger until it reaches a certain size. When the main movie clip isn't moving the motion tween works perfectly, but if the main movie clip is moving the motion tween suddenly stops working. Is there a way to fix this?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 20, 2009 Oct 20, 2009

is your other tween using an onEnterFrame method applied to the same movieclip?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 20, 2009 Oct 20, 2009

Nope, no AS, just a regular tween added between two keyframes. The tween is on the timeline of the movieclip, not on the main timeline. So, when the movie clip loads it automatically runs through it's own timeline. It gets bigger and bigger, then at the end of it's timeline the movieclip gets removed. The timeline seems to play properly because after a couple second the movie clip does get removed, it just doesn't get bigger. Why would having one movie clim move back and forth stop another movie clip from using it's tween?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 20, 2009 Oct 20, 2009

it wouldn't unless there's some relationship between the two movieclips.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 20, 2009 Oct 20, 2009
LATEST

I think I may have figured it out. I added your code to the first frame of the movie clip I wanted to move, rather than the main timeline. I put in this.speed and this.onEnterFrame instead of using the movie clip's instance name. I changed it to _root.movieclip.speed and _root.movieclip .onEnterFrame and it seems to be working properly now. Strange how using this. instead of the instance name made the tween stop working.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines