# Using AS to perform animation

**URL:** <https://forum.kirupa.com/t/using-as-to-perform-animation/7937>\
**Category:** Uncategorized\
**Created:** [November 10, 2002, 8:26pm UTC](https://forum.kirupa.com/t/using-as-to-perform-animation/7937 "2002-11-10T20:26:57Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![kdzines](https://avatars.discourse-cdn.com/v4/letter/k/7ea924/32.png) [@kdzines](https://forum.kirupa.com/u/kdzines)\
**Post date:** [November 10, 2002, 8:26pm UTC](https://forum.kirupa.com/t/using-as-to-perform-animation/7937/1 "2002-11-10T20:26:57Z")

</div>

hi everyone, i’m new to this forum (and to flash mx)!

I am trying to understand how to perform animations with actionscript.

for example:

let’s say i want to move a box from x=0 to x=400, i would write:

onClipEvent(enterFrame){  
if(this._x\<400){  
this._+=5;  
}  
}

Ok all of that i get, but:

What if when it reaches 400, i want the box to move back to x=200?

The only kinda good way i can figure out is to add:

else{\_root.nextFrame}

and then put a new keyframe in with a code like:

if{this.\_x\<200){  
this.\_x-=2;  
}

(with clipEvent handlers of course)!

This can’t be the best way to do stuff! It gets really messy with inertia code.

Please can someone help me to understand the logistics of doing this kind of programming!

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 10, 2002, 8:28pm UTC](https://forum.kirupa.com/t/using-as-to-perform-animation/7937/2 "2002-11-10T20:28:24Z")

</div>

sorry, please excuse the sloppy snytax. It’s sunday and my typing’s gone a bit wobbly!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 10, 2002, 8:35pm UTC](https://forum.kirupa.com/t/using-as-to-perform-animation/7937/3 "2002-11-10T20:35:30Z")

</div>

Ty this…

```auto
onClipEvent (enterFrame) {
	this._x += 5;
	if (this._x>=400) {
		this._x = 200;
	}
}

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 10, 2002, 8:53pm UTC](https://forum.kirupa.com/t/using-as-to-perform-animation/7937/4 "2002-11-10T20:53:39Z")

</div>

thanks for such a quick reply!

What i am trying to do is a bit different to what is outlined in your code.

Imagine a very basic idea of a ball bouncing up and down. i need the ball/box to animate back up to x=200, rather than just immediately returning there.

i guess i want to know how to terminate a script and then start a new block of code.

in literal terms:

if(the ball is less than y=600 ){

move the ball down uniformly at regular intervals  
}

then

if(the ball has reached y=600 ){

move the ball back up to y=200 at regular intervals  
}

Do you know what i mean?

p.s. your lost in beta banner is really good!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 10, 2002, 8:58pm UTC](https://forum.kirupa.com/t/using-as-to-perform-animation/7937/5 "2002-11-10T20:58:19Z")

</div>

something like:

```php
if(ball_mc._y<600 ){
//code
}

```

```php
if(ball_mc._y=600 ){
//code
}

```

Hope this helps, use lostinbeta previous post to debug the code i gave u

yours,  
h88

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 10, 2002, 9:28pm UTC](https://forum.kirupa.com/t/using-as-to-perform-animation/7937/6 "2002-11-10T21:28:46Z")

</div>

thanks h88,

this is not really what i mean. I am pretty competent at scripting, this is not the problem. My problem is that when there are things with eventhandlers, in a one frame animation, then how do i get a set of actions performed, then for that script to beterminated and the next section of script to begin?

Am i making it hard to understand? I’m just having a hard time expressing myself in these terms!

Example:

onClipEvent(enterFrame){

if(this.\_x\<600 ){

this.\_x+=2;  
}

if(this.\_x\>200 ){  
this.\_x-=2;  
}  
}

obviously this script won’t work as i am asking it to do to opposite things at the same time - so it does nothing!

however i want to do the first bit (if(this.\_x\<600), then when this is complete, i want that part of the script to be terminated/destroyed.

then i want it to do the second bit (if(this.\_x.200 ).

when this second condition is met, the ball will stop at x=200;

i hope this more clear, and your advice is really appreciated.

thanks,

kdzines

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 10, 2002, 9:36pm UTC](https://forum.kirupa.com/t/using-as-to-perform-animation/7937/7 "2002-11-10T21:36:07Z")

</div>

ahh, ok i understand what u mean, ill make some code tomorow, cuz really i g2g now!

sorry :cyclops:

yours,  
h88

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 10, 2002, 9:40pm UTC](https://forum.kirupa.com/t/using-as-to-perform-animation/7937/8 "2002-11-10T21:40:54Z")

</div>

thanks h88, and beta-x for your help.

look forward to seeing what you guys come up with,

cheers!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 10, 2002, 11:46pm UTC](https://forum.kirupa.com/t/using-as-to-perform-animation/7937/9 "2002-11-10T23:46:11Z")

</div>

Im sure we can accomplish this with a do/while action, but here’s the quickest way that comes to mind.

```auto

onClipEvent(enterFrame){
	if(this._x>600){
		status="left";
	}else if(this._x<0){
		status="right";
	}
	if(status=="right"){
		this._x+=25;
	}else{
		this._x-=25;
	}
}

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 11, 2002, 12:00am UTC](https://forum.kirupa.com/t/using-as-to-perform-animation/7937/10 "2002-11-11T00:00:08Z")

</div>

nope!!!

it’s still no good because the animation gets stuck in a loop (which is the same sort of probs i’ve had)!

the second the mc reaches x=600, the mc moves left by the specified amount.

BUT:

the second that has happened the initial condition kicks in again, beacause the position is again less than 600 (do you get me).

so…

it still don’t work!

its greaqt to have you guys helping me, if i can get this figured out, i’ll do a kick-ass tutorial about it!

thanks,

Kdzines

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 11, 2002, 3:23am UTC](https://forum.kirupa.com/t/using-as-to-perform-animation/7937/11 "2002-11-11T03:23:17Z")

</div>

So you just want it to go back and forth, and then stop?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 11, 2002, 3:35am UTC](https://forum.kirupa.com/t/using-as-to-perform-animation/7937/12 "2002-11-11T03:35:53Z")

</div>

I guess I dont understand. The first part of the script does what you want… get to the end and come back. Now, you want it to stop when it gets to the beginning, and not go back and forth. Right?

In that case…

```auto

onClipEvent(enterFrame){
	
	//check status
	switch(_root.status){
		
		case "start":
		this._x+=30;
		if(this._x>600){
			_root.status="reverse";
		}
		
		break;
		
		case "reverse":
		this._x-=30;
		if(this._x<0){
			_root.status="stop";
		}
                                 break;

		case "stop":
		this._x=this._x;
		break;
	}
}

```

to begin, frame 1 has a variable called status… like:

status=“start”;

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 11, 2002, 5:16am UTC](https://forum.kirupa.com/t/using-as-to-perform-animation/7937/13 "2002-11-11T05:16:34Z")

</div>

Yep, that works 🙂

Let us find out if that is what kdzines wanted shall we :q:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 11, 2002, 5:21am UTC](https://forum.kirupa.com/t/using-as-to-perform-animation/7937/14 "2002-11-11T05:21:53Z")

</div>

yah, no offense to kd, but if I read the first few posts, the request changed over time, so I was confused. Maybe I still am… hahhaaah

no offense kd! We’re all a bit scattered in the brain!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 11, 2002, 5:22am UTC](https://forum.kirupa.com/t/using-as-to-perform-animation/7937/15 "2002-11-11T05:22:58Z")

</div>

The descriptions of what kd wanted did change over the posts, that is why I am confused too, so don’t feel bad Montoya 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 11, 2002, 10:50am UTC](https://forum.kirupa.com/t/using-as-to-perform-animation/7937/16 "2002-11-11T10:50:23Z")

</div>

thanks guys:)

i wasn’t being very clear yesterday:cyclops:

using switches does do the trick though, so cheers!

its just really hard to find good actionscripted movement tutorials.

I’ve done all the ones on kirupa, they are a bit basic!

i’ve got another teaser for you:

what’s the math for acceleration?

i know how to slow it down,

if(this.\_x\<400 ){

this.\_x+=(400-this.\_x)/20;  
}

does anyone know a simple way to get something to accelerate to a point and then stop?

Penner’s equations are too complicated without some real life examples!

thansk once again,

kdzines

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 11, 2002, 3:41pm UTC](https://forum.kirupa.com/t/using-as-to-perform-animation/7937/17 "2002-11-11T15:41:22Z")

</div>

You might want to take a look at some of the tutorials at Bit-101. There’s couple of fairly advanced tutorials, including gravity and easing, which is somewhat similar to what you want to do. Here’s the url:  
[http://www.bit-101.com/](http://www.bit-101.com/)  
Hope this helps!  
The Kza

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 11, 2002, 3:46pm UTC](https://forum.kirupa.com/t/using-as-to-perform-animation/7937/18 "2002-11-11T15:46:25Z")

</div>

thanks the\_Kza!

These tutorials look really good!

This forum’s excellent!

Nice to hear from people who know what they are talking about, instead of the idiots i have to deal with at work.

thanks to all of you, see you around kirupa

Cheers
