How to tweenLite all objects in array and keep distance position? - arrays

Hey everyone so I have an array of Movie Clip Objects called aPlanetArray and what I am trying to accomplish is having all the objects in the array move down to a certain positing and then stop using tweenLite or any other method that would accomplish this. I know I can do it with y+=2 but I want all objects to move down the screen real quick in a bounce like effect using Tweenlite and to keep their distance ratios.
Here is how I have them setup when added to the stage:
//Numbers
xSpacing = 100;
ySpacing = 180;
startPoint = new Point((stage.stageWidth / 2), (stage.stageHeight / 2) );
private function addOuterPlanets():void
{
for (var i:int = 0; i < nPlanets; i++)
{
outerPlanets = new mcOuterPlanets();
outerPlanets.x = startPoint.x + (xSpacing * i);
outerPlanets.y = startPoint.y - (ySpacing * i);
stage.addChild(outerPlanets);
aPlanetArray.push(outerPlanets);
}
}
and when I tween them I am using this tweenlite function:
for each(var allPlanets:mcOuterPlanets in aPlanetArray)
{
TweenLite.to(allPlanets, 5.0, {y:550, ease:Back.easeOut});
}
This works perfect but all objects in array line up together and don't keep their spacing against one another. Any ideas would be appreciated thank you!

The simplest way would be to just have all the planets in a parent container and then move the container instead of the planets.
var planetContainer:Sprite = new Sprite();
function addPlanetsToContainer():void{
for (var i:int = 0; i < aPlanetArray.length; i++){
planetContainer.addChild(aPlanetArray[i]);
}
}
And now you can do your tween on planetContainer
Now to put the character on a planet, you can either do
planet.addChild(character);
or
character.x = planet.x + planet.parent.x;
character.y = planet.y + planet.parent.y;

Related

Have object rotate another object in array?

Hey everyone so I have an object character that initially rotates around the first Movie Clip called planet which isn't apart of the array. So the user can tap the screen and the character shoots in direction it is rotating. The objective is to have the character hit the other planets in a array and when it does have it start rotating around it just like the original. Everything works fine except when I shoot the character and it lands on one of the outerplanets in aPlanetArray it doesn't start rotating around that new planet but rather just disappears.
Here is how I set it up:
character Orbits first planet that is not in the array just have it as intial orbit then remove it once leaves to other outerPlanets:
private function startOrbit():void
{
//add Planet
planet.x = (stage.stageWidth / 2);
planet.y = (stage.stageHeight / 2) + 100;
stage.addChild(planet);
//radnom number set up in main class
if (randomOrbit == 1) //If equals to one then clock wise
{
clockWiseOrbiter();
}else
if (randomOrbit == 2) //If equals to one then counter clock wise
{
counterClockWiseOrbiter();
}
}
I add outer Planets in array:
private function addOuterPlanets():void
{
for (var i:int = 0; i < nPlanets; i++)
{
outerPlanets = new mcOuterPlanets();
outerPlanets.x = startPoint.x + (xSpacing * i);
outerPlanets.y = startPoint.y - (ySpacing * i);
stage.addChild(outerPlanets);
aPlanetArray.push(outerPlanets);
}
}
In my Enter Frame Handler:
private function logicHandler(e:Event):void
{
if (!tap && !nextlevel)
{
startOrbit(); //start player orbit around planet
}
if (nextlevel)
{
startNewOrbit();
trace("Starting new Orbit");
}
//When tapped have outher planets move down
if (tap)
{
characterTap(); //Character shoots from position
}
collisionPlanetHandler();
}
When the collision happens with Outer Planets:
**** EDIT*****
private function collisionPlanetHandler():void
{
for (var i:int = 0; i < aPlanetArray.length; i++)
{
var currentPlanet:mcOuterPlanets = aPlanetArray[i];
if (character.hitTestObject(currentPlanet[i]) && !nextlevel)
{
trace("HIT");
//Have charcter orbit that planet
//startNewOrbit();
//tap back to false
tap = false;
nextlevel = true;
character.destroy();
planet.destroy();
planetHit = currentPlanet[i];
}
}
}
When the character collides with the outerPlanets in array I have the New orbit happen around the OuterPlanet it lands on. Or I at least try:
private function startNewOrbit():void
{
newClockWise();
}
private function newClockWise():void
{
for (var i:int = 0; i < aPlanetArray.length; i++)
{
var currentPlanet:mcOuterPlanets = aPlanetArray[i];
stage.addChild(character);
character.rotation = (Math.atan2(character.y - currentPlanet.y, character.x - currentPlanet.x) * 180 / Math.PI);
var rad:Number = angle * (Math.PI / 180); // Converting Degrees To Radians
character.x = currentPlanet.x + radius * Math.cos(rad); // Position The Orbiter Along x-axis
character.y = currentPlanet.y + radius * Math.sin(rad); // Position The Orbiter Along y-axis
angle += speed; // Object will orbit clockwise
}
}
I know I am doing something wrong in the startNewOrbit and newClockWise functions. I have a for loop and I am trying to target the object in array it touches. I use this var currentPlanet:mcOuterPlanets = aPlanetArray[i]; But not sure if I need to use currentObject or something equivalent. Does anyone have any idea what i am doing wrong why its not orbiting the new planet it touches but rather just disappears completely?
var planetHit:MovieClip;
function collisionTest():void{
for (loop through planets){
if (planet[i].hitTestObject(character)){
planetHit = planet[i];
}
}
}
See now the planet at that location in the array is (==) planetHit so then you can do
character.x = planetHit.x;
or
planetHit.addChild(character);
etc.

