How would I code a statement that will choose 5 random frames out of 12 in Flash ActionScript 3? - actionscript-3

I'm sure this question has been asked a million times in a million ways, but I would appreciate the help anyway. I am working on a Flash Mastermind clone and have a movie clip with 12 colored "pegs" and a "hole" image. How would I code a statement that will pick five random frames and not just the first five frames? I have the barest idea, but I'm not entirely sure if it's right:
var totalColors:Number = 12;
var maxColors:Number = 5;
var chosenColors:Array:
for(var i:Number = 1; i<totalColors; i++)
{
chosenColors[i] = Math.floor(Math.random()*totalColors)+1
}
Thanks a lot in advance for your help!
Please note that the movieclip has been rearranged since I wrote this; I moved the first frame - the "hole" to another layer.
EDIT: Pan has helped a lot since I first asked this question. To test it I decided to expand on my Bejeweled clone code. I added five more shapes to the original 7. At first I thought the line "newPiece.type = Math.ceil(Math.random() * chosenColors.length);" inside the loop, so I commented the quoted line, which was outside of the j loop and part of the original code, and replaced it with this:
newPiece.type = chosenColors[j];
I am very sorry if this seems elementary to some; I am not the strongest programmer, especially when making games. I am far better at ASP.NET and UI design, but game development has always appealed to me for some weird, possibly insane reason. Anyway, here is part of the method for creating a new jewel. The two for loops are pan's code for choosing seven random frames out of twelve. Unfortunately, the movie still picks the first seven frames from the movie clip.
//i goes through all of the possible colors and adds them to the temp array
for (var i:uint = 1; i <= newPiece.totalFrames; i++)
{
temp.push(i);
}
//j chooses seven colors out of the array of all possibilities
for (var j:int = 0; j < numPieces; j++)
{
//index is the frame that has been chosen randomly
var index:int = int(Math.random() * temp.length);
chosenColors.push(j);
chosenColors[j] = temp[index];
//remove the index
temp.splice(index, 0);
}
newPiece.type = Math.ceil(Math.random() * chosenColors.length);
Again, if I've confused anyone with my bad code to English translation, here is an image of my running game and its Jewel movieclip so you will hopefully see what I mean.

var totalColors:Number = 12;
var maxColors:Number = 5;
var chosenColors:Array = [];
var temp:Array = [];
for(var i:Number = 1; i <= totalColors; i++)
{
temp.push(i);
}
for (var j:int = 0; j < maxColors; j++)
{
var index:int = int(Math.random()*temp.length);
chosenColors[j] = temp[index];
//remove the index
temp.splice(index, 1);
}

Related

MovieClip not responding to a function

I have a complicated problem that I don't know where it comes from.
I have three movie clip in my first frame that each one call "homework" function.
Problem:
There is something wrong with the third one that if you click on it first it works correctly, but if you click on the other objects and then come back and try third one it doesn't work anymore.
I don't have this problem with the others MovieClip objects so how is this problem possible for third MC using the exact same function?
Here is the homeWork function:
function homeWork(a :int) :void
{
gotoAndStop(2);
cubeContainer.addChild(movieClip);
movieClip.x= 20;
movieClip.y= 20;
var j:int =0;
for( var i = a; i < (a+10); i++)
{
cubeContainer.addChild(photos[i]);
photos[i].width = 130;
photos[i].height = 130;
photos[i].x = j-80;
photos[i].y = stage.height-photos[i].height;
j += 90;
photos[i].doubleClickEnabled=true;
photos[i].addEventListener(MouseEvent.DOUBLE_CLICK ,covering);
dragAndDrop(photos[i]);
}
}

Simple (maybe antiquated) AS3 script stops after 30 seconds

