Apply EventListner on Multiple MovieClip Instances - actionscript-3

I have 10 movielclips on stage with name of "mc1,mc2,mc3....". I want to apply one eventlistner on all and I was trying to call using variable but its not working when I can directly with instance name it works.
can anyone help that how can i apply one function on multiple movieclcip instances
//var mc ="mc1";
var mc = this["mc" + 1];
mc.addEventListener(MouseEvent.CLICK, testFunction);
function testFunction(e:MouseEvent):void
{
trace("Seconds elapsed: "+ e.target.name);
}

If your Movieclips are named mc1, mc2 etc, you can just loop through them like this:
for(var i = 1; i <= 10; i++) {
this["mc" + i].addEventListener(MouseEvent.CLICK, testFunction);
}
Keep in mind that accessing Movieclips by their name like this can lead to problems at runtime if the names change. It will also not give you any errors when compiling because this is all checked and executed at runtime. So debugging this might be a pain.

Related

ActionScript 3: Adding multiple instances of the same object to the stage and removing each separately

I have a piece of code adding three insances of the same MovieClip to the stage. I also added MouseEvent.CLICK listener. Once any of the MovieClips gets clicked, I want it to be removed from the stage. My problem is, whenever any of the elements gets clicked, only the last one gets removed and when I click again on a different instance, I'm getting:
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
I added these three MovieClips to an array, but I don't know how I can properly identify which instance got clicked and remove only the said instance.
This is the excerpt of code I have:
var myMC: SomeMC;
var myArray: Array = [];
function Loaded(e: Event): void {
for (var i: int = 0; i < 3; i++) {
myMC = new SomeMC();
myMC.addEventListener(MouseEvent.CLICK, Clicked);
myMC.y = 50;
myMC.x = 50 * i;
addChild(myMC);
myArray.push(myMC);
}
}
function imageClicked(e: MouseEvent){
// Only the last instance gets removed.
e.currentTarget.parent.removeChild(myMC);
}
I'd be grateful for any help.
In Loaded function you create 3 instances of the object, but by doing:
myMC = new SomeMC();
you overwrite reference. In first iteration myMC is 1st one, in 2nd 2nd one, etc...
Then in imageClicked you're trying to remove it. 1st time it's working because it's referencing the last object, but after you removed it from stage it won't work anymore.
How about changing e.currentTarget.parent.removeChild(myMC); to e.currentTarget.parent.removeChild(e.currentTarget); ? That should remove clicked one.

AS3 - How to put all instances in one code for hit testing?

