Using AS to perform animation

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!

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

Ty this…

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

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!

something like:

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

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

yours,
h88

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

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

sorry :cyclops:

yours,
h88

thanks h88, and beta-x for your help.

look forward to seeing what you guys come up with,

cheers!

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


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;
	}
}

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

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

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…



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”;

Yep, that works :slight_smile:

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

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!

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

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

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/
Hope this helps!
The Kza

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