Jump to content
GreenSock

Search In
  • More options...
Find results that contain...
Find results in...
jdrumm

Creating random spawning with static points

Recommended Posts

I'm trying to create a system of spawns for an as3 game.  The goal is to have my enemy tweens spawn from one of the 4 different spawn points defined in the array of new Points, randomly.  I've successful debugged my process so far, as the code at the very bottom illustrates.  
 
However, the problem comes when I attempt to enter my random spawn call into the starting point of the greensock tween.  The code below shows if the random spawn call starts at the x value,
 
that is to say: {startAt:{x:myArray[random]},
 
the problem here is that when myArray[random] is called in that manner it's going to return both an x and a y value which of course is not going to work b/c both points are put into the x value of the tween, and separating the x and y values into two different arrays will call random x and y values, voiding my original intent of them spawning from a static point randomly.  
 
That being said, I'm asking if there is a way that I could combine both an x and y value in one place
 
so, in theory the tween I'm looking for might look like this:
 
myTween = TweenMax.to(mc, 3, {startAt:{myArray[random]}, x:end.x,  y:end.y});
 
*notice how I want the starting point to be the randomly picked point in the array.
 
* here is the idea put into action, but notice starting point below is { x:myArray[random] }
 
var spawnPoint_Array:Array = [new Point(270, 808),            //////// X and Y value of SPAWN 1  
                                                 new Point(-81, 768),             //////// X and Y value of SPAWN 2
                                                 new Point(1024, 768),          //////// X and Y value of SPAWN 3
                                                 new Point(566, 809)]            //////// X and Y value of SPAWN 4
 
var random = Math.round(spawnPoint_Array.length * Math.random()); //////picks a random spawn point

 

myTween = TweenMax.to(mc, 3, {startAt:{x:myArray[random]}, x:end.x,  y:end.y});

 

*in the above tween, mc just refers to the movieclip name

 

 

 

Any ideas on how I would fix this problem?

 

 

 

Link to post
Share on other sites

Sure, the Point objects have x and y properties that you can access.

var random = Math.round(spawnPoint_Array.length * Math.random()); 
trace(spawnPoint_Array[random].y, spawnPoint_Array[random].y);

You can construct your tween like this:

TweenMax.to(mc, 3, {startAt:{x:spawnPoint_Array[random].x, y:spawnPoint_Array[random].y}, x:end.x,  y:end.y});
Link to post
Share on other sites

Sure, the Point objects have x and y properties that you can access.

 

var random = Math.round(spawnPoint_Array.length * Math.random()); 
trace(spawnPoint_Array[random].y, spawnPoint_Array[random].y);
You can construct your tween like this:

TweenMax.to(mc, 3, {startAt:{x:spawnPoint_Array[random].x, y:spawnPoint_Array[random].y}, x:end.x,  y:end.y});

 

 

 

y:spawnPoint_Array[random].y}

y:spawnPoint_Array[random].y}

Link to post
Share on other sites

Thanks for the assist.  I was close, I was using:

 

x:spawnPoint_Array[randomize], y:spawnPoint_Array[randomize]

 

instead of:

 

x.spawnPoint_Array[randomize].x, y:spawnPoint_Array[randomize].y

 

all I needed was the .y and .x on the end of the random call,

 

Wow, I feel foolish.  

 

Thanks for the answer.

 

YOU GUYS AT GREENSOCK ARE THE BEST, LOVE YOUR WORK.

 

"YOUR THE BEST AROUND, NO ONE'S GONNA EVER KEEP YA DOWN!"

  • Like 1
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×