I have 2.5D game so I cannot put all collision objects in a movieclip container, because I need to keep them as separate display objects. I have multiple instances in the stage. I wouldn't want to go write all the hitTest code for all the objects:
if (player.hitTestObject(object1)
if (player.hitTestObject(object2)
if (player.hitTestObject(object2)... etc
So I would like to know how to hitTest all these instances in one code. I have them added on the stage with instance names, so they are not variables and not added by using the addChild code.
There are plenty of ways to make it less tedious. I'll show a few:
Make a container. You could make a container movie clip and put all the objects in that. Those objects are still individual objects after that (as per your reason in your question for not wanting to go this route). Then you can iterate over all the children of that movie clip:
var i:int = container.numChildren;
while(i--){
if(player.hitTestObject(container.getChildAt(i) as DisplayObject)){
//hit, do something
}
}
Put all the objects in an array, then iterate over that array:
//when you app starts:
var objectArray:Array = [object1,object2,object3]//etc.
//OR, if you have say object1 - object20, you could do something like this:
//vector is basically the same as an array except every item has to be of the same type (on inherit from it)
var objectArray:Vector.<DisplayObject> = new Vector.<DisplayObject>();
for(var i:int=1;i<=20;i++){
var obj:DisplayObject = this.getChildByName("object" + i) as DisplayObject;
if(obj) objectArray.push(obj);
}
//THEN, later, when you do your hit test:
var i:int = objectArray.length;
while(i--){
if(player.hitTestObject(objectArray[i])){
}
}

Actionscript 3, access Symbol properties (AS Linkage)

I have created a new Symbol in the Flash IDE, I set it to Export for Actionscript and it has a class name of itemCoin
My stage has now 3 movieclips of that class, how can I:
count how many of itemCoin are on the stage
manipulate them, set and get their x, y individually etc. I tried itemCoin[0].x [1] [2]...etc but it throws an error
remove them from stage when needed
add an event listener that executes a function whenever a itemCoin is removed?
Instance name is used for referencing instances of objects.
For example, if you had a symbol of type ItemCoin (note that naming convention for a type usually starts with a capital letter):
When you place instances on the stage, you give them an instance name to reference them by (note that naming convention for an instance is usually camel case):
Now, properties may be accessed by referencing the instance name from code:
itemCoin1.x = 50;
itemCoin1.y = 25;
Remove it form stage:
removeChild(itemCoin1);
Add an event listener to the itemCoin1 instance for when it is removed:
import flash.events.Event;
itemCoin1.addEventListener(Event.REMOVED, removedHandler);
function removedHandler(event:Event):void {
trace("itemCoin1 was removed");
}
removeChild(itemCoin1);
Although generally a poor practice, you can iterate all children to identify instances. For example, to count the number of ItemCoins:
import flash.display.DisplayObject;
var count:uint = 0;
for (var i:uint = 0; i < numChildren; i++) {
var obj:DisplayObject = getChildAt(i);
if (obj is ItemCoin) {
trace("Found " + ++count + " item coins so far.");
}
}
To comprehensively search the display list, you'd have to traverse children of all display objects.
If knowing the total number of instances on the stage was that important, it might be a better idea to define some ActionScript inside the component or within a Factory class to reference count when added to stage and removed from stage.

Using a Numeric Stepper from one Flash file to affect gamespeed in an external Flash file? Actionscript 3.0

Currently I have an intro screen to my flash file which has two objects.
A button which will load an external flash file using:
var myLoader:Loader = new Loader();
var url:URLRequest = new URLRequest("flashgame.swf");
The second thing is a Numeric Stepper, which will be from 1 to 10. If the user selects a number e.g. 3 then the game speed I have set in the flashgame.swf should be changed
Such as:
var gameSpeed:uint = 10 * numericStepper.value;
But I think my problem is coming into place because the stepper and gamespeed are from different files.
Anyone got any idea please?
I have also tried creating a stepper in the game file and used this code:
var gameLevel:NumericStepper = new NumericStepper();
gameLevel.maximum = 10;
gameLevel.minimum = 1;
addChild(gameLevel);
var gameSpeed:uint = 10 * gameLevel.value;
For some reason the stepper just flashes on the stage, no errors come up and the game doesn't work
When you execute you code, the stepper has no chance to wait for user input.
There is no time between theese two instructions.
addChild(gameLevel);
var gameSpeed:uint = 10 * gameLevel.value;
You should wait for user input in your NumericStepper, and then, on user event, set the game speed.
Edit: Yeah I know it's kinda sad to type out all this code (especially since some people wouldn't even be grateful enough to say thanks) but I think this question is important enough to justify the code as it may be helpful to others in future also.
Hi,
You were close. In your game file you could have put a var _setgameSpeed and then from Intro you could adjust it by flashgame._setgameSpeed = gameSpeed; It's a bit more complicated though since you also have to setup a reference to flashgame in the first place. Let me explain...
Ideally you want to put all your code in one place (an .as file would be best but...) if you would rather use timeline then you should create a new empty layer called "actions" and put all your code in the first frame of that.
Also change your button to a movieClip type and remove any code within it since everything will be controlled by the code in "actions" layer. In the example I have that movieclip on the stage with instance name of "btn_load_SWF"
Intro.swf (Parent SWF file)
var my_Game_Swf:MovieClip; //reference name when accessing "flashgame.swf"
var _SWF_is_loaded:Boolean = false; //initial value
var set_gameSpeed:int; //temp value holder for speed
var swf_loader:Loader = new Loader();
btn_load_SWF.buttonMode = true; //instance name of movieclip used as "load" button
btn_load_SWF.addEventListener(MouseEvent.CLICK, load_Game_SWF);
function load_Game_SWF (event:MouseEvent) : void
{
//set_gameSpeed = 10 * numericStepper.value;
set_gameSpeed = 100; //manual set cos I dont have the above numericStepper
if ( _SWF_is_loaded == true)
{
stage.removeChild(swf_loader);
swf_loader.load ( new URLRequest ("flashgame.swf") );
}
else
{ swf_loader.load ( new URLRequest ("flashgame.swf") ); }
swf_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, Game_SWF_ready);
}
function Game_SWF_ready (evt:Event) : void
{
swf_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, Game_SWF_ready);
//Treat the Loader contents (flashgame.swf) as a MovieClip named "my_Game_Swf"
my_Game_Swf = swf_loader.content as MovieClip;
my_Game_Swf.gameSpeed = set_gameSpeed; //update gameSpeed variable in flashgame.swf
//also adjust SWF placement (.x and .y positions etc) here if necessary
stage.addChild(my_Game_Swf);
_SWF_is_loaded = true;
}
Now in you flashgame file make sure the there's also an actions layers and put in code like this below then compile it first before debugging the Intro/Parent file. When your Intro loads the flashgame.swf it should load an swf that already has the code below compiled.
flashgame.swf
var gameSpeed:int;
gameSpeed = 0; //initial value & will be changed by parent
addEventListener(Event.ADDED_TO_STAGE, onAdded_toStage);
function onAdded_toStage (e:Event):void
{
trace("trace gameSpeed is.." + String(gameSpeed)); //confirm speed in Debugger
//*** Example usage ***
var my_shape:Shape = new Shape();
my_shape.graphics.lineStyle(5, 0xFF0000, 5);
my_shape.graphics.moveTo(10, 50);
my_shape.graphics.lineTo(gameSpeed * 10, 50); //this line length is affected by gameSpeed as set in parent SWF
addChild(my_shape);
}
The key line in intro.swf is this: my_Game_Swf.gameSpeed = set_gameSpeed; as it updates a variable in flashgame.swf (referred as my_Game_Swf) with an amount that is taken from a variable in the Parent SWF.
This is just one way you can access information between two separate SWF files. Hope it helps out.