How to remove a child from a random array?

I have an array that randomly creates 10 dots. However there's a certain area where I do not want them to be created. How can I achieve this? My code gives me error 2025.
"The supplied DisplayObject must be a child of the caller."
It will occasionally output the totalDots as instructed, (trace""+totalDots), but 90% of the time it will give me the error.
public var numDots:Array = [];
public var totalDots:int = numDots.length;
public var box:Box = new Box();
public function addBox():void
{
box.x = stageWidth/2;
box.y = stageHeight/2;
addChild(box);
}
private function addDot():void
{
for(var i:int = 0; i < 10; i++)
{
var dot:Dot = new Dot();
dot.x = Math.floor(Math.random() * stageWidth);
dot.y = Math.floor(Math.random() * stageHeight);
this.addChild(dot);
totalDots++;
trace(""+totalDots);
for(var j:int = 0; j < totalDots; j++)
{
if(numDots[j].hitTestObject(box))
{
stage.removeChild(numDots[j]);
numDots.splice(j, 1);
totalDots--;
}
}
}
}
Your problem is your nested loop. With each iteration, you add one new dot, and then loop over all of the existing ones, and remove them if it collides with the box. I don't think that's what you intended to do.
It looks like you just want to make sure the dots are not added within a certain area. In that case, keep it simple with a do while loop:
for(var i:int = 0; i < 10; i++)
{
var dot:Dot = new Dot();
this.addChild(dot);
do {
dot.x = Math.floor(Math.random() * stageWidth);
dot.y = Math.floor(Math.random() * stageHeight);
} while(dot.hitTestObject(box))
totalDots++;
trace(""+totalDots);
}
You never add any dot to your array.
You add the dot to the display list like so:
this.addChild(dot);
and you try to remove it like so:
stage.removeChild(numDots[j]);
Despite the fact that the dot is never added to the array, this couldn't have worked even if it was. That's because this is not stage. They are two different things.
You should never use stage.addChild() (check the documentation for more info on that). Just call addChild() all the time which is equivalent to this.addChild(). This ensures that you always operate on one and the same DisplayObjectContainer
For what it's worth, you can avoid the trial loop altogether by calculating a random value with the proper interval (the difference between the include and exclude areas) and deriving x and y coordinates from that.
The following code (written in a language I do not know, apologies if the syntax is faulty) has two cases. The if case handles the situation where the dot will appear left or right of the exclusion box, and the range of x values is restricted to being left or right of that box. The else case is where the dot will appear above or below the box, and the x values are not restricted.
var interval:int = stageWidth * stageHeight - box.w * box.h;
var cut:int = interval - (stageWidth - box.w) * box.h;
for (var i:int = 0; i < 10; i++) {
var r:int = Math.floor(Math.random() * interval);
var x:int;
var y:int;
if (r >= cut) {
r -= cut;
y = r / (stageWidth - box.w);
x = r - y * (stageWidth - box.w);
y += box.y;
if (x >= box.x) x += box.w;
} else {
y = r / stageWidth;
x = r - y * stageWidth;
if (y >= box.y) y += box.h;
}
var dot:Dot = new Dot();
dot.x = x;
dot.y = y;
this.addChild(dot);
totalDots++;
trace(""+totalDots);
}
This assumes that box is entirely within stageWidth,stageHeight.
Also notable is that it allows the dots to overlap; same as the original code.
For more complex shapes you can set box to the largest rectangle fully enclosed by that shape so as to avoid many but not all retry cases. This can be helpful for large shapes and many dots. Or there are variations which might manage a perfect fit to another shape (eg., an ellipse).

Moving pipes query in a Flappy Bird game

