Looping for events in actionscript 3, only get last number - arrays

So I want to code faster by making array of all button that I have, and also make array of function which index numbers are connected to the array of each buttons.
For example, buttons[0], handler events for hover is button_over_funcs[0] and for out is button_out_funcs[0].
To make it clearer (since english is not my first language), take a look at my code:
var buttons:Array = [playbtn, tutorialbtn];
var button_over_funcs:Array = new Array();
var button_out_funcs:Array = new Array();
var i = 0;
for each(var j in buttons){
j.buttonMode = true;
button_over_funcs.push(function(e:MouseEvent){
j.gotoAndPlay("hover");
});
button_out_funcs.push(function(e:MouseEvent){
j.gotoAndPlay("out");
});
j.addEventListener(MouseEvent.ROLL_OVER, button_over_funcs[i]);
j.addEventListener(MouseEvent.ROLL_OUT, button_out_funcs[i]);
i++;
}
but the j will always refer to tutorialbtn, regardless which button I hover/out. I tried for-in as well
var buttons:Array = [playbtn, tutorialbtn];
var button_over_funcs:Array = new Array();
var button_out_funcs:Array = new Array();
for(var j in buttons){
buttons[j].buttonMode = true;
button_over_funcs.push(function(e:MouseEvent){
buttons[j].gotoAndPlay("hover");
});
button_out_funcs.push(function(e:MouseEvent){
buttons[j].gotoAndPlay("out");
});
buttons[j].addEventListener(MouseEvent.ROLL_OVER, button_over_funcs[j]);
buttons[j].addEventListener(MouseEvent.ROLL_OUT, button_out_funcs[j]);
}
Both seems the same. It seems like actionscript always refers to the last value of j instead of assigning it. Do you have any idea on how to make this as I expected? Is it impossible to make this quicker and not assigning the button to do same exact stuff?

There's no point in reinventing the wheel. Use SimpleButton and be done with it.
In general: a class is the way to go when you want to define behaviour that several objects have in common.

So I found the way by myself. If you found same problem as me, you can take a look.
var buttons:Array = [playbtn, tutorialbtn];
var button_over_funcs:Array = new Array();
var button_out_funcs:Array = new Array();
function addOver(ob){
button_over_funcs.push(function(e:MouseEvent){
ob.gotoAndPlay("hover");
});
}
function addOut(ob){
button_out_funcs.push(function(e:MouseEvent){
ob.gotoAndPlay("out");
});
}
function addEvent(ob){
addOver(ob);
addOut(ob);
}
for each(var j in buttons){
addEvent(j)
}
for(var i = 0; i<= buttons.length - 1; i++){
buttons[i].buttonMode = true;
buttons[i].addEventListener(MouseEvent.ROLL_OVER, button_over_funcs[i]);
buttons[i].addEventListener(MouseEvent.ROLL_OUT, button_out_funcs[i]);
}

Related

In Actionscript 3, how to create many instances of the same symbol?

Though coding, I must make many copies of the same MovieClip to be placed on the stage, each to be manipulated code-wise on its own
For instance, I have a MovieClip named MC and I wish to have 99 copies of ít loaded onto the stage, each in a different x coordinate. What should I do?
I think on doing this:
Step 1: in the library, turning MC into a class
Step 2: placing the following code in the scene's script
var MyArray:Array = new Array
for (var i:int = 0; i<99;i++)
{
var MCInstance:MC = new MC
MC Instance = MyArray[i]
MovieClip.(MyArray[i]).x = i*30
}
Would that make sense?
That's probably the right idea, your syntax is just a little off. Try this:
var myArray:Array = [];
for (var i:int = 0; i < 99;i++)
{
var mc:MC = new MC();
myArray[i] = mc;
mc.x = i * 30
}
AS3 style conventions: use lowerCamelCase for variable names, don't omit constructor parens even though they are optional, and create arrays using literals (source).
You could push each MovieClip to an Array after adding it to the Stage.
var myArray = [];
for(var i:int = 0; i < 99; i++)
{
var myMc:MC = new MC();
addChild(myMc);
myMc.x = myMc.width * i + 2;
myMc.y = 10;
myArray.push(myMc);
}

