Copy link to clipboard
Copied
Hi,
I have a simple code.
One item is on stage, and it is moving after clicked. Based on the x value, additional three movieclips are added to the stage. And pushed to an array.
They are all moving with the same speed.
I need control the motion via the first movie clip (the one already on stage, PartSuur).
When it is clicked, the item itself and the items in array should stop.
And they do.
When clicked again, they should start moving again. With the same speed as before.
PartSuur is moving and stopping correctly. But the items in array start to speed up after each stop and move click.
I have tried a few changes in the code, couldn't solve it.
Hope someone has time to review and give me some advice.
Many thanks in advance!
origX = PartSuur.x;
origY = PartSuur.y;
PartSuur.addEventListener(MouseEvent.CLICK, addEnterFrame);
function addEnterFrame(event: MouseEvent): void
{
PartSuur.removeEventListener(MouseEvent.CLICK, addEnterFrame);
PartSuur.addEventListener(Event.ENTER_FRAME, moveDuckBig);
for(var i: uint = 0; i < arrayDuckSmall.length; i++)
{
arrayDuckSmall.addEventListener(Event.ENTER_FRAME, moveDuckSmall);
trace(".EventListener added");
}
PartSuur.addEventListener(MouseEvent.CLICK, stopDucks);
}
function moveDuckBig(event: Event): void
{
PartSuur.x += 0.5;
if(PartSuur.x == origX + 75)
{
addDuck();
}
if(PartSuur.x == origX + 125)
{
addDuck();
}
if(PartSuur.x == origX + 175)
{
addDuck();
}
}
function stopDucks(event: MouseEvent): void
{
PartSuur.removeEventListener(Event.ENTER_FRAME, moveDuckBig);
for(var i: uint = 0; i < arrayDuckSmall.length; i++)
{
arrayDuckSmall.removeEventListener(Event.ENTER_FRAME, moveDuckSmall);
trace(".EventListener removed");
}
PartSuur.addEventListener(MouseEvent.CLICK, addEnterFrame);
}
function addDuck(): void
{
newDuckSmall = new partVaike();
addChild(newDuckSmall);
arrayDuckSmall.push(newDuckSmall);
newDuckSmall.scaleX = 1;
newDuckSmall.scaleY = newDuckSmall.scaleX;
newDuckSmall.x = origX;
newDuckSmall.y = origY;
arrayDuckSmall.addEventListener(Event.ENTER_FRAME, moveDuckSmall);
}
function moveDuckSmall(event: Event): void
{
for(var i: uint = 0; i < arrayDuckSmall.length; i++)
{
arrayDuckSmall.x += 0.5;
}
}
The value of i in the addDuck function is not working, as I indicated. If it is always 0 then it is always adding an event listener to the first duck added to the array. You should change that line from...
arrayDuckSmall.addEventListener(Event.ENTER_FRAME, moveDuckSmall);
to
newDuckSmall.addEventListener(Event.ENTER_FRAME, moveDuckSmall);
so that it is added to the duck that was just added, not the first duck that was added.
A change in your approach to this could help alleviate issues with havin
...Copy link to clipboard
Copied
It sounds like the ENTER_FRAME event listener is being compounded for the small ducks. What you might try is to use the trace function and trace each time the event listener is added in your code and have it identify which duck it is being assigned to. MAke sure you identify which trace is triggered as well so that you can see where the extra listeners are coming from
I see a potential issue in your addDuck function where you add the event listener. You assign it using the " i " variable, but inside that function the variable is not defined, so it is likely always going to be 0... so the first duck added to the array will be the only one getting the listener. Since you don't show all of the code I might be mistaken and maybe you have " i " defined for global use, though I would recommend against using it that way since you use it several other places for loops as well.
Copy link to clipboard
Copied
Hi,
the i is defined and the arrayDuckSmall is the only array in that file.
var arrayDuckSmall: Array = new Array();
var i: uint;
When the event listener is added, how do I trace it, which item got it?
function addDuck(): void
{
newDuckSmall = new partVaike();
addChild(newDuckSmall);
arrayDuckSmall.push(newDuckSmall);
newDuckSmall.scaleX = 1;
newDuckSmall.scaleY = newDuckSmall.scaleX;
newDuckSmall.x = origX;
newDuckSmall.y = origY;
arrayDuckSmall.addEventListener(Event.ENTER_FRAME, moveDuckSmall);
trace(?);
}
Would it help to add names to the DuckSmall's inside the addDuck function?
for ex:
newDuckSmall.name = "duckSmall" + arrayDuckSmall.length;
Would that help when adding the EventListeners?
Copy link to clipboard
Copied
If you trace the value of i when you add the event listener that should be sufficient to identify which duck it is being assigned to.
trace( i, "EventListener added");
trace( i, "EventListener removed");
Copy link to clipboard
Copied
function addDuck(): void
{
newDuckSmall = new partVaike();
addChild(newDuckSmall);
arrayDuckSmall.push(newDuckSmall);
newDuckSmall.scaleX = 1;
newDuckSmall.scaleY = newDuckSmall.scaleX;
newDuckSmall.x = origX;
newDuckSmall.y = origY;
arrayDuckSmall.addEventListener(Event.ENTER_FRAME, moveDuckSmall);
trace( i, "EventListener added");
}
gives the output of "0 EventListener added" after each small duck added to the stage
Should it be different?
function stopDucks(event: MouseEvent): void
{
PartSuur.removeEventListener(Event.ENTER_FRAME, moveDuckBig);
for(var i: int = 0; i < arrayDuckSmall.length; i++)
{
arrayDuckSmall.removeEventListener(Event.ENTER_FRAME, moveDuckSmall);
trace(i, "EventListener removed");
}
PartSuur.addEventListener(MouseEvent.CLICK, addEnterFrame);
}
gives the output:
"0 EventListener removed"
"1 EventListener removed"
"2 EventListener removed"
function addEnterFrame(event: MouseEvent): void
{
PartSuur.removeEventListener(MouseEvent.CLICK, addEnterFrame);
PartSuur.addEventListener(Event.ENTER_FRAME, moveDuckBig);
for(var i: uint = 0; i < arrayDuckSmall.length; i++)
{
arrayDuckSmall.addEventListener(Event.ENTER_FRAME, moveDuckSmall);
trace(".EventListener added");
}
PartSuur.addEventListener(MouseEvent.CLICK, stopDucks);
}
gives the output:
"0 EventListener added"
"1 EventListener added"
"2 EventListener added"
I tested the code, when only one small duck was added, and then all worked perfectly.
But when there are already two small ducks added, they start to speed up. Both of them, the small ducks. Not only the first one.
Copy link to clipboard
Copied
The value of i in the addDuck function is not working, as I indicated. If it is always 0 then it is always adding an event listener to the first duck added to the array. You should change that line from...
arrayDuckSmall.addEventListener(Event.ENTER_FRAME, moveDuckSmall);
to
newDuckSmall.addEventListener(Event.ENTER_FRAME, moveDuckSmall);
so that it is added to the duck that was just added, not the first duck that was added.
A change in your approach to this could help alleviate issues with having too many listeners getting created. Just create one ENTER_FRAME event listener for everything and use that for control of all movement. In the event handler function you have for moving the main duck, also include code for moving the small ducks. Use the arrayDuckSmall to target them so that you are only targeting the ones currently added, if any.
When you want to stop the ducks you can either remove that one event listener, or you can put a conditional around the movement code that prohibits it from moving any ducks when you want them to stop. To start them back up again you either re-add the listener or change the conditional control to allow movement. The logic for the conditional approach would be along the lines of...
var allowMovement:Boolean = true;
stage.addEventListener(Event.ENTER_FRAME, moveDucks);
function moveDucks(evt:Event):void {
if(allowMovement){
// move main duck
// loop thru small ducks array and move whoever is in that array
}
}
function stopDucks(evt:MouseEvent):void {
allowMovement = false; // the only line need to stop all ducks
}
Copy link to clipboard
Copied
Hi,
I'm testing different versions.
the change in the addDuck() function:
when I change the
arrayDuckSmall.addEventListener(Event.ENTER_FRAME, moveDuckSmall);
to
newDuckSmall.addEventListener(Event.ENTER_FRAME, moveDuckSmall);
then the trace statement output is the same 0 EventListener added
Isn't it because the new duck is pushed to the first position in the array?
When with the version "arrayDuckSmall" the ducks move as expected until for the first stop-resume on the big duck, then with the version "newDuckSmall" the small ducks speed up as soon as the second duck appears on the stage.
So at this point it makes it worse.
I make some more tests based on your recommendations above, changing the structure a bit.
And I will late you know.
Copy link to clipboard
Copied
The Array.push method places the added item at the end of the array,not the start. So when you use i and i is always 0 it is always assigning to ther first duck added to the array.
I think you will find it alot easier to manage if you limit the ENTER_FRAME listeners to just one of them.
Copy link to clipboard
Copied
/*-----***** VAR DUCK BIG *****-----*/
var parentOrigX: Number = PartSuur.parent.width / 2;
var origX: Number;
var origY: Number;
/*-----***** VAR DUCK SMALL *****-----*/
var newDuckSmall: partVaike;
var newDuckSmallTween: Tween;
var arrayDuckSmall: Array = new Array();
var i: int;
/*-----***** FUNCTIONS DUCK BIG *****-----*/
origX = PartSuur.x;
origY = PartSuur.y;
PartSuur.addEventListener(MouseEvent.CLICK, addEnterFrame);
function addEnterFrame(event: MouseEvent): void
{
PartSuur.removeEventListener(MouseEvent.CLICK, addEnterFrame);
PartSuur.addEventListener(Event.ENTER_FRAME, moveDucks);
for(var i: int = 0; i < arrayDuckSmall.length; i++)
{
arrayDuckSmall.addEventListener(Event.ENTER_FRAME, moveDucks);
trace(i, "EventListener added");
}
PartSuur.addEventListener(MouseEvent.CLICK, stopDucks);
}
function moveDucks(event: Event): void
{
PartSuur.x += 0.5;
for(var i: int = 0; i < arrayDuckSmall.length; i++)
{
arrayDuckSmall.x += 0.5;
}
if(PartSuur.x == origX + 75)
{
addDuck();
}
if(PartSuur.x == origX + 125)
{
addDuck();
}
if(PartSuur.x == origX + 175)
{
addDuck();
}
}
function stopDucks(event: MouseEvent): void
{
PartSuur.removeEventListener(Event.ENTER_FRAME, moveDucks);
for(var i: int = 0; i < arrayDuckSmall.length; i++)
{
arrayDuckSmall.removeEventListener(Event.ENTER_FRAME, moveDucks);
trace(i, "EventListener removed");
}
PartSuur.addEventListener(MouseEvent.CLICK, addEnterFrame);
}
/*-----***** FUNCTIONS DUCK SMALL *****-----*/
function addDuck(): void
{
newDuckSmall = new partVaike();
addChild(newDuckSmall);
arrayDuckSmall.push(newDuckSmall);
trace(arrayDuckSmall.length);
newDuckSmall.scaleX = 1;
newDuckSmall.scaleY = newDuckSmall.scaleX;
newDuckSmall.x = origX;
newDuckSmall.y = origY;
}
Copy link to clipboard
Copied
I posted adjusted code.
Only the "var newDuckSmallTween: Tween;" is excessive.
I deleted the addListener from the addDuck()
and combined the enter frame listeners.
Was that what you described above? Hope I understood you correctly.
But ... its still not working as expected.
Now they speed up all together, including the big duck. After the first stop-resume clicks.
Any ideas?
Copy link to clipboard
Copied
You are still adding multiple ENTER_FRAME event listeners. In the addEnterFrame function remove the for loop entirely. Similarly, remove the loop that removes those listeners in the stopDucks function. Each of those listeners will end up calling the moveDucks function, which moves all the ducks.... you only want one listener calling that function and the one assigned to the big duck does that.
Copy link to clipboard
Copied
Thank you Ned for the help and being so patient with me.
Its working nicely.
I learned a lot again. As usual, things are simple when we do not make them difficult.
Copy link to clipboard
Copied
You're welcome
Find more inspiration, events, and resources on the new Adobe Community
Explore Now