Home Tutorials Forums Articles Blogs Movies Library Employment Press Buy templates

Go Back   ActionScript.org Forums > Community Boards > Just for Kicks Challenges

Reply
 
Thread Tools Rate Thread Display Modes
Old 03-30-2004, 11:52 AM   #1
CnD
Registered User
 
Join Date: Mar 2004
Location: Indjija, Serbia
Posts: 5
Send a message via ICQ to CnD
Cool magnetic fields

I made a very simple code. One ball that runs away from mouse pointer when it comes near the ball.

[as]
var dx:Number=random(9)-5;
var dy:Number=random(9)-5;
var positX:Number;
var positY:Number;
_root.createEmptyMovieClip("circle_mc", 1);
circle_mc._x = 200;
circle_mc._y = 200;
circle_mc.lineStyle(100, 0x125543, 100);
circle_mc.lineTo(1, 0);
positX=this.circle_mc._x;
positY=this.circle_mc._y;
var distan:Number;

onEnterFrame = function()
{
positX+=dx;
positY+=dy;
if (positX<48 || positX>500) {dx=-dx; positX+=dx};
if (positY<50 || positY>350) {dy=-dy; positY+=dy};
distan =Math.sqrt(Math.pow(_xmouse-positX,2) + Math.pow(_ymouse-positY,2));
if (distan<150)
{
if (positX < _xmouse) { dx-=20/Math.exp(distan/30)};
if (positX > _xmouse) { dx+=20/Math.exp(distan/30)};
if (positY < _ymouse) { dy-=20/Math.exp(distan/30)};
if (positY > _ymouse) { dy+=20/Math.exp(distan/30)};
if (dx>10) { dx=10};
if (dx<(-10)) { dx=(-10)};
if (dy>10) { dy=10};
if (dy<(-10)) { dy=(-10)};
};
dx*=.99;
dy*=.99;
this.circle_mc._x=positX;
this.circle_mc._y=positY;
}
[as]


Lets spice it a bit


Cole
CnD is offline   Reply With Quote
Old 03-31-2004, 01:41 AM   #2
liquidj
Registered User
 
Join Date: Mar 2004
Location: White Rock, BC, Canada
Posts: 12
Wink More to play with..

How about this

ActionScript Code:
var iter = 30; for (var i = 0; i<iter; i++) { this.createEmptyMovieClip("circle"+i, i); var clip = this["circle"+i]; var dx:Number = random(9)-5; clip.dx = dx; var dy:Number = random(9)-5; clip.dy = dy; var positX:Number; clip.positX = positX; var positY:Number; clip.positY = positY; clip._x = 200; clip._y = 200; clip.lineStyle(Math.random()*15+10, 0xFFFFCC, 100); clip.lineTo(1, 0); clip.positX = clip._x; clip.positY = clip._y; var distan:Number; clip.distan = distan; clip.onEnterFrame = function() { this.positX += this.dx; this.positY += this.dy; if (this.positX<48 || this.positX>500) { this.dx = -this.dx; this.positX += this.dx; } if (this.positY<50 || this.positY>350) { this.dy = -this.dy; this.positY += this.dy; } this.distan = Math.sqrt(Math.pow(_xmouse-this.positX, 2)+Math.pow(_ymouse-this.positY, 2)); if (this.distan<150) { if (this.positX<_xmouse) { this.dx -= 20/Math.exp(this.distan/30); } if (this.positX>_xmouse) { this.dx += 20/Math.exp(this.distan/30); } if (this.positY<_ymouse) { this.dy -= 20/Math.exp(this.distan/30); } if (this.positY>_ymouse) { this.dy += 20/Math.exp(this.distan/30); } if (this.dx>10) { this.dx = 10; } if (this.dx<(-10)) { this.dx = (-10); } if (this.dy>10) { this.dy = 10; } if (this.dy<(-10)) { this.dy = (-10); } } this.dx *= .99; this.dy *= .99; this._x = this.positX; this._y = this.positY; }; }


My answer to everything... If one is good, lots are better.
liquidj is offline   Reply With Quote
Old 03-31-2004, 08:14 AM   #3
CnD
Registered User
 
Join Date: Mar 2004
Location: Indjija, Serbia
Posts: 5
Send a message via ICQ to CnD
Default

Nice one...respect...

I am at work right now, so I found a little time to make something different.


