flash animation video tutorials

 

 

 

START LEARNING
FLASH NOW

Get instant access to over
45 minutes of FREE Flash tutorials on video
 and our newsletter.

Name:
E-Mail:
Phone:
Describe

.

.

.

 
Web video-animation.com

.

.

Flash MX2004 - Falling Triangle Physics

This is a commented script by Deamothul. He has been studying Physics and advanced Actionscript rather thoroughly and from the looks of this code, he has come up with some amazing stuff. This has been written in Flash Actionscript 2.0 and utilises a class , the Vector3D(v3d.as) class that we featured in an earlier tutorial.
Go here for Actionscript 2.0 Vector3D class
The v3d.as file and this fla you will make must go in the same folder.
The code is fully commented, so just read away.
Go here to have a look at it.

Flash Tutorials in Video Format - Watch them now at LearnFlash.com  

You will need 2 frames. The first frame has this code on it :

// This scripts needs the v3d.as class
// The Math.random's are there because 
// onMouseDown triggers a reload of the script and 
// thus a different triangle  everytime.
stop();
// Point 1
//Create a movieclip that represents a point of the triangle. 
this.createEmptyMovieClip("p1", 1);
//Initialise the position vector
p1.pos = new v3d(0 + Math.random()*200,0,0);
//Init the initial velocity vector.
p1.u = new v3d(0,0,0);					
//Init the velocity vector.
p1.v = new v3d(0,0,0);
//Init the acceleration vector.
p1.a = new v3d(0,0,0);						
//Init the displacement vector.
p1.s = new v3d(0,0,0);						
//Init s forceA vector.
p1.forceA = new v3d(0,0,0);
//Init the friction scalar.
p1.friction = 0.80+Math.random()*0.1;
//Set the bounce factor.
p1.bounceFactor = .78

//Point 2
this.createEmptyMovieClip("p2",2);
p2.pos = new v3d(200+Math.random()*200,100,0);
p2.u = new v3d(0,0,0);		
p2.v = new v3d(0,0,0);								
p2.a = new v3d(0,0,0);								
p2.s = new v3d(0,0,0);								
p2.forceA = new v3d(0,0,0);
p2.friction = 0.70+Math.random()*0.31;
p2.bounceFactor = .82

// point 3 
this.createEmptyMovieClip("p3",3); 
p3.pos = new v3d(400+Math.random()*100,0,0);
p3.u = new v3d(0,0,0);
p3.v = new v3d(0,0,0);								
p3.a = new v3d(0,0,0);								
p3.s = new v3d(0,0,0);								
p3.forceA = new v3d(0,0,0);
p3.friction = 0.70+Math.random()*0.11;
p3.bounceFactor = .88;

// initialise the gravity vector -
// always pointing down the yaxis (up in flash)
gravity = new v3d(0,0.7,0);
// init the netForce which will cause the 
// acceleration on the object, 
// a netForce is the sum of forces
netForce = new v3d(0,0,0);

//Execute this code block  every frame
_root.onEnterFrame = function(){
//Clear the  stage before drawing new stuff.
	_root.clear();
//Set the fill color to dark red with a 100 percent alpha.
	//_root.beginFill(0xdd0000,100)
// Set how the line must look, i set it to a 6 pixels white 
// line with an alpha of 100
	_root.lineStyle(6,0xffffff,100);
//Moves to the first point's x and y position
	_root.moveTo(p1.pos.x,p1.pos.y);
//Draws the lines of the triangle.
	_root.lineTo(p2.pos.x,p2.pos.y);
	_root.lineTo(p3.pos.x,p3.pos.y);
	_root.lineTo(p1.pos.x,p1.pos.y);
//End the fill.
	//_root.endFill();
	
// Add the gravity to the net force to accelerate 
// objects down just as gravity does.
	netForce.addVec(gravity);
	
// point 1 physics calcs
//Add the netForce to the accceleration causing the movement.
	p1.a.addVec(netForce);
//Add the acceleration to the velocity.
	p1.v.addVec(p1.a);
// Scale the velocity down by (1 - friction),
// thus scaling the velocity down by a certain percentage.
	p1.v.scaleVec(1-p1.friction);
// Finally add the velocity to the position to cause 
// the the position to change if the velocity changes.
	p1.pos.addVec(p1.v);

// point 2 physics calcs 
	p2.a.addVec(netForce);
	p2.v.addVec(p2.a);
	p2.v.scaleVec(1-p2.friction);
	p2.pos.addVec(p2.v);

// point 3 physics
	p3.a.addVec(netForce);
	p3.v.addVec(p3.a);
	p3.v.scaleVec(1-p3.friction);
	p3.pos.addVec(p3.v);

//1st point
//If the y of the position vector is larger than 500
//( the ground), execute the code block 
	if(p1.pos.y>500){
//Set y to the number defined as ground.
		p1.pos.y = 500;
// Scale the acceleration vector down by the -bounce factor.
// a = -k*d ,which has the effect of slowing the object down 
// until it is at zero velocity.
		p1.a.scaleVec(-p1.bounceFactor);
	}
	
// second point
	if(p2.pos.y>500){
	p2.pos.y = 500;	
	p2.a.scaleVec(-p2.bounceFactor);
	}
	
// 3 rd point
	if(p3.pos.y>500){
	p3.pos.y = 500;
	p3.a.scaleVec(-p3.bounceFactor);
	}
	
//render the points  to screen.
	p1._x = p1.pos.x;
	p1._y = p1.pos.y;
	p2._x = p2.pos.x;
	p2._y = p2.pos.y;
	p3._x = p3.pos.x;
	p3._y = p3.pos.y;
};
//Trigger a reload of the script 
_root.onMouseDown=function(){ 
//	Goto and play frame 2, 
// there is only a gotoAndStop(1) 
// on frame 2 so the script will reload
	gotoAndPlay(2); 
}



If you are having trouble with your website, Albany web design is a great idea.  Whether you need a website usability analysis or some custom applications, the web will help you meet all your needs.


On frame 2 of the fla, just put this code :

	gotoAndStop(1);

flash 8
.