how to stop particles from moving off stage, Actionscript random particle generator - actionscript-3

Below is some code for a random particle generator. I want to change it so the particles only move a small distance before stopping and not disappearing. As it is now, they keep moving until they go off stage. I'm new to Actionscript so any help would be appreciated, thanks.
var particleArray:Array = new Array();
var maxParticles:Number = 50000;
function addParticle(e:Event)
{
var Symbol:Symbol3 = new Symbol3();
Symbol.alpha = Math.random() * .8 + .2;
Symbol.scaleX = Symbol.scaleY = Math.random() * .8 + .2;
Symbol.xMovement = Math.random() * 10 - 5;
Symbol.yMovement = Math.random() * 10 - 5;
Symbol.x = mouseX;
Symbol.y = mouseY;
particleArray.push(Symbol);
addChild(Symbol);
Symbol.cacheAsBitmap = true;
if (particleArray.length >= maxParticles)
{
removeChild(particleArray.shift());
}
Symbol.addEventListener(Event.ENTER_FRAME,moveParticle);
}
function moveParticle(e:Event)
{
e.currentTarget.x += e.currentTarget.xMovement;
e.currentTarget.y += e.currentTarget.yMovement;
}
var myTimer:Timer = new Timer(50);
myTimer.addEventListener(TimerEvent.TIMER, addParticle);
myTimer.start();

One way to do this would be to remove the event listener after a number executions:
function addParticle(e:Event)
{
var Symbol:Symbol3 = new Symbol3();
Symbol.alpha = Math.random() * .8 + .2;
Symbol.scaleX = Symbol.scaleY = Math.random() * .8 + .2;
Symbol.xMovement = Math.random() * 10 - 5;
Symbol.yMovement = Math.random() * 10 - 5;
// set the number of times this instance can move.
// Using a randomly generated number here
Symbol.movementCount = int(Math.random() * 10);
Symbol.x = mouseX;
Symbol.y = mouseY;
particleArray.push(Symbol);
addChild(Symbol);
Symbol.cacheAsBitmap = true;
if (particleArray.length >= maxParticles)
{
removeChild(particleArray.shift());
}
Symbol.addEventListener(Event.ENTER_FRAME,moveParticle);
}
function moveParticle(e:Event)
{
e.currentTarget.x += e.currentTarget.xMovement;
e.currentTarget.y += e.currentTarget.yMovement;
// decrement your move count, and then check to see if it's hit zero
e.currentTarget.movementCount--;
if(e.currentTarget.movementCount <=0)
{
// if so, remove the event listener
e.currentTarget.removeEventListener(Event.ENTER_FRAME, moveParticle);
}
}

Related

AS3 animate dynamically created movie clips with ENTER_FRAME

I have some code which loads a movie clip from the library, reproduces it and spreads it around the stage in different sizes, positions and rotations. What I can't figure out is how to then animate each one with an ENTER_FRAME event listener - So maybe I can also animate the scale, position and rotations? Any help greatly appreciated. Thanks.
for (var i = 0; i < 20; i++ )
{
//Generate Item from library
var MovieClip_mc:mcMovieClip = new mcMovieClip();
addChild(MovieClip_mc);
//Size
var RandomSize = (Math.random() * 0.5) + 0.5;
MovieClip_mc.scaleX = RandomSize;
MovieClip_mc.scaleY = RandomSize;
//Rotation
var RandomRotation:Number = Math.floor(Math.random()*360);
MovieClip_mc.rotation = RandomRotation;
//Position
MovieClip_mc.x = Math.floor(Math.random() * CanvasWidth);
MovieClip_mc.y = Math.floor(Math.random() * CanvasHeight);
}
For performance benefits, it is better to do it using one ENTER_FRAME handler. The handler can be located in your main class and update each mcMovieClip's properties by calling certain methods declared in the mcMovieClip class. Below is a simple example.
Main class
var mcs:Array = [];
for(var i:int = 0; i < 1; i++)
{
mcs.push(new mcMovieClip());
addChild(mcs[i]);
}
addEventListener(Event.ENTER_FRAME, updateTime);
function updateTime(e:Event):void
{
for(var j:int = 0; j < mcs.length; j++)
{
mcs[j].updatePosition(Math.random() * stage.stageWidth,
Math.random() * stage.stageHeight);
mcs[j].updateRotation(Math.random() * 360);
mcs[j].updateSize(Math.random());
}
}
mcMovieClip class
function updateSize(size:Number):void
{
this.scaleX = this.scaleY = size;
}
function updateRotation(rot:Number):void
{
this.rotation = rot;
}
function updatePosition(newX:Number, newY:Number):void
{
this.x = newX;
this.y = newY;
}
You don't actually need to do that from the outside. You can animate it with its own script in the first frame, so each instance will be animated:
addEventListener(Event.ENTER_FRAME, onFrame);
function onFrame(e:Event):void
{
// Math.random() - 0.5 will produce a random value from -0.5 to 0.5
x += Math.random() - 0.5;
y += Math.random() - 0.5;
scaleX += (Math.random() - 0.5) / 10;
scaleY = scaleX;
rotation += (Math.random() - 0.5) * 5;
}