[as]
iter=50;
for (var i = 0; i<iter; i++)
{
this.createEmptyMovieClip ("circle"+i,i);
var clip = this ["circle"+i];
var color=random(0xFFFFFF);
clip.lineStyle(Math.random()*30+10, color , 100);
clip.lineTo(1, 0);
clip._x=Math.random()*550;
clip._y=Math.random()*400;
clip.ClipScale=clip._xscale;
clip.opa=clip._alpha;
clip.dScale=Math.random()*3;
clip.dx=Math.random(10)*-5;
clip.dy=Math.random(10)*-5;

clip.onEnterFrame =function()
{
if (this.ClipScale <= 30) {this.ClipScale=30};
this._xscale = this.ClipScale;
if (this.ClipScale==30)
{
this._x=Math.random()*550;
this._y=Math.random()*400;
this.ClipScale=100;
this._alpha=100;
};
this.ClipScale-=this.dScale;
this._alpha=(this._xscale-30)*10/7;
this._x-=this.dx;
this._y-=this.dy;
var dist:Number;
dist=Math.sqrt(Math.pow(_xmouse-this._x, 2)+Math.pow(_ymouse-this._y, 2));
if (dist<150)
{
if (this._x<_xmouse) {this.dx += 20/Math.exp(this.dist/30);}
if (this._x>_xmouse) {this.dx -= 20/Math.exp(this.dist/30);}
if (this._y<_ymouse) {this.dy += 20/Math.exp(this.dist/30);}
if (this._y>_ymouse) {this.dy -= 20/Math.exp(this.dist/30);}
if (this.dx>10) {this.dx = 10;}
if (this.dx<(-10)) {this.dx = (-10);}
if (this.dy>10) {this.dy = 10;}
if (this.dy<(-10)) {this.dy = (-10);}
};
};
}
[as]


Cole

Last edited by CnD; 03-31-2004 at 09:42 AM..
CnD is offline   Reply With Quote
Old 04-14-2004, 08:09 PM   #4
pom
Allez les Bleus!!!
 
pom's Avatar
 
Join Date: Oct 2002
Location: Paname
Posts: 2,495
Default

Since large amounts of code seem to be allowed, here you go:
Code:
_global.root = this ;

this.createEmptyMovieClip("dot", -10) ;
this.dot.lineStyle(5, 0xffffff, 100) ;
this.dot.lineTo(.45, .15) ;
this.dot._visible = 0 ;

min_dist = 100 ;
max_clip = 8 ;
damp = 1 ;
k = .01 ;
left = 0 ;
right = 300 ;
up = 0 ;
down = 300 ;
n_arcs = 2 ;

MovieClip.prototype.electricArc = function (mc1, mc2, alpha, steps, off) {
	for (var n=0; n < n_arcs; n++) {
		this.lineStyle (random(3), 0xffffff, alpha) ;
		this.moveTo (mc1._x, mc1._y) ;
		var dx = (mc2._x - mc1._x) / steps ;
		var dy = (mc2._y - mc1._y) / steps ;
		for (var s=1; s < steps; s++) {
			var x = mc1._x + s*dx + getRandom(off) ;
			var y = mc1._y + s*dy + getRandom(off) ;
			this.lineTo(x, y) ;
		}
		this.lineTo(mc2._x, mc2._y) ;
	}
}
MovieClip.prototype.checkBoundaries = function () {
	if (this._x < left) {
		this._x = left ;
		this.vx *= -1 ;
	}
	if (this._x > right) {
		this._x = right ;
		this.vx *= -1 ;
	}
	if (this._y < up) {
		this._y = up ;
		this.vy *= -1 ;
	}
	if (this._y > down) {
		this._y = down ;
		this.vy *= -1 ;
	}	
}
function getDistance ( mc1, mc2 ) {
	var dx = mc2._x - mc1._x ;
	var dy = mc2._y - mc1._y ;
	return Math.sqrt ( dx*dx + dy*dy ) ;
}
function getRandom (inter) {
	return random(inter) - inter / 2 ; 
}
function move () {
	this._x += this.vx ;
	this._y += this.vy ;
	this.checkBoundaries () ;
	if (this.num == max_clip-1) root.clear() ;
	for (var j=this.num+1; j < max_clip; j++) {
		var cl = mcs[j] ;
		var dx = cl._x - this._x ;
		var dy = cl._y - this._y ;
		var dist = Math.sqrt(dx*dx + dy*dy) ;
		if (dist < min_dist) {
			var angle = Math.atan2(dy, dx) ;
			var tx = this._x + min_dist * Math.cos(angle) ;
			var ty = this._y + min_dist * Math.sin(angle) ;
			this.vx -= (tx - this._x) * k;
			this.vy -= (ty - this._y) * k;
			cl.vx += (tx - this._x) * k;
			cl.vy += (ty - this._y) * k;
		}
		if ( dist <= min_dist*2 ) {
			root.electricArc(this, cl, 100-dist/2, 5, 20) ;
		}
	}
	this.vx *= damp;
	this.vy *= damp;
}