Designing some lessons in AS3 for the HS multimedia class I teach.
I haven't tried to teach this in my class in a few years, so I'm a bit rusty. It's also possible my script is not as smooth as it could be as I've based it on some AS2 work I did quite sometime ago.
That said, this little bit of code runs for about thirty seconds and then stops. No error, no feedback outside the trace command I put in to track array length.
This is part of a lesson series for getting the kids in the direction of writing some games in AS3. In this one we're looking at creating random timing and random placement. We've already covered motion, properties, variables, listeners, and functions.
Here's the code:
import flash.events.Event;
var firstMeteor:Number = 1;
var timeSinceLast:Number = 0;
var lastMeteor:Number = 0;
var totalMeteors = 60;
var metProp=.08;
var rocksBox:Array = new Array;
var dropSpeed:Number = 15;
var lowLimit:Number = 350;
stage.addEventListener(Event.ENTER_FRAME,dropRocks);
stage.addEventListener(Event.ENTER_FRAME,moveRocks);
function dropRocks(e:Event):void{
if(lastMeteor<totalMeteors) {
if(Math.random() < metProp) {
lastMeteor++;
var rock:meteor = new meteor;
addChild(rock);
rocksBox.push(rock);
rock.x = Math.random()*500;
rock.y = 20;
timeSinceLast = 0;
}
}
timeSinceLast++;
}
function moveRocks(e:Event):void {
for(var i:int = rocksBox.length-1; i>=0; i--) {
rocksBox[i].y += dropSpeed;
if(rocksBox[i].y>lowLimit) {
removeChild(rocksBox[i]);
rocksBox.splice(i,1);
trace(rocksBox.length);
}
}
}
In your code, you drop a rock only when the total count of the meteors created so far (i.e: the lastMeteor), has not yet reached the maximum amount of totalMeteors you specified! When it reached the maximum, then if(lastMeteor<totalMeteors) won't let any other "meteor" creation happen!!

AS3 - How to animate all movie clips in the stage if i click a certain button

