Gravity

A variation of the gravity with throwable ball experiment. It has optional gravity.

Click the ball to drag and release to drop or throw it.
Press the space bar to add more balls (up to 30).
Press the down arrow to turn gravity off, and pres the up arrow to turn it back on.

You can make some pretty interesting movements. The source fla file is at the bottom of the post. I learned most of this from Keith Peter’s gravity tutorial.

[kml_flashembed publishmethod=”dynamic” fversion=”9.0.0″ movie=”https://circlecube.com/circlecube/wp-content/uploads/sites/10/2007/03/gravity07.swf” width=”550″ height=”550″ targetclass=”flashmovie”]

Get Adobe Flash player

[/kml_flashembed]

Actionscript (as2)

[cc lang=”actionscript” tab_size=”2″ lines=”40″]
//global variables initialized in frame 1
gravity=2;
drag=.99;
bounce=.8;
_root.id = 1;

//on the ball
onClipEvent (load) {
//random placement
this._x = random(Stage.width);
this._y = random(Stage.height);
//random x and y speed
this.xspeed = Math.random() * 30;
this.yspeed = Math.random() * 30;
//give this ball an id
this.ballNum = _root.id;
//increment id until 30 and then reset
if (_root.id < 30) {
_root.id++;
}
else {
_root.id = 2;
}
}
//drag ball on press
on (press) {
startDrag(this);
this.dragging = true;
}
//create new ball with space bar
on (keyPress “<Space>”) {
if (this.ballNum == 1) {
duplicateMovieClip(_root.ball, “ball” + _root.id, _root.id);
}
}
//toggle gravity with up and down arrow
on (keyPress “<Down>”) {
_root.gravity = 0;
}
on (keyPress “<Up>”) {
_root.gravity = 2;
}
//drop on release
on (release, releaseOutside) {
stopDrag();
this.dragging = false;
}
onClipEvent (enterFrame) {
//if not dragging
if (!this.dragging) {
//calculate new x and y position
this._y = this._y + this.yspeed;
this._x = this._x + this.xspeed;
//bounce off the bottom of stage and reverse yspeed
if (this._y + this._height / 2 > Stage.height) {
this._y = Stage.height – this._height / 2;
this.yspeed = -this.yspeed * _root.bounce;
}
//bounce off the top of stage and reverse yspeed
if (this._y – this._height / 2 < 0) {
this._y = this._height / 2;
this.yspeed = -this.yspeed * _root.bounce;
}
//bounce off right of stage
if (this._x + this._width / 2 > Stage.width) {
this._x = Stage.width – this._width / 2;
this.xspeed = -this.xspeed * _root.bounce;
}
//bounce off left of stage
if (this._x – this._width / 2 < 0) {
this._x = this._width / 2;
this.xspeed = -this.xspeed * _root.bounce;
}
//recalculate x and y speeds figuring in drag (friction) and gravity for y
this.yspeed = this.yspeed * _root.drag + _root.gravity;
this.xspeed = this.xspeed * _root.drag;
}
else {
//if dragging then calculate new speeds according to dragging movement and speed
this.xspeed = this._x – this.oldx;
this.yspeed = this._y – this.oldy;
this.oldx = this._x;
this.oldy = this._y;
}
}
[/cc]

Here is the gravity.fla to download and play with.

Drag Line

Actionscript exercise with a line connecting movable movieClips.

Flash Example

[kml_flashembed publishmethod=”dynamic” fversion=”9.0.0″ movie=”https://circlecube.com/circlecube/wp-content/uploads/sites/10/2007/02/dragline.swf” width=”550″ height=”550″ targetclass=”flashmovie”]

Get Adobe Flash player

[/kml_flashembed]

actionscript

[cc lang=”actionscript” tab_size=”2″ lines=”40″]
// get x and y coordinates of the circles
var ax = _root.point1._x;
var ay = _root.point1._y;
var bx = _root.point2._x;
var by = _root.point2._y;
var cx = _root.point3._x;
var cy = _root.point3._y;

// draw line between them
_root.createEmptyMovieClip (“line”, 0);
with (_root.line){
lineStyle (1, 0x999999, 100);
moveTo (ax, ay);
lineTo (bx, by);
lineTo (cx, cy);
lineTo (ax, ay);
}
[/cc]

Download

dragLine.fla

Multiple Targets

An actionscript exercise. Trying to get a movie clip to go to the closest dynamically created target. Click the button to make more targets. Dragable targets. Move the targets around to see the ball’s target change.

Example

[kml_flashembed publishmethod=”dynamic” fversion=”9.0.0″ movie=”https://circlecube.com/circlecube/wp-content/uploads/sites/10/2007/02/multipletargets.swf” width=”550″ height=”550″ targetclass=”flashmovie”]

Get Adobe Flash player

[/kml_flashembed]

actionscript

Here’s the actioscript for the little ball:
[cc lang=”actionscript” tab_size=”2″ lines=”40″]
onClipEvent (load) {
//random placement
_x = Math.random()* 450;
_y = Math.random()* 450;
//speed is speed, higher number is slower movement
speed = (Math.random()*5)+7;
//x and y distance from target
xdist = 0;
ydist = 0;
}

onClipEvent (enterFrame) {
//determine which target is closer a^2+b^2=c^2
//use a for loop to cycle through all targets,
//and assign the closest to be THE target
for (j=0; j <= _root.i; j++) {
//for the first automatically assign it as the closest
if (j == 0) {
xdist = _root.target0._x – _x;
ydist = _root.target0._y – _y;
}
//find the distance to next target
else {
//temporary distance variables
xdistT = _root[“target”+j]._x – _x;
ydistT = _root[“target”+j]._y – _y;
//if it is closer assign it to the closest, if not do nothing
if(Math.sqrt((xdist*xdist) + (ydist*ydist)) > Math.sqrt((xdistT*xdistT) + (ydistT*ydistT))) {
xdist = xdistT;
ydist = ydistT;
}
}
}

//move toward the selected target
_x += xdist/speed;
_y += ydist/speed;

//if target reached go to another random place
//if xdist is less than 2 and ydist is less than 2 and xdist is greater than 2 and ydist is greater than 2
if(xdist < 2 && ydist < 2 && xdist > -2 && ydist > -2) {
_x = Math.random() * 450;
_y = Math.random() * 450;
}
}
[/cc]

Source Flash File

download multipleTargets.fla

Mothorax

A website with an alternative navigation philosophy. The site is a game you must “play” in order to see the content. Use the arrow keys to move around.

[kml_flashembed publishmethod=”dynamic” fversion=”9.0.0″ movie=”https://circlecube.com/circlecube/wp-content/uploads/sites/10/2007/02/mothoraxnomusic.swf” width=”550″ height=”550″ targetclass=”flashmovie”]

Get Adobe Flash player

[/kml_flashembed]

Sniff

A nice effect. Do you smell something funny? Click to attract the nose. Achieved with an animated mask over a movie clip that grows to reveal the face. And generating new layers of the face continuously!

[kml_flashembed publishmethod=”dynamic” fversion=”9.0.0″ movie=”https://circlecube.com/circlecube/wp-content/uploads/sites/10/2007/02/snif.swf” width=”550″ height=”550″ targetclass=”flashmovie”]

Get Adobe Flash player

[/kml_flashembed]