PDA

View Full Version : 5 circles animating to random locations on stage, but must not touch each other...


ThreeDollarBill
08-13-2009, 08:04 AM
THE BASIC IDEA:
add 5 circles on random locations on stage...
and animate them to random locations on stage without touching each other...

THE PROBLEM:
the code below works perfectly (up to a certain point)...
it succeeds in making the circles animate to random points on stage...
it also succeeds in detecting if once circle comes to close proximity with another circle... only problem is, i dont know what i should do next...

code on main timeline:
var enemyArray:Array = new Array();
var timeToCreateEnemy:Timer;

// set up timer that will fire every 2 seconds, 5 times
timeToCreateEnemy = new Timer(2000, 5)
timeToCreateEnemy.addEventListener(TimerEvent.TIME R, createEnemy);
timeToCreateEnemy.start();

// function that will create an enemy when timer fires
function createEnemy(event:TimerEvent):void
{
var enemyShip:MovieClip = new Enemy();
enemyArray.push(enemyShip); // put this enemy into the array
enemyShip.x = Math.round(Math.random() * stage.stageWidth);
enemyShip.y = Math.round(Math.random() * stage.stageHeight);
addChild(enemyShip);
}

code on Enemy class:
package
{
import flash.display.*;
import flash.events.*;
import flash.utils.Timer;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;

public class Enemy extends MovieClip
{
private var timeToMove:Timer;
private var indexOfThis:int;
private var randomXPosition:Number;
private var randomYPosition:Number;
private var thisSpeed:Number;
private var xDistanceFromOtherEnemy:Number;
private var yDistanceFromOtherEnemy:Number;
private var distanceFromOtherEnemy:Number;
private var moveXTween:Tween;
private var moveYTween:Tween;
private var thisRadius:Number;

public function Enemy()
{
initEnemy();
}

function initEnemy():void
{
// set up some variables
thisSpeed = 200; // speed
thisRadius = this.width; // radius of this enemy

// set up timer that runs the moveEnemy function
timeToMove = new Timer(3000, 1);
timeToMove.addEventListener(TimerEvent.TIMER, moveEnemy);
timeToMove.start();

// add an enterframe event listener
this.addEventListener(Event.ENTER_FRAME, runEnterFrame);
}

function runEnterFrame(event:Event):void
{
// test if enemy is in close proximity with another enemy
if(MovieClip(root).enemyArray.length > 0)
{
for(var i:Number = 0; i < MovieClip(root).enemyArray.length; i++)
{
xDistanceFromOtherEnemy = this.x - MovieClip(root).enemyArray[i].x;
yDistanceFromOtherEnemy = this.y - MovieClip(root).enemyArray[i].y;
distanceFromOtherEnemy = Math.sqrt( Math.pow(xDistanceFromOtherEnemy, 2) + Math.pow(yDistanceFromOtherEnemy, 2) );
var otherEnemyRadius:Number = MovieClip(root).enemyArray[i].width / 2;
if(distanceFromOtherEnemy <= thisRadius + otherEnemyRadius + 10 && MovieClip(root).enemyArray[i] != this)
{
trace("this enemy is about to collide with another enemy");
// need to put some kind of code here...
/* i already tried:

moveXTween.stop();
moveYTween.stop();
timeToMove.start();

...and i get weird results / errors */

}
}
}
}

// set up a Tween and begin moving
function moveEnemy(event:TimerEvent):void
{
// set a random target position and animate the enemy towards it
randomXPosition = Math.round(Math.random() * stage.stageWidth);
randomYPosition = Math.round(Math.random() * stage.stageHeight);
var dist = Math.sqrt(Math.pow(Math.abs(randomXPosition - this.x),2) + Math.pow(Math.abs(randomYPosition - this.y),2));
var time = dist / thisSpeed;
moveXTween = new Tween(this, "x", None.easeNone, this.x, randomXPosition, time, true);
moveYTween = new Tween(this, "y", None.easeNone, this.y, randomYPosition, time, true);

// set up a listener for when the tween finishes; if so, restart the timer
moveYTween.addEventListener(TweenEvent.MOTION_FINI SH, restartTimer);
}

// funtion that restarts timer
function restartTimer(event:TweenEvent):void
{
timeToMove.start();
}
}
}

flyingbuddha
08-13-2009, 08:22 AM
Can you not use hitTestObject where you've put..
// need to put some kind of code here...

ThreeDollarBill
08-13-2009, 08:36 AM
Can you not use hitTestObject where you've put..
// need to put some kind of code here...
i need to put some kind of code there that will stop the current Tween so it wont collide with the other enemy... and then begin moving it again to a different direction...

ThreeDollarBill
08-13-2009, 04:33 PM
no one?

lordofduct
08-13-2009, 05:57 PM
well what is the reaction of it you want???

A) you can decide a straight line route to take before performing the animation that don't intersect 3 dimensionally (x,y,time) by setting parametric equations of each equal to each other and solving for them and inferring that they must be undefined.

B) you can just select random points and IF they come in proximity they react... but how do you want them to react??? Does one stop and wait for the other to pass. Or do they bounce off each other? Or what, what do you want to happen?

ThreeDollarBill
08-13-2009, 06:36 PM
well what is the reaction of it you want???

A) you can decide a straight line route to take before performing the animation that don't intersect 3 dimensionally (x,y,time) by setting parametric equations of each equal to each other and solving for them and inferring that they must be undefined.

B) you can just select random points and IF they come in proximity they react... but how do you want them to react??? Does one stop and wait for the other to pass. Or do they bounce off each other? Or what, what do you want to happen?

it would be nice if they could bounce off each other / go the opposite direction..
im down for anything actually.. as long as the movieclips dont collide with each other..
as you can see i've tried to write a code that stops the tweens but that didnt work out too well...