need some array help in actionscript 3

i am very new to concept of array...so i need a bit help understanding and executing it....
so I have 5 buttons...
monday - friday
now when somebody presses one of the buttons I want the program to trace the day that was pressed only.. can somebody please explain me step by step on what to do and why?
I am using flash actionscript 3..
this is pretty much the only thing I know how to do...I've tried some onlint tuts but they weren't very clear
var days: Array ["mon", "tues", "wed", "thurs", "fri"];
Your Array was created wrong in your post. Try this:
var days:Array = ["mon", "tues", "wed", "thurs", "fri"];
or you could also create it like this, using the push() method of the Array class:
var days:Array = new Array();
days.push( 'mon');
days.push( 'tues');
days.push( 'weds');
days.push( 'thurs');
days.push( 'fri');
to trace the values of the array in your post:
trace( days[0] ); // returns "mon"
trace( days[1] ); // returns "tues"
trace( days[2] ); // returns "wed"
trace( days[3] ); // returns "thurs"
trace( days[4] ); // returns "fri"
The array's contents are stored in the array's "indexes". An array's index always starts with 0.
Array's have a length. An empty array's length is 0. If the array has at least one item in it, the length is 1 and increases as you add more items. To loop through your array to get the values do this:
for(var i:int = 0; i < days.length; i++)
{
trace( days[i] );
}
Arrays are a powerful and essential part of any programming language. You can remove items from the array, add items, remove a specific item at a specific index, remove an item by name, combine arrays, and lots more. You'd benefit from looking at this link and studying the properties and methods of the Array class. Once you get the hang of how to manipulate Array's you'll never now how you existed without them!
There are many ways to associate a button with a particular array index. The answer provided by Dr.Denis McCracleJizz is one way, although if you are as new to AS3 as you say, it might seem a little overwhelming as it uses several concepts you may not yet be familiar with. Let me see if I can simplify it a bit, although this will make the code a bit more lengthy:
mondayButton.addEventListener( MouseEvent.CLICK, onClickDayButton );
tuesdayButton.addEventListener( MouseEvent.CLICK, onClickDayButton );
function onClickDayButton( e:MouseEvent ):void
{
if( e.target.name == 'mondayButton')
{
trace( days[0] );
}
else if( e.target.name == 'tuesdayButton')
{
trace( days [1] );
}
// and so on...
}
If you're familiar with objects you could create an object for each button that hold both the button and the button id and store those in an array:
var days:Array = ["mon", "tues", "wed", "thurs", "fri"];
var dayButtonArray:Array = new Array();
var mondayButtonObject:Object = new Object();
mondayButtonObject.button = mondayButton;
mondayButtonObject.id = 0;
dayButtonArray.push( mondayButtonObject );
var tuesdayButtonObject:Object = new Object();
tuesdayButtonObject.button = tuesdayButton;
tuesdayButtonObject.id = 1;
dayButtonArray.push( tuesdayButtonObject );
// and like above, the rest of the days here
// then loop through them and set the mouseEvent
for(var i:int = 0; i < dayButtonArray.length; i++)
{
dayButtonArray[i].button.addEventListener( MoouseEvent.CLICK, onClickDayButton );
}
// and the function each button calls to
function onClickDayButton( e:MouseEvent ):void
{
trace( days[ evt.target.id ] );
}
You could further simplify the object method above by skipping the days array altogether and adding the day associated with the button right into the dayButtonArray instead of an id:
var dayButtonArray:Array = new Array();
var mondayButtonObject:Object = new Object();
mondayButtonObject.button = mondayButton;
mondayButtonObject.day = "monday";
dayButtonArray.push( mondayButtonObject );
var tuesdayButtonObject:Object = new Object();
tuesdayButtonObject.button = tuesdayButton;
tuesdayButtonObject.day = "tuesday";
dayButtonArray.push( tuesdayButtonObject );
// and like above, the rest of the days here
// then loop through them and set the mouseEvent
for(var i:int = 0; i < dayButtonArray.length; i++)
{
dayButtonArray[i].button.addEventListener( MoouseEvent.CLICK, onClickDayButton );
}
// and the function each button calls to
function onClickDayButton( e:MouseEvent ):void
{
trace( evt.target.day );
}
Now were getting as complicated as Dr.Denis McCracleJizz's answer. His is definitely worth looking at as well.
Your array is a list of items, so your array is composed of the days of the week.
You can access them by using an index like so.
trace(days[0])
//Outputs: mon
trace(days[3])
//Outputs: thurs
Now for your buttons there is two way of doing it, the clean one that's more complicated and the other one that works just as well and that is more simple.
Add event listeners on your buttons so that when you click the first one you can
trace the first element of your days array using days[0]
the real way to do it is this.
var days: Array ["mon", "tues", "wed", "thurs", "fri"];
var buttons: Array = [ stage.getChildByName("mondayButton"), stage.getChildByName("tuesdayButton") ];
//you need buttons name 'mondayButton' in your stage or this will crash.
//then somewhere in your program you add event listener to all the buttons in your array like so
for(var i:int =0; i < buttons.Length; i++){
var myButton:Button = buttons[i];
myButton.AddEventListener(onButtonClicked,Event.Mouse_Click);
}
public onButtonClicked(MouseEvent e):void{
var myClickedButton:Button = (e.target as Button);
//e.target is who sends the event, in this case its your buttons
//next you look what position this button takes in your button array and get get
//the day in the array from its index
var index:int = buttons.indexOf(myClickedButton);
trace(days[index]);
}
Take note that this code is written from memory, dont copy paste. This is the more "complicated" method of doing what you want to do. Hope this helps :)