AS3 How do I make these objects loop endlessly?

Here is my code so far, I am trying to create a top down car game and here is the code I have so far, I am currently trying to get the cars to loop but I am struggling to find a way to do it, please try and help me out with the code or point me in the right direction if you can please and thank you in advance
import flashx.textLayout.utils.CharacterUtil;
var result:Number = Math.random() * 100
var randomX:Number = Math.random() * stage.stageWidth
var background = new Background;
var char = new Char();
var car1 = new Car1();
var car2 = new Car2();
var car3 = new Car3();
var car4 = new Car4();
car1.x = Math.random()* stage.stageWidth;
car2.x = Math.random()* stage.stageWidth;
car3.x = Math.random()* stage.stageWidth;
car4.x = Math.random()* stage.stageWidth;
background.x = 200;
char.x = 700/2;
car1.y = -0;
car2.y = -150;
car3.y = -300;
car4.y = -450;
background.y = 200;
char.y = 450;
addChild(background);
addChild(char);
addChild(car1);
addChild(car2);
addChild(car3);
addChild(car4);
char.gotoAndStop("car");
addEventListener(Event.ENTER_FRAME, fl_EnterFrameHandler);
function fl_EnterFrameHandler(event:Event):void
{
car1.y +=12;
car2.y +=12;
car3.y +=12;
car4.y +=12;
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_PressKeyToMove);
function fl_PressKeyToMove(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.RIGHT:
{
char.x += 15;
if (char.hitTestObject(barrier1))
{
char.x -=15 ;
}
break;
}
case Keyboard.LEFT:
{
char.x -= 15;
if (char.hitTestObject(barrier2))
{
char.x +=15 ;
}
break;
}
}
}
stage.addEventListener(Event.ENTER_FRAME, detectCollision);
function detectCollision(event:Event) {
if(char.hitTestObject(car1))
{
char.gotoAndStop("boom");
}
if(char.hitTestObject(car2))
{
char.gotoAndStop("boom");
}
if(char.hitTestObject(car3))
{
char.gotoAndStop("boom");
}
if(char.hitTestObject(car4))
{
char.gotoAndStop("boom");
}
}
If you're trying to get cars to reposition to the top of the screen as #onekidney guessed, you could easily do so by using the % operator:
function fl_EnterFrameHandler(event:Event):void
{
car1.y = (car1.y + 12) % stage.stageHeight;
car2.y = (car2.y + 12) % stage.stageHeight;
car3.y = (car3.y + 12) % stage.stageHeight;
car4.y = (car4.y + 12) % stage.stageHeight;
}
You can read more about the modulo operator in the documentation

as3 MOUSE_UP stopDrag() not functioning

