-
Senior Member
[RESOLVED] CS4 AS3 - RemoveEventListener Error
Hey,
I have this code:
Code:
stage.addEventListener(Event.RESIZE,sizeVideo, false, 0, true);
backVideo.width = stage.stageWidth;
backVideo.scaleY = backVideo.scaleX;
function sizeVideo(e:Event){
if(stage.stageWidth > 990){
backVideo.width = stage.stageWidth;
backVideo.scaleY = backVideo.scaleX;
}
if(stage.stageHeight > 500){
mainContent.y = stage.stageHeight-243;
}
}
just scales the background in the movie...works fine
I have one page I don't want this to happen anymore so want to remove the listener. I have this in a movie:
Code:
function changeBack(e:Event){
var bg:Object = this.parent.parent.getChildByName("siteBackground");
while(bg.numChildren > 0){
bg.removeChildAt(0)
}
bg.scaleX = 1;
bg.scaleY = 1;
trace(stage)
stage.removeEventListener(Event.RESIZE,sizeVideo, false);
var obj:Object = e.target;
obj.content.height = stage.stageHeight;
obj.content.scaleX = obj.content.scaleY;
obj.content.x = (stage.stageWidth/2)-(obj.content.width/2);
if(stage.stageWidth > obj.content.width){
var duplicates:Number = Math.ceil(stage.stageWidth/obj.content.width)*2;
var sidesAdded:Number = 0;
for(i=0;i<duplicates;i++){
this['dupe'+i] = new MovieClip();
var duplicationBitmap:Bitmap = new Bitmap(Bitmap(obj.content).bitmapData);
duplicationBitmap.width = obj.content.width;
duplicationBitmap.height = obj.content.height;
if(i%2 == 0){
this['dupe'+i].x = obj.content.x - obj.content.width - (sidesAdded*obj.content.width);
}else{
this['dupe'+i].x = obj.content.x + obj.content.width + (sidesAdded*obj.content.width);
sidesAdded += 1;
}
this['dupe'+i].addChild(duplicationBitmap);
bg.addChild(this['dupe'+i]);
}
}
bg.addChild(obj.content);
}
but the remove listener line errors: 1120:Access of undefined property sizeVideo
Any help is most appreciated
-
if(sizeVideo.parent != null) {
stage.removeEventListener(Event.RESIZE,sizeVideo, false);
}
Hope that solves.
-
No, that won't help. For one, sizeVideo is a function, not a display object and would not have a parent property. For two, that would cause the same error by trying to access the undefined property sizeVideo.
The actual problem is that sizeVideo is not defined in the scope of the changeBack function. This could be because changeBack is defined on a different frame than sizeVideo, or a different object. To resolve it, you need to save a reference to sizeVideo in a scope where it is accessible to changeBack and use that reference. The particulars will depend on where exactly each of those code blocks is defined. Are they on different frames? Different movies?
-
Total Universe Mod
Even better, you could encapsulate the add and remove listener commands in a central location and trigger those from anywhere in the site rather than tightly coupling references.
-
Senior Member
Yes they are in different places....different frames and sizeVideo is on the stage timeline....the code to stop it is in a movieclip attached to another movieclip on the main time line.
How would I encapsulate the add and remove listener commands in a central location?
Thanks for help
-
Senior Member
-
One way would be to use an event listener at the root level, and dispatch a new event from the place you want to trigger the function.
in the root (where you currently have sizeVideo defined):
Code:
function stopSizing(e:Event):void{
stage.removeEventListener(Event.RESIZE, sizeVideo, false);
}
addEventListener("stopSizing", stopSizing);
In any displayObject (must be on the displayList) which you want to trigger stopSizing:
Code:
dispatchEvent(new Event("stopSizing", true));
This way, the event will bubble upward until it is received by the root, which will call the stopSizing function. If your stuff were more class-based, you could have more directly implemented jAQUAN's suggestion by creating static methods in your main class to add and remove that listener.
-
Senior Member
Splendid good sirs
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|