Hi have been reading on this great website, here is what i want to accomplish,
on this certain page in the app im doing, there are movie clips (buttons, texts, menus) , now what i want is if i click this "close" button, i want everything to animate e.g shrink one by one. Question: Is there a great way to code this efficiently, and not coding it one by one e.g menu_mc.gotoAndPlay("out");, text_mc.gotoAndPlay("out"); image.gotoAndPlay("out"); ...something like that. Sorry im so new to this, any help will be greatly appreciated.
You could iterate the DisplayList, and call your method...
for (var i:int = 0; i < this.numChildren; i++) {
var obj:* = this.getChildAt(i);
if (obj is MovieClip) {
obj.gotoAndPlay("out");
}
}
Or you could animate with an animation library (such as Greensock)...
for (var i:int = 0; i < this.numChildren; i++) {
var obj:* = this.getChildAt(i);
TweenLite.to(obj, .35, {scaleX:0.5, scaleY:0.5, delay:0.25*i});
}
Or you could reference an Array for the objects you want to animate; a superior choice (if your objects aren't in the same container).
var a:Array = [menu_mc, text_mc, image];
for (var i:int = 0; i < a.length; i++) {
TweenLite.to(a[i], .35, {scaleX:0.5, scaleY:0.5, delay:0.25*i});
}

Arranging items in an inventory

I'm working on an inventory system I made following a short tutorial that leaves you stranded. I've managed to get the items removed and rearrange to the correct order somewhat. For some reason though, if I click on the last item in my inventory, then on the first item, the items do not rearrange correctly.
public class Inventory {
var itemsInInventory:Array;
var inventorySprite:Sprite;
var itemNum:int;
public function Inventory(parentMC:MovieClip) {
itemNum=0;
itemsInInventory = new Array();
inventorySprite = new Sprite();
inventorySprite.x = 50;
inventorySprite.y = 360;
parentMC.addChild(inventorySprite);
}
public function makeInventoryItems(arrayOfItems:Array){
for(var i:int = 0; i < arrayOfItems.length; i++){
arrayOfItems[i].addEventListener(MouseEvent.CLICK, getItem);
arrayOfItems[i].buttonMode = true;
}
}
public function getItem(e:MouseEvent){
var item:MovieClip = MovieClip(e.currentTarget);
itemsInInventory.push(item);
inventorySprite.addChild(item);
item.x = (itemsInInventory.length-1)*40;
item.y = 0;
item.removeEventListener(MouseEvent.CLICK, getItem);
item.addEventListener(MouseEvent.CLICK, useItem);
}
public function useItem(e:MouseEvent){
var item:MovieClip = MovieClip(e.currentTarget);
itemNum = item.x;
inventorySprite.removeChild(item);
itemsInInventory.splice(item, 1);
sortInventory();
}
public function sortInventory(){
for(var i:int = 0; i < itemsInInventory.length; i++){
if(itemsInInventory[i].x > itemNum){
itemsInInventory[i].x -= 40;
}
}
itemNum=0;
}
}
I belive thats all the coding info I need to provide for help solving this mystery.
Also, a link to the game for testing. If you would like a link for a download of the game, please ask.
LINK
Instead of substracting 40px, just set their position again:
for(var i:int = 0; i < itemsInInventory.length; i++){
itemsInInventory[i].x = i*40;
}
Also, I did not even know that it is possible to give an object reference to the splice function, I would rather use:
itemsInInventory.splice(itemsInInventory.indexOf(item), 1);
And remove the event listener from the item when you delete it from the inventory in the useItem function.
item.removeEventListener(MouseEvent.CLICK, useItem);
EDIT:
With Flash Player 10, Adobe introduced the Vector class which is kind of the same as the Array class, but it can only store one data type. In your case it would be MovieClip or Sprite. The Vector class is singificantly faster and more developer friendly because you can see the help from the IDE when you are typing myVector[i].. I recommend using that instead, although there is nothing wrong with Array. It is just outdated a bit, but is helpful when you want to store more data types.
myVector:Vector.<MovieClip> = new Vector.<MovieClip>();

How to adjust the z-axis, if there is, for graphics in AS3

I'm a beginner in FlashDevelop. I drew a simple circle. I also embedded a .jpg. Is there any way to place the circle I drew before the jpeg?
This is placed inside the init():
[Embed (source = "images/Untitled.png")]
var bg:Class;
var bmp1:Bitmap = new bg;
addChild(bmp1);
var k:int, l:int;
for (l = 0; l < 10; l++)
{
for (k = 0; k < 8; k++)
{
graphics.beginFill(0xff0000, 1);
graphics.drawCircle(k*50, l*50, 10);
graphics.endFill();
}
}
[Embed (source = "images/Untitled.png")]
var bg:Class;
var bmp1:Bitmap = new bg;
addChild(bmp1); // Add the bitmap first
var sprite1:Sprite = new Sprite();
this.addChild(sprite1); // Add the sprite afterwards; it appears on top of the bitmap
var k:int, l:int;
for (l = 0; l < 10; l++)
{
for (k = 0; k < 8; k++)
{
sprite1.graphics.beginFill(0xff0000, 1);
sprite1.graphics.drawCircle(k*50, l*50, 10);
sprite1.graphics.endFill();
}
}
What you're looking at here is a topic called The Display List. Put very simply and very shortly, objects that are added to the stage (or other containers) will be displayed in the order that they are added, with the latest object going on top of the previous one.
Pranav Negandhi's answer is correct in showing how to add your circle object infront/on-top/before your jpeg, but you should consider reading all or some of the following articles in order to fully understand the display list and what tools you have available to make objects appear just the way you want them on the screen.
AS3 101: The Display List
Adobe's Display list programming in ActionScript 3
Republic Of Code's AS3: The Display List
You could also have a look through the reference for displayobjectcontainer, which contains most of the methods you'll be working with, such as addChild() or addChildAt()
Display Object Container Reference
As always, feel free to ask questions here on stackoverflow if you need help!