Select random elements from an array without repeats?

edit: I can't believe I didn't catch this sooner. Turns out my problem was re-declaring my first variables over and over again, essentially starting the program fresh instead of continuing it. To fix it, I replaced the first two lines with this:
if (initialized === undefined) {
trace("INITIALIZING");
var MCs = [];
var lastPos = "intializer";
var initialized = 1;
}
Now it works like a charm. I feel like a noob for this one; sorry to anyone whose time I wasted. I'd post this as an answer to my own question, but it won't let me since I'm still new.
Original Post follows:
I'm trying to make a flash that will randomly choose an ad, play it, and then randomly play another. To that end, I've succeeded by shuffling an array, and then gotoAndPlay-ing the label in the first element of the array, and then removing that element. At the end of each ad is gotoAndPlay(1); with all the main code being on the first frame. If the array is empty, it rebuilds it and reshuffles it.
The problem is, I don't want it to repeat any ads until its run through all of them; I think I've got that down, but I'm not positive. Further, I don't want the last element in the array to be the same as the first in the new one, so the same ad won't ever show twice in a row. I'm trying to have it detect if the element it just used matches the one it's about to use, and reshuffle if that happens, but in my testing it continues to occasionally show the same ad twice in a row.
I'm obviously doing something wrong, but being entirely new to ActionScript3 (and in fact to flash) I'm having a lot of trouble identifying what it is. Here's what I have right now:
var MCs = [];
var lastPos = "intializer";
if (MCs.length == 0) {
MCs = reset();
if (lastPos == MCs[0]) {
while (lastPos == MCs[0]) {
MCs = reset();
}
}
}
if (MCs.length > 0) {
lastPos = MCs[0];
MCs.splice(0,1);
gotoAndPlay(lastPos+"MC");
}
function reset(){
var PrepMCs = new Array("Image1", "Image2", "Image3");
var WorkMCs = new Array(PrepMCs.length);
var randomPos:Number = 0;
for (var i:int = 0; i < WorkMCs.length; i++)
{
randomPos = int(Math.random() * PrepMCs.length);
WorkMCs[i] = PrepMCs.splice(randomPos, 1)[0];
}
return WorkMCs;
}
Personally, I'd rather just do this with JavaScript, HTML, and images; it'd be really simple. But for hosting/CMS reasons I don't have any control over, I'm limited to a single file or a single block of code; I can't host anything externally, which as far as I can tell leaves Flash as my best option for this.
Any help would be greatly appreciated, thanks! If I've done something horribly, horribly wrong, and it's a wonder this even runs at all, don't hesitate to tell me!
edit: It just occurred to me, it is perfectly fine if the second run is in the same order as the first run, etc. The main thing is, it needs to be random. This is probably much easier to implement.
edit 2: MASSIVE DERP HERE. Every time it runs, it re-initializes MCs and lastPos... in other words, it's shuffling every time and starting over. What I should be researching is how to only run a line of code if a variable hasn't been initialized yet.
Blatantly stealing from #32bitKid, this is my version.
The main problem I have with his solution is the push/splice idea. As much as possible, I like to create once, and reuse. Shrinking and growing arrays is bulky, even if effective.
Also, this method does not re-order the array, which may or may not be valuable.
BTW, I like the way that he prevents a repeat of the previous item ("almost empty").
So here is another method:
package
{
public class RandomizedList
{
private var _items:Array;
private var idxs:Array;
private var rnd:int;
private var priorItemIdx:int;
private var curIdx:int;
public function RandomizedList(inarr:Array)
{
items = inarr;
}
private function initRandomize():void
{
idxs = new Array();
//Fisher-Yates initialization (http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle):
idxs[i] = 0;
for (var i:int = 1; i < items.length; i++)
{
rnd = int(Math.random() * (i + 1));
idxs[i] = idxs[rnd];
idxs[rnd] = rnd;
}
curIdx = 0;
priorItemIdx = -1;
}
private function randomize():void
{
var tempint:int;
//Fisher-Yates (http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle):
for (var i:int = items.length; i >= 1; i--)
{
rnd = int(Math.random() * (i + 1));
tempint = idxs[i];
idxs[i] = idxs[rnd];
idxs[rnd] = tempint;
}
curIdx = 0;
}
public function next():void
{
if (curIdx >= idxs.length)
{
randomize();
}
if (items.length > 1 && priorItemIdx == idxs[curIdx])
{
curIdx++;
}
priorItemIdx = idxs[curIdx++];
return items[priorItemIdx];
}
public function get items():Array
{
return _items;
}
public function set items(value:Array):void
{
_items = value;
initRandomize();
}
}
}
I would use a utility class like this to abstract out the behavior I wanted:
import flash.text.TextField;
class Randomizer {
private var unused:Array = [];
private var used:Array;
public function Randomizer(playList:Array) {
used = playList;
}
public function next():* {
// If almost empty, refill the unused array
if(unused.length <= 1) refill();
// Get the first item off the playList
var item:* = unused.shift();
// Shove it into the bucket
used.push(item);
// return it back
return item;
}
public function refill():void {
var i:int;
// Fisher-Yates shuffle to refill the unused array
while(used.length > 0) {
i = Math.floor(Math.random() * used.length)
unused.push(used.splice(i,1)[0])
}
}
}
Notice that it refills the unused array when the unused array still has one item in it, this makes it impossible for the last result to repeat twice in a row. This will return each item once before before looping, and will never repeat the same item twice.
You would use it by saying something like:
var ads:Randomizer = new Randomizer(["Image1", "Image2", "Image3"]);
ads.next(); // will return something
ads.next(); // will return something
ads.next(); // will return something
ads.next(); // will return something
// Keep going into infinity...
There is a little test example of this code working here.
See if this makes any sense
//create your array of all your ad names/frame labels
var PrepMCs:Array = new Array("Image1", "Image2", "Image3");
var shuffledMCs:Array = [];
//store the name of the last played ad in this var
var lastAdPlayed:String;
//shuffle the array
shuffleArray(PrepMCs);
function shuffleArray(arrayToShuffle:Array):void {
//clear the array
shuffledMCs = [];
var len:int = arrayToShuffle.length;
for(var i:int = 0; i<len; i++) {
shuffledMCs[i] = arrayToShuffle.splice(int(Math.random() * (len - i)), 1)[0];
}
//test to see if the new first ad is the same as the last played ad
if (lastAdPlayed == shuffledMCs[0]) {
//reshuffle
shuffleArray(PrepMCs);
} else {
lastAdPlayed = [0];
trace(shuffledMCs);
playAds();
}
}
//after each ad has played, call this function
function playAds():void {
if (shuffledMCs.length > 0) {
gotoAndPlay(shuffledMCs[0]);
shuffledMCs.splice(0,1);
} else {
//array is empty so we have played all the ads
shuffleArray(PrepMCs);
}
}

