Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 02-13-2012, 02:36 AM   #1
TheMadHatter
Registered User
 
Join Date: Feb 2012
Posts: 7
Default Random Array, create sprite

How can I create a sprite with a random array?


Code:
var ball:Array = new Array();
var currentBall:Number = Math.random();
Now I want to create a sprite with ball[currentBall]

What I am trying to do is have it so that I can keep creating sprites from a random number

like if i wanted every time a user pressed a spacebar to create a new sprite.
No I am not asking anyone to do this, I am asking for help on how I should go about this? Is array the best way to do this?

Last edited by TheMadHatter; 02-13-2012 at 02:52 AM.
TheMadHatter is offline   Reply With Quote
Old 02-13-2012, 05:50 AM   #2
-:)lauri
Senior Member
 
Join Date: Aug 2008
Location: Helsinki, Finland
Posts: 1,184
Default

Probably yes, array sounds like it would be the best choice.
-:)lauri is offline   Reply With Quote
Old 02-13-2012, 01:46 PM   #3
SanderSoulwax
Senior Member
 
Join Date: Sep 2010
Posts: 131
Default

First of all, math.random creates a random number between 0 and 1. You can't use that as indexes for an array off course, so you'll have to multiply the random number with something and use math.floor or math.ceil to round the number up or down.

Secondly, why would you want random indexes? Just create a new Ball() (sprite) and push it into the array when the user presses spacebar.

If you then want to get one of the balls in the array randomly you can use Math.floor(array.length - 1 * Math.random) for the index. That will give you a random ball from the array of balls.
SanderSoulwax is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 03:20 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.