ActionScript 3 name property is not returning the right name...?

I experienced a problem with the name property in as3, I created this "dot" movieclip and I exported to a class,
then I anonymously created a bunch of dots using a loop. I assigned numbers as name to each dots
private function callDots(num:Number):void
{
for (var i = 0; i < subImagesTotal[num]; i++)
{
var d:Dot = new Dot();
d.x = i*23;
d.y = 0;
d.name = i;
dotContainer.addChild(d]);
}
}
so far so good, I checked that if I trace the name here, I will get the number I want.
However, it's not giving me the numbers if I trace it in other functions.
I added all of my dots to "dotContainer", and if I click on one of the dots, it will call this function
private function callFullSub(e:MouseEvent):void
{
var full_loader:Loader = new Loader();
var temp:XMLList = subImages[sub];
var full_url = temp[e.target.name].#IMG;
full_loader.load(new URLRequest(full_url));
full_loader.contentLoaderInfo.addEventListener(Event.INIT, fullLoaded);
}
e.target.name is suppose to be numbers like 1 or 2, but it's giving me "instance66" "instance70" and I
have no idea why. Because I did the same thing with loaders before and it totally worked.
Any ideas? Thanks.
christine
The e.target returns the inner most object clicked on, this could be a TextField, another MovieClip or posibly a shape (I'm not 100% of the last one) inside the "Dot".
To prevent this you could try to set the mouseChildren property to false on the Dot's when you add them. This should insure that nothing inside the dots can dispatch the click event, and thus the Dot's should do it.
Perhaps you could also in the event handler verify the target type with code like this:
private function callFullSub(e:MouseEvent):void
{
if(!e.target is Dot)
throw new Error("target in callFullSub is not Dot but: " + e.target.toString());
//The rest of you code here
}
The answer is [e.currentTarget.name] I perform this all the time!
Should return "Dot1" "Dot2", etc.
If the value you wish to return is a number or other data type other than a string (name of object) use [e.currentTarget.name.substr(3,1).toString()]
Should return 1, 2, etc.
Navee
I tried to reproduce your problem first with Flex using runtime created movieClips and then with Flash using Dot movieClip symbols exported for ActionScript. Neither application exhibited the problem.
You may already know names like "instance66" "instance70" are default enumerated instance names. So, whatever is dispatching the MouseEvent is NOT the dot instance. Perhaps you are unintentionally assigning callFullSub to the wrong targets, maybe your containers? Try assigning it to dot instance right after you create them, like this:
private function callDots(num:Number):void
{
for (var i = 0; i < subImagesTotal[num]; i++)
{
var d:Dot = new Dot();
d.x = i*23;
d.y = 0;
d.name = i;
d.addEventListener(MouseEvent.CLICK, callFullSub);
dotContainer.addChild(d]);
}
}
Be sure to temporarily comment out your original assignment.
Try this might work,..
d.name = i.toString();
You have not shown enough of your code for me to be able to give you a DEFINATE answer, I will however say this.
//After you create each loader you need to set its mouseEnabled
//property to false if you do not want it to be the target of
//Mouse Events, which may be superseding the actual intended target;
var full_loader:Loader = new Loader();
full_loader.mouseEnabled = false;
//Also you could name the loaders and see if what comes back when you click is the same.
ALSO! Add this to your Mouse Event handler for CLICK or MOUSE_DOWN:
trace(e.target is Loader); //If traces true you have an answer
I believe that the mouse events are being dispatched by the Loaders.
please provide more of your code, the code where the Loader.contentLoaderInfo's COMPLETE handler fires. I assume this is where you adding the loaders to the display list as I cannot see that now.