Creating a new array for eache iteration of the function call. AS3

so I have writing a function that returns objects on the stage and puts them into an array. and the function works fine until i call the function on more than one object name, meaning if im in the root class, and I call this function on object1 lets say it will add all the object one's from the stage, but if i call it on object2 it will throw an error, which makes some sense, i guess it means that it is not adding it to a unique array, but im not sure how to do that.
would it be a good idea to maybe make a multidimensional array? if that is the case would it be too slow?
here is the function code:
public function findObjects(objectName, objLocation, bVisible = false):Array{
for (var i = 0; i < objLocation.numChildren; i++){
var nObj=objLocation.getChildAt(i);
if (nObj is objectName){
// add to array and make invisible
obj.push(nObj);
nObj.visible=bVisible;
}
}
return obj;
}
any help with this would be greatly appreciated.
Try this:
public function findObjects(type:Class, target:DisplayObjectContainer, bVisible:Boolean=false):Array
{
var out:Array = [];
for(var i:int = 0; i<target.numChildren; i++)
{
var obj:DisplayObject = target.getChildAt(i);
if(obj is type)
{
out.push(obj);
obj.visible = bVisible;
}
}
return out;
}
And then based on your code, the implementation would probably be:
obj = findObjects(MovieClip, container);

flash as3 how to prevent an item from being added to an array if it already exists in the array