I am working on a basic as3 slingshot game which uses startDrag() and stopDrag() to let the user pull an object and fire. when the object is not stretching the "elastic" the MOUSE_UP function works as it should, but when it is below the set points and is stretching the string the MOUSE_UP function is not being called.
vars
var gravity = 0.1;
var angle1:Number = 0;
var angle2:Number = 0;
var radius:Number = 1;
var elasticCoefficient:Number = 0.002;
var released:Boolean = true;
var forced:Boolean = false;
var acc:Object = {x:0 , y:0};
var vel:Object = {x:0 , y:0};
var elastic:MovieClip = new MovieClip();
the ENTER_FRAME code is
function doConstantly(e:Event):void
{
acc.x = 0;
acc.y = gravity;
if(released == true)
{
vel.x += acc.x;
vel.y += acc.y;
ball.x += vel.x;
ball.y += vel.y
}
if(ball.y > stage.stageHeight + 500 || ball.y < -50)
{
resetLevel();
}
elastic.graphics.clear();
elastic.graphics.lineStyle(2, 0xFFF2BD);
if(ball.y > point1.y && ball.x < point2.x)
{
forced = true;
var x1:Number = ball.x - point1.x;
var y1:Number = ball.y - point1.y;
var x2:Number = point2.x - ball.x;
var y2:Number = point2.y - ball.y;
var distance1:Number = Math.sqrt(x1 * x1 + y1 * y1);
var distance2:Number = Math.sqrt(x2 * x2 + y2 * y2);
angle1 = Math.atan2(y1,x1);
angle2 = Math.atan2(y2,x2);
var xOffset:Number = Math.cos(angle1 + Math.PI / 2) * radius;
var yOffset:Number = Math.sin(angle1 + Math.PI / 2) * radius;
var xOffset2:Number = Math.cos(angle2 + Math.PI / 2) * radius;
var yOffset2:Number = Math.sin(angle2 + Math.PI / 2) * radius;
angle1 += Math.sin(radius / distance1);
angle2 += Math.sin(radius / distance2) * -1;
elastic.graphics.moveTo(point1.x, point1.y);
elastic.graphics.lineTo(ball.x+xOffset, ball.y+yOffset);
elastic.graphics.moveTo(point2.x, point2.y);
elastic.graphics.lineTo(ball.x+xOffset2, ball.y+yOffset2);
}
else
{
forced = false;
if(forced == true){trace("forced is true")}
if(forced == false){trace("forced is false")}
elastic.graphics.moveTo(point1.x, point1.y);
elastic.graphics.lineTo(point2.x, point2.y);
}
if (released == true && forced == true)
{
acc.x += distance1 * Math.sin(angle2) * elasticCoefficient;
acc.y += - distance1 * Math.cos(angle1) * elasticCoefficient;
acc.x += distance2 * Math.sin(angle1) * elasticCoefficient;
acc.y += - distance2 * Math.cos(angle2) * elasticCoefficient;
vel.x += acc.x;
vel.y += acc.y;
}
}
and the mouse events
function ballMouseDown(event:MouseEvent)
{
//call function to reset level
resetLevel();
//follow mouse
ball.x = mouseX;
ball.y = mouseY;
ball.startDrag();
//set released to false so that gravity wont affect the ball when clicked
released = false;
}
function ballMouseUp(event:MouseEvent)
{
trace("mouse up function called")
released = true; //gravity will affect the ball when released
ball.stopDrag();
}
Thanks.
Try adding the MOUSE_UP handler to the stage instead - at the moment, you will need to release your mouse while it is over the ball which may not be the case.
Update your MOUSE_DOWN handler to attach the listener to the stage:
function ballMouseDown(e:MouseEvent):void
{
// ...your current code.
stage.addEventListener(MouseEvent.MOUSE_UP, ballMouseUp);
}
And removing the listener when the handler is triggered:
function ballMouseUp(e:MouseEvent):void
{
// ...your current code.
stage.removeEventListener(MouseEvent.MOUSE_UP, ballMouseUp);
}

Action Script 3. Spawn objects in different time