I am trying to build a Flappy Bird like game in Adobe Flash, using Action Script 3 as a school project. I am using an array of Pipe objects to create the number of Pipes for each level (at this stage my code is with one level/stage).
I have one problem though, after I created the array of Pipes and them added each Pipe object to the stage, I tried to use other function to move the pipes, iterating through the array and changing the x dimension value to every Pipe object, but it doesn't work.
Here is my code:
import flash.events.MouseEvent;
stop();
Bird_mc.stop();
//array of pipes;
var pipeArray:Vector.<Pipe > = new Vector.<Pipe > ;
var birdVelocity:int = 0;
var stageGravity:int = 2;
stage.addEventListener(Event.ENTER_FRAME,birdFall);
stage.addEventListener(Event.ENTER_FRAME,createPipesAndLines);
stage.addEventListener(Event.ENTER_FRAME,movePipesAndLines);
stage.addEventListener(MouseEvent.CLICK,birdFly);
function birdFall(event:Event):void
{
Bird_mc.y += birdVelocity;
birdVelocity += stageGravity;
}
function birdFly(event:MouseEvent):void
{
birdVelocity = -16;
Bird_mc.play();
}
/*Move Pipes and Lines to the left - TO BE MADE*/
function createPipesAndLines(event:Event):void
{
for (var i:int = 0; i<10; i++)
{ //use of of-else to separate the pipes up and down and rotate em
if (i%2==0)
{
pipeArray.push(new Pipe());
addChild(pipeArray[i]);
pipeArray[i].x = i * 250;
pipeArray[i].y = 50;
}
else
{
var tempPipe:Pipe = new Pipe();
tempPipe.rotation = 180;
pipeArray.push(tempPipe);
addChild(pipeArray[i]);
pipeArray[i].x = i * 300;
pipeArray[i].y = 400;
}
}
}
//move the pipes to left
function movePipesAndLines(event:Event):void
{
for (var i:int = 0; i<10; i++)
{
pipeArray[i].x -= 0.5;
}
}
Firstly I'd recommend having a single event function that calls every other function that need updating.
stage.addEventListener(Event.ENTER_FRAME, loop);
function loop(event:Event):void {
birdFall();
birdFly();
movePipesAndLines();
}
Secondly you don't really want to add all pipes from the start of the game. I would imagine Flappy Bird attaches new Pipe objects as the screen moves. It's a whole lot more efficient that way.
Other than that the code looks good. I would however like to see what's inside the Pipe object. Perhaps Pipe.x is an int instead of Number?

Assigning instance names to array Objects: ActionScript 3.0

I'll just start by saying that I am a bit new to programming, and I apologize if this is a stupid question.
I have a timer running in my application that at every interval, creates an a new instance of a MovieClip called blueBall.Here is my code:
var randomX:Number = Math.random() * 350;
var newBlue:mc_BlueBall = new mc_BlueBall ;
newBlue.x = randomX;
newBlue.y = -20;
for(var i:Number = 0;i < blueArray.length; i++)
{
newBlue.name = "newBlue" + i;
}
blueArray.push(newBlue);
addChild(newBlue);
}
var randomX:Number = Math.random() * 350;
var newBlue:mc_BlueBall = new mc_BlueBall ;
newBlue.x = randomX;
newBlue.y = -20;
for(var i:Number = 0;i < blueArray.length; i++)
{
newBlue.name = "newBlue" + i;
}
blueArray.push(newBlue);
addChild(newBlue);
}
My question is: How do I make it so that each newly created object in the array has it's own hitTestObject Event? I want to make it so that if the if the user's icon touches one of the newBlue objects, that newBlue object with be removed, and the score will go up a point.
Thanks!
this is my first time answering a question here but I hope I can help! Assuming you have a timer for your main game loop, you should try something like this once per frame:
//For each blue ball in the array
for(var i:int = 0; i < blueArray.length; i++) {
//If it touches the player
if(blueArray[i].hitTestObject(thePlayerMC)) {
//Increment the score
score++;
//Remove from stage and array
removeChild(blueArray[i]);
blueArray.splice(i, 1); //<-At index i, remove 1 element
//Decrement i since we just pulled it out of the array and don't want to skip the next array item
i--;
}
}
This is sort of the quick and dirty solution, but highly effective and commonly used.

ActionScript 3.0 Randomize array, display number, and splice that number

So I am building a deck of cards. I have them blocked out, and appearing correctly on the screen. Now I need to build an array with numbers 1-16, and display them randomly on the cards without duplicating any numbers. My main problem is I can't figure out how to display the random numbers on the cards. I have filled the array, got the cards displaying correctly, I can even display the numbers in order, but can't figure out the code to display the numbers randomly. Thanks in advance.
Below is the code I have so far, which displays the cards with numerically ordered numbers.
var numberOfColumns=8;
var cardNumber:Array = new Array();
//---"for" function to display card columns---\\
for(var i:int = 1; i < 17; i++) {
var card = new Card();
cardNumber[i]= i;
card.x = ((i-1) % numberOfColumns) * 70;
card.y = (Math.floor ((i-1)/numberOfColumns) * 80);
card.cardTxt.text = i;
trace(i);//trace card count in Output
addChild(card);//add object to display list
}
trace(cardNumber);
One solution ( definetly not the best ) would be to fill another array with only indexes and then randomly splice it, like this :
var cardCount:int = 17;
var indexesArray:Array = [];
for(var j:int = 1; j < cardCount; j++)
indexesArray.push(j)
for(var i:int = 1; i < cardCount; i++) {
var card = new Card();
//Use a random index inside the array length
var randIndex:int = Math.round(Math.random()*indexesArray.length);
cardNumber[i]= indexesArray[idIndex];
//Strip out our used index so we can't use it again
indexesArray.splice( randIndex, 1 );
//... your code
}
However I'm pretty sure some logic freak can come up with a more optimized/clean solution, and avoid the need for a second array.

Resources