I know how to remove duplicates from an array, but what I'm trying to do is prevent an item from ever being added to an array in the first place if it already exists. I'm pulling in data from an xml feed in a loop, and I thought that searching for that values index would work, but no matter what, the index is always -1. Here's my code:
var yearArr:Array = new Array();
for (var i=0;i<numCovers;i++){
var coverRef = xmlObj.cover[i];
var coverClip:MovieClip = new MovieClip();
coverClip.year = coverRef.#year;
if (yearArr.indexOf(coverClip.year === -1)){
yearArr.push (coverClip.year);
}
}
Maybe I'm misunderstanding the indexOf function, but I thought it was supposed to return -1 if a value did not exist in an array. What am I doing wrong?
Here's the solution I came up with:
var yearArr:Array = new Array();
for (var i=0;i<numCovers;i++){
var coverRef = xmlObj.cover[i];
var coverClip:MovieClip = new MovieClip();
coverYear = coverRef.#year;
addCoverYear(coverYear);
}
function addCoverYear(coverYear:int):void {
if (yearArr.indexOf(coverYear) == -1){
yearArr.push(coverYear);
}
}
you can reduce an array by passing everything to a dictionary, which will automatically remove redundancies. then pass the dictionary back as a new array.
//Reduce Array
private function reduceArray(array:Array):Array
{
var dictionary:Dictionary = new Dictionary();
for each (var element:String in array)
dictionary[element] = true;
var result:Array = new Array();
for (var key:String in dictionary)
result.push(key);
dictionary = null;
return result;
}
Your code is almost fine. The problem is that an E4X property .#year is not a literal string (I'm not sure right now, but I believe it's an XMLList object). That's why the indexOf call will keep returning -1, because it is looking for a duplicate of that object, not a string. E4X will convert it to a string as soon as you put it somewhere where only strings can go, but until that time it is something else.
If you rewrite your code like this, it should work right away:
var yearArr:Array = new Array();
for each (var coverRef : XML in xmlObj.cover){
var year : String = coverRef.#year; // force the property to be a string
if (yearArr.indexOf(year) < 0){
yearArr.push (year);
}
}
There were also a few other optimizations you could do to your code. The new MovieClip() part wasn't used, not all variables were strongly typed and by using a for each loop, you can state much clearer what objects you're looping through.
Here is what you could do, if for example, you have an array of strings.
var ItemList:Array = new Array();
for each(var Item:String in UseYourXMLFeed)
{
if(ItemList.indexOf(Item) == -1)
{
ItemList.push(Item);
}
}
Edit:
Anyways, your real answer is in the comment by Sam.

Resources