Pushing, random arrangement

Code Actionscript 2.0

Frame action

onClipEvent (load) { speedx = 0; speedy = 0; } onClipEvent (enterFrame) { speedx = speedx / 1.1; speedy = speedy / 1.1; this._x = this._x + speedx; this._y = this._y + speedy; } on (rollOver) { speedx = (this._x - _root._xmouse) * 2; speedy = (this._y - _root._ymouse) * 2; }

Instance action

for (i = 1; i < 64; i = i + 1) { square0.duplicateMovieClip("square" + i, i); this["square" + i]._x = random(400); this["square" + i]._y = random(400); }

Code Actionscript 3.0

var Squares:Array = new Array(); var Speedsx:Array = new Array(); var Speedsy:Array = new Array(); var i=1; var targetSquare= null; for (i = 1; i < 64; i = i + 1) { //important: square has to be exported for actionscript //go to "linkage" in library menu Squares[i] = new squareObject(); Squares[i].addEventListener( Event.ENTER_FRAME, enterFrame); Squares[i].addEventListener( MouseEvent.MOUSE_OVER, rollOver); Squares[i].x = int(Math.random()*400); Squares[i].y = int(Math.random()*400); Squares[i].id = i; addChild(Squares[i]); Speedsx[i] = 0; Speedsy[i] = 0; } square0.visible = false; function enterFrame(e:Event) { if(targetSquare != null){ var myid = e.target.id; Speedsx[myid] = Speedsx[myid] / 1.1; Speedsy[myid] = Speedsy[myid] / 1.1; e.target.x = e.target.x + Speedsx[myid]; e.target.y = e.target.y + Speedsy[myid]; } } function rollOver(e:MouseEvent) { targetSquare = e.target; var myid = e.target.id Speedsx[myid] = (e.target.x- mouseX) * 2; Speedsy[myid] = (e.target.y - mouseY) * 2; }

Description

The squares are arranged randomly. When touched by the mouse, the are pushed away. This code is a combination of two other examples. In Actionscript 2, "random arrangement" consists of a frame-action and "mouse pushing" of an instance-action. The code from "mouse pushing" is represented in enterFrame and rollOver, and the code from "random arrangement" is represented in the for-loop.

related to: Mouse pushing, Random arrangement

Download

Right click: Flashfile AS 2.0 | Flashfile AS 3.0 | SWF-File


Share