mcs = [] ;

for (var i=0; i < max_clip; i++) {
	var _mc = this.dot.duplicateMovieClip("d"+i, i) ;
	mcs.push(_mc) ;
	_mc.num = i ;
	_mc._x = random(right) ;
	_mc._y = random(down) ;
	_mc.vx = getRandom(10) ;
	_mc.vy = getRandom(10) ;
	_mc.k = k * ( random(2) ? -1 : 1 ) ;
	_mc.onEnterFrame = move ;
}
Mainly inspired by Bit-101's amazing node garden.
pom is offline   Reply With Quote
Old 04-22-2004, 04:37 PM   #5
pom
Allez les Bleus!!!
 
pom's Avatar
 
Join Date: Oct 2002
Location: Paname
Posts: 2,495
Default

Just a simple one, in 19 lines
Code:
//pom
this.createEmptyMovieClip ("_mc", -5) ;
_mc.lineStyle (0, 0, 100) ;
_mc.moveTo (20, -5) ;
_mc.lineTo (20, 5) ;

_mc.onEnterFrame = rotate ;
for (var i = 0 ; i < 70 ; i++) {
	var mc = _mc.duplicateMovieClip ("d" + i, i) ;
	mc._x = random (200) ;
	mc._y = random (100) ;
	mc.onEnterFrame = rotate ;
}

function rotate () {
	var dx = _xmouse - this._x ;
	var dy = _ymouse - this._y ;
	var an = Math.atan2 (dy, dx) ;
	this._rotation = an * 180 / Math.PI + 45 ;
}
pom is offline   Reply With Quote
Old 04-23-2004, 03:06 AM   #6
liquidj
Registered User
 
Join Date: Mar 2004
Location: White Rock, BC, Canada
Posts: 12
Default subtle change on awsome code

I really just modified the last line of the rotate function.
Any random number changes were just to spread the objects out a bit on a larger field.
That last bit of [pom] code is sweet.

Code:
this.createEmptyMovieClip ("_mc", -5) ;
_mc.lineStyle (0, 0, 100) ;
_mc.moveTo (20, -5) ;
_mc.lineTo (20, 5) ;
_mc.onEnterFrame = rotate ;
//
for (var i = 0 ; i < 270 ; i++) {
        var mc = _mc.duplicateMovieClip ("d" + i, i) ;
        mc._x = random (500) ;
        mc._y = random (300) ;
        mc.onEnterFrame = rotate ;
}
//
function rotate () {
        var dx = _xmouse - this._x ;
        var dy = _ymouse - this._y ;
        var an = Math.atan2 (dy, dx) ;
        this._rotation += ((an * 180 / Math.PI + 45)- this._rotation)/7 ;
}
liquidj is offline   Reply With Quote
Old 04-23-2004, 03:26 AM   #7
pom
Allez les Bleus!!!
 
pom's Avatar
 
Join Date: Oct 2002
Location: Paname
Posts: 2,495
Default

Nice
pom is offline   Reply With Quote
Old 04-26-2004, 08:34 AM   #8
vulcanpimp
Registered User
 
Join Date: Aug 2003
Posts: 116
Default

Hey POM

thats actually pretty good...tell me have you used flash before?
vulcanpimp is offline   Reply With Quote
Old 04-26-2004, 08:40 AM   #9
CyanBlue
Super Moderator
 
CyanBlue's Avatar
 
Join Date: Jan 2002
Location: Centreville, VA
Posts: 24,879
Default

Very nice... Love it... That reminds me of the play that I used to do with the magnet when I was young...
__________________
CyanBlue / Jason Je / Macromedia Certified Flash Developer & Designer
http://CyanBlue.FlashVacuum.com
http://www.FlashVacuum.com
http://tutorials.FlashVacuum.com

Do NOT PM, Email or Call me... Your question belongs right in this forum...
CyanBlue is offline   Reply With Quote
Old 04-26-2004, 12:32 PM   #10
pom
Allez les Bleus!!!
 
pom's Avatar
 
Join Date: Oct 2002
Location: Paname
Posts: 2,495
Default

Quote:
Originally Posted by vulcanpimp
Hey POM

thats actually pretty good...tell me have you used flash before?
Before what?
pom is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT. The time now is 04:31 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2009 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.