Actionscript code not executing but no compiler errors - actionscript-3

Below is the code I did for a basic button and menu interaction using flash tween and ease class for animation. Tested movie but no response and no compiler errors either except output panel says :
TypeError: Error #1123: Filter operator not supported on type builtin.as$0.MethodClosure.
at CloudRail_fla::LaunchDeckcontrols_3/frame1()
where Cloudrail is my document name.
Here is the code :
switchbd_btn.addEventListener.(MouseEvent.MOUSE_DOWN, ShowswitchBD);
var switchbdIN:Tween = new Tween (switchbd, "x", Strong.easeOut, 1089.05, 277.85, 1, true);
var switchbdOUT:Tween = new Tween (switchbd, "x", Strong.easeOut, 277.85, 1089.05, 1, true);
function ShowswitchBD(e:MouseEvent):void {
if (switchbd.currentFrame == 1)
{
gotoAndStop(2);
switchbdIN.start();
}
else {
gotoAndStop(1);
switchbdOUT.start();
}
}`
please what could be wrong?

switchbd_btn.addEventListener.(MouseEvent.MOUSE_DOWN, ShowswitchBD);
should be
switchbd_btn.addEventListener(MouseEvent.MOUSE_DOWN, ShowswitchBD);

Related

unable to remove movieclip itself from stage as3

I had a button, with that button a movieclip is called on the stage and it is working fine with the below script.
I just updated the script and it is now working fine with the removing of movieclip from the stage, but now the issue came with playing the movieclip. Now the movieclip is not playing well.
var btn: btn_Lemon = new btn_Lemon();
var mc: mc_lemon = new mc_lemon();
addChild(btn);
btn.x = 304.45;
btn.y = 209.8;
btn.addEventListener(MouseEvent.MOUSE_DOWN, login, false, 0, true);
function login(event: MouseEvent): void {
stage.addChild(mc);
mc.x = 304.45;
mc.y = 209.8;
mc.addEventListener(Event.ENTER_FRAME, fadeOver, false, 0, true);
}
function fadeOver(event: Event): void {
if (event.currentTarget.currentFrame == 25) {
event.currentTarget.removeEventListener(Event.ENTER_FRAME, fadeOver);
stage.removeChild(mc);
}
}
What is going on please help somebody
Presuming it's a timeline you're trying to play inside lemon_mc and it's the play method that doesn't seem to run properly.
In my experience Flash can sometimes be a bit fickle with playing as I recall.
Here are some pointers for pushing the MC to play:
Force the a mc to play at the end of the
function login(event: MouseEvent): void {
stage.addChild(mc);
mc.x = 304.45;
mc.y = 209.8;
mc.addEventListener(Event.ENTER_FRAME, fadeOver, false, 0, true);
mc.play();
}
Simple add
play();
at the first frame inside the movie clip you want to play.
Also if the playback is a bit fickle at certain scenarios you can define with code, then you can use a test:
if (mc.isPlaying == false) {
mc.play();
}
As a beginner I found the as3 documentation from adobe to be easy to understand and apply. You might find some other properties to read or methods you can manipulate to force the play method:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/MovieClip.html

How to correctly load und unload swf galleries for PORTFOLIO web site?

My problem looks like: I have five buttons and four of them are loading four different swf galleries into the main swf, the last one unloads all galleries. Each of them has exactly the same code, the only differeces are the locations of jpg files and swf files. Also each o them has the listeners ADDTOSTAGE and REMOVEFROMSTAGE, because i'm using the stage inside the gallery/swfs.
So when I enter the menu and load the first swf i don't get any output errors, when I click the btn_back he unloads the swf properly with no errors. And so on but only when i load only one gallery.
But when I, after loading the first gallery, want to click on another button and load second gallery then i got this error in output, so many times as many pictures are loaded inside the loaded swf/gallery:
TypeError: Error #1009: Nie można uzyskać dostępu do właściwości lub metody dla odniesienia do obiektu null.
at DocObject/resizeMyDoc()
at DocObject/onResizeDoc()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.display::Stage/dispatchEvent()
at Miniaturka/onAddedToStage()
at flash.display::DisplayObjectContainer/addChild()
at Miniaturki/callback()
at Function/http://adobe.com/AS3/2006/builtin::apply()
at Miniaturka/onLoadComplete()
So my conclusion is that the problem is not inside the loaded swf but in the code of loading and unloading code of the mine swf. The only solution that I can find is: that when I click the second button the code should first unload the current swf, and when the unload is complete then immidietly load the another one. But I don't know how to write it. Can anyone help? So here is the code.
import flash.events.MouseEvent;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
import flash.display.Loader;
var _mainLoader:Loader;
var _currentContent:DisplayObject;
var url:URLRequest;
var loaded:Boolean = false;
var a:Array = new Array(s1_1_arch, s1_2_arch, s1_3_arch, s1_4_arch);
var swfList:Array = ["A0101.swf", "A0102.swf", "A0103.swf"];
//Initiate Loader, do it only once
function initiateLoader():void {
_mainLoader = new Loader();
_mainLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
}
function loadGallery(path:String):void {
//Check if content previously loaded, unload it
if (_currentContent != null) {
removeChild(_currentContent);
_currentContent = null;
_mainLoader.unloadAndStop();
}
//Now load another one
_mainLoader.load(new URLRequest[1]);
}
function onComplete(e:Event):void {
_currentContent = _mainLoader.content;
//Add root DisplayObject of the loaded content to the display list
stage.addChild(_currentContent);
}
function samClick(e:MouseEvent):void
{
switch (e.target)
{
case s1_1_arch :
break;
case s1_2_arch :
break;
case s1_3_arch :
break;
case s1_4_arch :
break;
}
}
function samOver(e:MouseEvent):void{
switch (e.target)
{
case s1_1_arch : var sam1Ou:Tween = new Tween(s1_1_arch, "alpha", Strong.easeOut, 1, 0.2, 0.5, true);
break;
case s1_2_arch : var sam2Ou:Tween = new Tween(s1_2_arch, "alpha", Strong.easeOut, 1, 0.2, 0.5, true);
break;
case s1_3_arch : var sam3Ou:Tween = new Tween(s1_3_arch, "alpha", Strong.easeOut, 1, 0.2, 0.5, true);
break;
case s1_4_arch : var sam4Ou:Tween = new Tween(s1_4_arch, "alpha", Strong.easeOut, 1, 0.2, 0.5, true);
break;
}
}
function samOut(e:MouseEvent):void{
switch (e.target)
{
case s1_1_arch : var sam1Ou:Tween = new Tween(s1_1_arch, "alpha", Strong.easeOut, 0.2, 1, 0.5, true);
break;
case s1_2_arch : var sam2Ou:Tween = new Tween(s1_2_arch, "alpha", Strong.easeOut, 0.2, 1, 0.5, true);
break;
case s1_3_arch : var sam3Ou:Tween = new Tween(s1_3_arch, "alpha", Strong.easeOut, 0.2, 1, 0.5, true);
break;
case s1_4_arch : var sam4Ou:Tween = new Tween(s1_4_arch, "alpha", Strong.easeOut, 0.2, 1, 0.5, true);
break;
}
}
for (var i:Number = 0; i < 4; i++)
{
a[i].addEventListener(MouseEvent.CLICK, samClick);
a[i].addEventListener(MouseEvent.MOUSE_OVER, samOver);
a[i].addEventListener(MouseEvent.MOUSE_OUT, samOut);
}
Just to add...
For something like this you are better of having all code in one place. A class file will save you other frustrations later on if you're still new. Just control all elements from one place. Also consider what Nicolas Siver said in his answer..
Just a quick guide on how to setup class file. If you save as Main.as and attach to your FLA then you can paste in your timeline code here.
package
{
//IMPORTS go here
//Declare your Class (name of .as file)
public class Main extends MovieClip
{
//VARS go here
//Declare main function of your Class (must have same name as Class (.as file)
public function Main()
{
// Your main program code go here
//Something(); //example: run a function called Something
}
public function Something();
{
// Do something code
}
} //End of Class
} //End of Package
You are on the right way. But you have many problems in your code, most of your switches are redundant. By default, all galleries, that you are loading, will be loaded in child ApplicationDomains, so they are suitable for garbage collection.
If you will use single Loader it will be easier for you to manage it, and load one section in time.
//Pseudo code
Create main loader (with onComplete event handler);
Create a function for loading content, and call it.
Check if previous load operation was successful (use your flag `loaded`), if Yes - unload previous content, remove previous content from the display list;
Load new content;
After loading is completed, add content to the main display list holder, designed to show gallery content.
Also I would recommend to use TweenLite, It's also more developer friendly.
Here for you several utility functions that will help you accomplish your task:
private var _mainLoader:Loader;
private var _currentContent:DisplayObject;
//Initiate Loader, do it only once
private function initiateLoader():void {
_mainLoader = new Loader();
_mainLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
}
private function loadGallery(path:String):void {
//Check if content previously loaded, unload it
if (_currentContent != null) {
removeChild(_currentContent);
_currentContent = null;
_mainLoader.unloadAndStop();
}
//Now load another one
_mainLoader.load(new URLRequest(path));
}
private function onComplete(e:Event):void {
_currentContent = _mainLoader.content;
//Add root DisplayObject of the loaded content to the display list
addChild(_currentContent);
}
More concrete realisation, you could use it in your frame, but don't forget to do all imports, and create all necessary display object on the scene:
//Self explanatory
var currentContent:DisplayObject;
//Initiate Loader
var mainLoader:Loader = new Loader();
mainLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
//Create some MovieClip and give it a name, you will place loaded content there, ex: contentHolder
//Add buttons in one container (MovieClip) and give it name, ex: buttonHolder
//Give buttons some names, ex: s1Arch, s2Arch, s3Arch
//Map our buttons to external links and prepare for mouse events
var links:Object = {"s1Arch": "A0101.swf", "s2Arch": "A0102.swf", "s3Arch": "A0103.swf"};
//Register listeners, you could do it in cycle
s1Arch.addEventListener(MouseEvent.CLICK, onClickButton);
s2Arch.addEventListener(MouseEvent.CLICK, onClickButton);
s3Arch.addEventListener(MouseEvent.CLICK, onClickButton);
//Mouse events, using events bubbling
buttonHolder.addEventListener(MouseEvent.CLICK, onClickButton);
function loadGallery(path:String):void {
//Check if content previously loaded, unload it
if (currentContent != null) {
contentHolder.removeChild(currentContent);
currentContent = null;
mainLoader.unloadAndStop();
}
//Now load another one
mainLoader.load(new URLRequest(path));
}
function onClickButton(e:MouseEvent):void {
//Explanation step-by-step
//Clicked button
var button: DisplayObject = DisplayObject(e.currentTarget);
//Path to the external file
var path: String = links[button.name];
//Utility function will handle another stuff
loadGallery(path);
}
function onComplete(e:Event):void {
currentContent = mainLoader.content;
//Add root DisplayObject of the loaded content to the display list
contentHolder.addChild(currentContent);
}

1120: Access of undefined property

I am getting this error message when I try to access a button movieclip from inside a videoPlayer movieclip. They are not nested. When the videoPlayer button is clicked the buttons tween to the bottom of the screen out of the way whilst the videoPlayer sits in their previous location. On closing the videoPlayer I would like the buttons to tween back in from the right of the screen but I get the following error message "1120: Access of undefined property TweenMax.from(btn1, 3, {x:450, y:209.1, ease:Back.easeOut});. My question is what would I use in as3 to access control of the buttons so I can tween them on closing the videoPlayer movieclip. Any pointers would be greatly appreciated.
The flvPlayer script:-
stop();
closeBtn.addEventListener(MouseEvent.CLICK, closeVid)
closeBtn.buttonMode = true;
function closeVid(e:MouseEvent):void
{
video.stop();
gotoAndStop(1);
TweenMax.from(btn1, 3, {x:450, y:209.1, ease:Back.easeOut});
}
**********************************************************************
The script from outside the videoPlayer or scene1:-
import com.greensock.*;
import com.greensock.easing.*;
var numberOfButtons:Number = 3
var startX:Number;
var startY:Number;
var currentButton:MovieClip
for (var i:int = 1; i <= numberOfButtons; i++)
{
this["btn"+i].buttonMode = true;
this["btn"+i].mouseChildren = false;
this["btn"+i].addEventListener(MouseEvent.MOUSE_OVER, doRollover)
this["btn"+i].addEventListener(MouseEvent.MOUSE_OUT, doRollout)
this["btn"+i].addEventListener(MouseEvent.CLICK, showbtnContent);
}
function doRollover(e:MouseEvent):void
{
TweenMax.to(this[e.target.name], 1, {scaleX:1.1, scaleY:1.1, ease:Elastic.easeOut});
}
// This function handles the Rollout event
function doRollout(e:MouseEvent):void
{
TweenMax.to(this[e.target.name], 1, {scaleX:1, scaleY:1, ease:Elastic.easeOut});
}
function showbtnContent(e:MouseEvent):void
{
currentButton = this[e.target.name]
switch (currentButton)
{
case btn1:
TweenMax.from(audioPlayer, 1, {x:449, y:-112, ease:Back.easeOut});
audioPlayer.gotoAndStop(2);
audioPlayer.parent.setChildIndex(audioPlayer, audioPlayer.parent.numChildren-1);
break;
case btn2:
TweenMax.from(mcGallery, 1, {x:450, y:300, scaleX:1, scaleY:1, ease:Back.easeOut});
mcGallery.gotoAndStop(2);
mcGallery.parent.setChildIndex(mcGallery, mcGallery.parent.numChildren-1);
break;
case btn3:
TweenMax.to(btn1, 20, {x:450, y:777.1, ease:Elastic.easeOut});
TweenMax.to(btn2, 16, {x:450, y:777.1, ease:Elastic.easeOut});
TweenMax.to(btn3, 12, {x:450, y:777.1, ease:Elastic.easeOut});
TweenMax.from(flvPlayer, 3, {x:1060, y:280, ease:Back.easeOut});
flvPlayer.gotoAndStop(2);
flvPlayer.parent.setChildIndex(flvPlayer, flvPlayer.parent.numChildren-1);
break;
}
}
when you use gotoAndStop(1); it moves to the first frame, where the btn1,btn2 and btn3 did not exist.
fixing it will be tough, because you'll need to move the timeline into a separate MovieClip from the MovieClip or Sprite that is holding the buttons, so that this does not happen.

Flash Action Script 3 rotate button

Could anyone tell me whats wrong with this code? I'm attempting to rotate a button in action script 3 and i keep getting the error:
ArgumentError: Error #2025: The supplied DisplayObject must be a child
of the caller. at flash.display::DisplayObjectContainer/removeChild()
at
distributor_app_fla::MainTimeline/NewChartOptionsReturn()[distributor_app_fla.MainTimeline::frame1:218]
at
distributor_app_fla::MainTimeline/ClickNewChartOptions()[distributor_app_fla.MainTimeline::frame1:101]
I've already Googled the error and everything i read told me to remove the child then re-add it to the frame but it continues to break at the same spot.
code:
//defined
var btnNewChartOptions:NewChartOptions = new NewChartOptions();
btnNewChartOptions.y = 279;
btnNewChartOptions.x = 439;
//created
function NewChartDown():String
{
btnNewChartOptions.addEventListener(MouseEvent.CLICK, ClickNewChartOptions);
btnNewChartOptions.alpha = 0;
addChild(btnNewChartOptions);
var NewChartOptionsTween:Tween = new Tween(btnNewChartOptions, "alpha", Strong.easeOut, 0, 1, 1, true);
return "NewChartSelected";
}
//actual code on button
function NewChartOptionsDown():String
{
rightGrayOut.alpha = 0;
addChild(rightGrayOut);
var grayOutTween:Tween = new Tween(rightGrayOut, "alpha", Strong.easeOut, 0, 1, 1, true);
var rotateTween:Tween = new Tween(btnNewChartOptions, "rotation", Strong.easeOut, 0, 180, 1, true);
return "NewChartOptions";
}
any help is appreciated!
There will be no need to remove and re-add the object because it is either present and available to the method or not, meaning if it were available to the method for removal then it would also be available for addressing and manipulation. This sounds a whole lot like a scope/visibility issue.
This code is on frame 1 on the timeline, which means that it should be able to address anything that's directly on the stage or assigned to a variable that is in the same scope at the time the function is run. (In the _root scope if this was AS2, like the methods on frame 1)
Is btnNewChartOptions inside another DisplayObject, like a sprite you use to hold all your buttons or something, as opposed to sitting directly on the stage? Maybe you can describe the object heirarchy you are trying to address as well as how the button gets attached to the stage (e.g. instantiated and attached at runtime or placed on the stage in a keyframe).
Can you provide the error that was being thrown before the add and remove fix was attempted?
If looks like to me the object has not been added yet to the display list.
// replace the following lines
removeChild(btnNewChartOptions);
addChild(btnNewChartOptions);
// with
if( !this.contains( btnNewChartOptions ) ){
return "";
}
[EDIT]
Your code.
//var created
var btnNewChartOptions:NewChartOptions = new NewChartOptions();
btnNewChartOptions.y = 279;
btnNewChartOptions.x = 439;
//button code clicked that creates button
function NewChartDown():String {
btnNewChartOptions.addEventListener(MouseEvent.CLICK, ClickNewChartOptions);
btnNewChartOptions.alpha = 0;
addChild(btnNewChartOptions);
var NewChartOptionsTween:Tween = new Tween(btnNewChartOptions, "alpha", Strong.easeOut, 0, 1, 1, true);
return "NewChartSelected";
}
//function for button
function NewChartOptionsDown():String {
rightGrayOut.alpha = 0;
addChild(rightGrayOut);
var grayOutTween:Tween = new Tween(rightGrayOut, "alpha", Strong.easeOut, 0, 1, 1, true);
var rotateTween:Tween = new Tween(btnNewChartOptions, "rotation", Strong.easeOut, 0, 180, 1, true);
return "NewChartOptions";
}
In your code you provided you are only doing addChild(btnNewChartOptions); in the NewChartDown function.
So that tells me that NewChartOptionsDown can not be called until NewChartDown has been called first. Because the btnNewChartOptions has never been added.
What you can do is move addChild(btnNewChartOptions); outside of the function.
//var created
var btnNewChartOptions:NewChartOptions = new NewChartOptions();
btnNewChartOptions.y = 279;
btnNewChartOptions.x = 439;
addChild(btnNewChartOptions);

Removing Tween For Garbage Collection in AS3

I'm trying to remove a tween object after it has complete so the memory can be freed by garbage collection.
In this example I'm passing the fadeIn function a UILoader object that is cast as a sprite so that it fades in when it is finished loading. When the tween finishes animating, I want to remove the tween object. I've included the compiler errors as comments.
function fadeIn(e:Sprite):void
{
var myTween:Tween = new Tween(e, "alpha", None.easeNone, 0.0, 1.0, 0.2, true);
myTween.addEventListener(Event.COMPLETE, deallocateObject, false, 0, true);
}
function deallocateObject(e:Event):void
{
//delete(e.currentTarget); //Warning: 3600: The declared property currentTarget cannot be deleted. To free associated memory, set its value to null.
e.currentTarget = null; //1059:Property is read-only.
}
First of all, you want to use a TweenEvent to handle the completion of the tween. The currentTarget property of Event is read-only, so you need to "get" the current target from the event and cast it as a tween, then remove your event and set it to null:
// assuming MC on stage with instance name "test"
import fl.transitions.*;
import fl.transitions.easing.*;
function fadeIn(e:Sprite):void
{
var myTween:Tween = new Tween(e, "alpha", None.easeNone, 0.0, 1.0, 1, true);
myTween.addEventListener(TweenEvent.MOTION_FINISH, deallocateObject, false, 0, true);
}
function deallocateObject(e:TweenEvent):void
{
var myTween:Tween = e.currentTarget as Tween;
// -- I always remove even when using weak listeners
myTween.removeEventListener(TweenEvent.MOTION_FINISH, deallocateObject);
myTween = null;
}
fadeIn(test);
Watch out when using local Tweens inside a function. Often they will get garbage collected before the tween completes. You'll have to declare the tween as a class property instead if that happens. I recommend saving yourself some headache and using Tweener, gTween, et al. The Tween class sucks.
function fadeIn(e:Sprite):void
{
var myTween:Tween = new Tween(e, "alpha", None.easeNone, 0.0, 1.0, 0.2, true);
myTween.addEventListener(TweenEvent.MOTION_FINISH, deallocateObject);
}
function deallocateObject(e:Event):void
{
delete(e.currentTarget as Tween);
}
This works.