I'm creating game where falling 4 different objects from the sky and need to destroy them by clicking mouse button. My problem is that all 4 objects spawns at the same time. I need to make that randomly spawned all 4, but not in the same time and after player have 200 points I need to make that spawned and falled more objects. (I have working counter which counting points).
And one more thing if you know how to make that objects not spawned on one other.
I mean:
Bad spawn.
Here is my constant vars:
public static const GRAVITY:Number = 3;
public static const HIT_TOLERANCE:Number = 50;
//Powerup
public static const APPLE_END_Y:Number = 640;
public static const APPLE_SPAWN_CHANCE:Number = 0.02; //per frame per second
public static const APPLE_START_Y:Number = 110;
public static const APPLE_SPAWN_START_X:Number = 50;
public static const APPLE_SPAWN_END_X:Number = 500;
//Scoring
public static const PLAYER_START_SCORE:Number = 0;
public static const SCORE_PER_APPLE:Number = 10;
Here is part of my code:
public function startGame()
{
speed = C.PLAYER_SPEED;
gravity = C.GRAVITY;
score = C.PLAYER_START_SCORE;
randomChance = C.APPLE_SPAWN_CHANCE;
tol = C.HIT_TOLERANCE;
apples = new Array();
mcGameStage.addEventListener(Event.ENTER_FRAME,update);
}
private function update(evt:Event)
{
//Spawn new apples
if (Math.random() < randomChance)
{
//spawn x coordinates
var newPirmas = new Pirmas();
newPirmas.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X;
var newAntras = new Antras();
newAntras.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X;
var newTrecias = new Trecias();
newTrecias.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X;
var newApple = new Apple();
newApple.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X;
//spawn y coordinates
newPirmas.y = C.APPLE_START_Y;
newAntras.y = C.APPLE_START_Y;
newTrecias.y = C.APPLE_START_Y;
newApple.y = C.APPLE_START_Y;
apples.push(newPirmas);
apples.push(newAntras);
apples.push(newTrecias);
apples.push(newApple);
newPirmas.addEventListener(MouseEvent.CLICK, onClick);
newAntras.addEventListener(MouseEvent.CLICK, onClick);
newTrecias.addEventListener(MouseEvent.CLICK, onClick);
newApple.addEventListener(MouseEvent.CLICK, onClick);
mcGameStage.addChildAt(newPirmas,0);
mcGameStage.addChildAt(newAntras,0);
mcGameStage.addChildAt(newTrecias,0);
mcGameStage.addChildAt(newApple,0);
}
//Move Apples
for (var i = apples.length-1; i >= 0; i--)
{
apples[i].y += gravity;
if (apples[i].y > C.APPLE_END_Y)
{
mcGameStage.removeChild(apples[i]);
apples.splice(i,1);
m_iLives--;
if (!m_iLives)
{
trace("Game Over");
// newApple.removeEventListener(MouseEvent.CLICK, onClick);
break;
}
}
}
txtScore.text = String(score);
}
function onClick(evt:MouseEvent):void{
var apples = evt.target;
apples.visible = false;
apples = tol;
score += C.SCORE_PER_APPLE;
}
}
public function startGame()
{
speed = C.PLAYER_SPEED;
gravity = C.GRAVITY;
score = C.PLAYER_START_SCORE;
randomChance = C.APPLE_SPAWN_CHANCE;
tol = C.HIT_TOLERANCE;
apples = [];
itemsToSpawn = [];
mcGameStage.addEventListener(Event.ENTER_FRAME,update);
nextSpawn:Number = 0;
}
private function update(evt:Event)
{
//Spawn new apples
if (Math.random() < randomChance)
{
//spawn x coordinates
var newPirmas = new Pirmas();
newPirmas.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X;
var newAntras = new Antras();
newAntras.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X;
var newTrecias = new Trecias();
newTrecias.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X;
var newApple = new Apple();
newApple.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X;
//spawn y coordinates
newPirmas.y = C.APPLE_START_Y;
newAntras.y = C.APPLE_START_Y;
newTrecias.y = C.APPLE_START_Y;
newApple.y = C.APPLE_START_Y;
newPirmas.addEventListener(MouseEvent.CLICK, onClick);
newAntras.addEventListener(MouseEvent.CLICK, onClick);
newTrecias.addEventListener(MouseEvent.CLICK, onClick);
newApple.addEventListener(MouseEvent.CLICK, onClick);
//add items to itemsToAdd
itemsToAdd.push(newPirmas, newAntras, newTrecias, newApple);
}
//Move Apples
for (var i = apples.length-1; i >= 0; i--)
{
apples[i].y += gravity;
if (apples[i].y > C.APPLE_END_Y)
{
mcGameStage.removeChild(apples[i]);
apples.splice(i,1);
m_iLives--;
if (!m_iLives)
{
trace("Game Over");
// newApple.removeEventListener(MouseEvent.CLICK, onClick);
break;
}
}
}
txtScore.text = String(score);
if( itemsToSpawn.length > 0 && getTimeout() > nextSpawn )
{
var item:DisplayObject = itemsToAdd.shift();
apples.push(item);
mcGameStage.addChild(item);
nextSpawn = getTimeout() + Math.random() * 4000;
}
}
function onClick(event:MouseEvent):void
{
var apples = event.currentTarget;
apples.visible = false;
apples = tol;
score += C.SCORE_PER_APPLE;
if(score % 100 == 0)
{
gravity += 10;
}
}
In your update code, you're checking a single time for a random value to be less than randomChance, and when it is, you spawn 4 apples.
If you want them to spawn at different times, you would call them one at a time. I would make a function to create a randomly placed apple, and call that whenever your if statement is satisfied.
Eg:
private function update(evt:Event){
if(Math.random() < randomValue){
createApple();
}
Where createApple() would create a new apple, position it, add it to the array, and add it to the stage.
If you're trying to create a different apple each time, you can put the different types in an array and iterate through it as you create new apples.
EG.
private function createApple(){
...
var newApple = appleToSpawn[currentApple]
...
}
Where appleToSpawn is an array of apple objects, and currentApple is an index of which apple you're currently on.
Alternatively, you could do a series of comparisons to a random number, and spawn a different apple depending on which condition is satisfied.
As for making sure they don't overlap, you can keep a history of their spawn points, and change how you get their random X value. Just iterate through the array of previous X values, and make sure the new one isn't within (oldX + (width/2)).
Now for spawning more than 4 as you get more points, just change your randomChance to a bigger number as you go. If it's a bigger number, you'll satisfy your conditional statement more often, therefor spawning more apples.

Physics angular bounce as3

Below is my current code and i am trying to get my ball to bounce off with the proper angle of refraction based on the random angled walls and direction of the ball...My problem is I have only taken a basic physics class and know the equation "angle of incidence = angle of refraction" also, this is my first year in as3 so my coding is rather crude. The angle seems to be off...the problem is with the code "Bullet.hitTestObject(myblockadeHolder[t])" Thanks guys.
stage.addEventListener(Event.ENTER_FRAME,rotate);
var _trueRotation:Number;
var _dx:Number;
var _dy:Number;
function rotate (e:Event){
// calculate rotation based on mouse X & Y
_dx = Turret.x - stage.mouseX;
_dy = Turret.y - stage.mouseY;
// which way to rotate
var rotateTo:Number = getDegrees(getRadians(_dx, _dy));
// keep rotation positive, between 0 and 360 degrees
if (rotateTo > Turret.rotation + 180) rotateTo -= 360;
if (rotateTo < Turret.rotation - 180) rotateTo += 360;
// ease rotation
_trueRotation = (rotateTo - Turret.rotation - 90) / 3;
// update rotation
Turret.rotation += _trueRotation;
}
//Turret Rotation
//Create an array to hold multiple sprites
var mySpriteHolder:Array = [];
//Create a counter to keep track of the number of sprites
var lbCounter:int = 0;
//Maximum number of sprites on the canvas
var maxLB:int = 1;
//Keypress Code
stage.addEventListener(MouseEvent.CLICK, dropBullet);
//Function for the mouse event to fire bullet
function dropBullet(evt:MouseEvent):void{
var bcos:Number = Math.cos((Turret.rotation - 90) * Math.PI / 180);
var bsin:Number = Math.sin((Turret.rotation - 90) * Math.PI / 180);
//starting x and y
var startx:int = Turret.x + (70 * bcos);
var starty:int = Turret.y + (70 * bsin);
//calculates where the bullet needs to go by aiming in front of the gun
var endx:int = Turret.x + (100 * bcos);
var endy:int = Turret.y + (100 * bsin);
var Bullet:MovieClip = new bullet();
Bullet.x = startx;
Bullet.y = starty;
Bullet.xspeed = (endx - startx)/5;
Bullet.yspeed = (endy - starty)/5;
mySpriteHolder.push(Bullet);
stage.addChild(Bullet);
//this calls the move down function
stage.addEventListener(Event.ENTER_FRAME,BulletFire);
}
//Function to shoot bullet
var Points:Number = 0;
var Life:Number = 100;
stage.addEventListener(Event.ENTER_FRAME, TextCounter);
function BulletFire(evt:Event):void{
var Bullet:MovieClip;
//Use a for loop to move the Bullets
for(var i:int=mySpriteHolder.length-1; i>=0; i--){
Bullet = mySpriteHolder[i];
//Bounds Collision
if(Bullet.hitTestObject(Up)){
Bullet.yspeed*=-1;
}
if(Bullet.hitTestObject(Lower)){
Bullet.yspeed*=-1;
}
if(Bullet.hitTestObject(Left)){
Bullet.xspeed*=-1;
}
if(Bullet.hitTestObject(Right)){
Bullet.xspeed*=-1;
}
if(Bullet.hitTestObject(Tank)){
stage.removeChild(Bullet);
mySpriteHolder.splice(i,1);
lbCounter --;
Life -= 10;
}
//Blockade Collision
for(var t in myBlockadeHolder){
if(Bullet.hitTestObject(myBlockadeHolder[t])){
_trueRotation*=2
var newAngle = (180 - (_trueRotation) - (smallangle))*-1;
var newXspeed = Math.cos(newAngle);
var newYspeed = Math.sin(newAngle);
Bullet.xspeed = newXspeed+2.5;
Bullet.yspeed = newYspeed+2.5;
}
}
//Target Collision
for(var c in mytargetHolder){
if(Bullet.hitTestObject(mytargetHolder[c])){
stage.removeChild(Bullet);
mySpriteHolder.splice(i,1);
lbCounter --;
Points += 10;
mytargetHolder[c].y = Math.random()*380 + 10;
mytargetHolder[c].x = Math.random()*380 + 10;
while(mytargetHolder[c].hitTestObject(Turret)){
mytargetHolder[c].y = Math.random()*380 + 10;
mytargetHolder[c].x = Math.random()*380 + 10;
}
}
for(var a in mytargetHolder){
for(var s in mytargetHolder){
while(mytargetHolder[a].hitTestObject(mytargetHolder[s])&& a!=s){
mytargetHolder[a].y = Math.random()*380 + 10;
mytargetHolder[a].x = Math.random()*380 + 10;
}
}
for(var g in myBlockadeHolder){
while(mytargetHolder[a].hitTestObject(myBlockadeHolder[g])&& a!=g){
mytargetHolder[a].y = Math.random()*380 + 10;
mytargetHolder[a].x = Math.random()*380 + 10;
}
}
}
}
Bullet.y += Bullet.yspeed;
Bullet.x += Bullet.xspeed;
}
}//Bullet Code
stage.addEventListener(Event.ENTER_FRAME, HealthCheck);
function HealthCheck(e:Event):void{
if(Life<=0){
stage.removeEventListener(MouseEvent.CLICK, dropBullet);
stage.removeEventListener(Event.ENTER_FRAME, BulletFire);
stage.removeEventListener(Event.ENTER_FRAME, droptarget);
stage.removeEventListener(Event.ENTER_FRAME, dropblockade);
}
}//Health Code
//variables for blockade
var myblockadeSprite:Sprite;
//blockade is the linkage name in the library
var blockade:Blockade;
//Create an array to hold multiple sprites
var myBlockadeHolder:Array = new Array();
//Create a counter to keep track of the number of sprites
var LbCounter:int = 0;
//Maximum number of sprites on the canvas
var maxlb:int = 6;
//Keypress Code
stage.addEventListener(Event.ENTER_FRAME, dropblockade);
//Function for the mouse event to fire blockade
function dropblockade(evt:Event):void{
for(var i:int=0;i<maxlb; i++){
//PLACE DO LOOP INSIDE TO GENERATE EMPTY IN RANDOM COORDS
//add the blockades to the canvas
myblockadeSprite = new Sprite();
stage.addChild(myblockadeSprite);
//Get the actual picture from the library
blockade = new Blockade();
myblockadeSprite.addChild(blockade);
//Going to load up the array with the sprites
myBlockadeHolder[i] = myblockadeSprite;
myBlockadeHolder[i].y = Math.random()*390 + 10;
myBlockadeHolder[i].x = Math.random()*390 + 10;
myBlockadeHolder[i].rotation = Math.random()*360;
while(myBlockadeHolder[i].hitTestObject(Tank)){
myBlockadeHolder[i].y = Math.random()*390 + 10;
myBlockadeHolder[i].x = Math.random()*390 + 10;
}
}
for(var t:int=0;t<maxlb; t++){
for(var d:int=0;d<maxlb; d++){
while(myBlockadeHolder[t].hitTestObject(myBlockadeHolder[d])&& t!=d){
myBlockadeHolder[t].y = Math.random()*390 + 10;
myBlockadeHolder[t].x = Math.random()*390 + 10;
}
}
}
stage.removeEventListener(Event.ENTER_FRAME, dropblockade);
}//Blockade Code
//variables for target
var mytargetSprite:Sprite;
//target is the linkage name in the library
var target:Target;
//Create an array to hold multiple sprites
var mytargetHolder:Array = new Array();
//Create a counter to keep track of the number of sprites
var TargetCounter:int = 0;
//Maximum number of sprites on the canvas
var maxtrgs:int = 3;
//Keypress Code
stage.addEventListener(Event.ENTER_FRAME, droptarget);
function droptarget(evt:Event):void{
for(var i:int=0;i<maxtrgs; i++){
//PLACE DO LOOP INSIDE TO GENERATE EMPTY IN RANDOM COORDS
//add the targets to the canvas
mytargetSprite = new Sprite();
stage.addChild(mytargetSprite);
//Get the actual picture from the library
target = new Target();
mytargetSprite.addChild(target);
//Going to load up the array with the sprites
mytargetHolder[i] = mytargetSprite;
mytargetHolder[i].y = Math.random()*390 + 10;
mytargetHolder[i].x = Math.random()*390 + 10;
while(mytargetHolder[i].hitTestObject(Tank)){
mytargetHolder[i].y = Math.random()*390 + 10;
mytargetHolder[i].x = Math.random()*390 + 10;
}
}
for(var t:int=0;t<maxtrgs; t++){
for(var d:int=0;d<maxtrgs; d++){
while(mytargetHolder[t].hitTestObject(mytargetHolder[d])&& t!=d){
mytargetHolder[t].y = Math.random()*390 + 10;
mytargetHolder[t].x = Math.random()*390 + 10;
}
}
for(var w:int=0;w<maxtrgs; w++){
while(mytargetHolder[t].hitTestObject(myBlockadeHolder[w])&& t!=w){
mytargetHolder[t].y = Math.random()*390 + 10;
mytargetHolder[t].x = Math.random()*390 + 10;
}
}
}
stage.removeEventListener(Event.ENTER_FRAME, droptarget);
}//Target Code
function getRadians(delta_x:Number, delta_y:Number):Number{
var r:Number = Math.atan2(delta_y, delta_x);
if (delta_y < 0){
r += (2 * Math.PI);
}
return r;
}
/**
* Get degrees
* #param radians Takes radians
* #return Returns degrees
*/
function getDegrees(radians:Number):Number{
return Math.floor(radians/(Math.PI/180));
}
function TextCounter(e:Event):void{
PointCounter.text = String(Points);
LifeCounter.text = String(Life);
}
I'm not really a physics guy but I can refer you to an article/tutorial that will most likely help you solve this issue. If it does, please post your fixed code as an answer because that's a better/more direct answer for this question. If not, hopefully someone else will come along with a better answer:
http://blog.generalrelativity.org/actionscript-30/dynamic-circlecircle-collision-detection-in-actionscript-3/