As3 - script work at root timeline but not inside MovieClip - actionscript-3

Please help me inspect this code:
function btntxt(target:String, txt:String):void
{
var button:MovieClip = MovieClip(this.getChildByName(target));
** var btnText:TLFTextField = TLFTextField(button.getChildByName("btnText"));
btnText.text = txt;
button.gotoAndStop(1);
button.buttonMode = true;
button.useHandCursor = true;
button.addEventListener(MouseEvent.MOUSE_OVER,overListener);
button.addEventListener(MouseEvent.MOUSE_OUT,outListener);
button.addEventListener(MouseEvent.MOUSE_DOWN,clickListener);
button.addEventListener(MouseEvent.MOUSE_UP,upListener);
}
When I debug, it gives me error at **:
1046: Type was not found or was not a compile-time constant: TLFTextField.
1180: Call to a possibly undefined method TLFTextField.
Also output error 1065. This code works at the top level but when I copy it inside a Movieclip's timeline, it doesn't work! Why?
If this information is short, please tell me.

Add the import statement at the top of your code:
import fl.text.TLFTextField;

Related

How to fix 'TypeError: Error #1009' error in ActionScript3.0 Adobe Animate

I'm setting up a button on the first frame which when clicked will transfer the user to the 2nd frame using this code:
stop();
Btn_1.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndPlayFromFrame_2);
function fl_ClickToGoToAndPlayFromFrame_2(event:MouseEvent):void
{
gotoAndPlay(2);
}
and on the second frame, I set up a dynamic text that will perform a countdown using this code:
var myTimer:Timer = new Timer(1000,60); // every second for 60 seconds
myTimer.addEventListener(TimerEvent.TIMER, onTimer);
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onComplete);
myTimer.start();
function onTimer(e: TimerEvent):void {
countdown_text.text = String(myTimer.repeatCount - myTimer.currentCount);
}
function onComplete(e: TimerEvent):void{
gotoAndStop(3);
}
the thing is keep getting TypeError: Error #1009 message after debugging it. I know the fault is in line 7 of the 2nd code but I have no idea what is wrong with it. Pls help!
I should see your source fla, but it is most likely related to countdown_text not being accessible in that frame. Error description is "Cannot access a property or method of a null object reference", that means it cannot find the reference which is "countdown_text".
It is very very bad practice to write AS directly in frames. Convert code into a class and assign it as a document class.
You can find Adobe documentation for document class here: https://helpx.adobe.com/animate/using/actionscript-publish-settings.html

AS3 - types and classes

I'm not exactly sure but i'm guessing my problem has to do with how i'm declaring my variables.
Is the below code legal in AS3?
var fish1:Fish = new Fish;
var fish2:Fish = new Fish;
var fish3:Fish = new Fish;
var fish4:Fish = new Fish;
addChild(fish1);
addChild(fish2);
addChild(fish3);
addChild(fish4);
fish1.x = 0;
fish2.x = 150;
fish3.x = 300;
fish4.x = 450;
i'm getting compiler errors for each line of addChild saying:
Main.as, Line 14 1180: Call to a possibly undefined method addChild.
Main.as, Line 14 1120: Access of undefined property fish3.
and for every line where i'm specifying the x coordinates of my fish i'm getting
a compiler error saying
Main.as, Line 15 1120: Access of undefined property fish4.
the fish variables are of type Fish and I've defined them in my library in my .fla file.
Thank you in advanced!
Your Class needs to subclass some form of DisplayObjectContainer, of which MovieClip and Sprite are two possible choices (find out, be sure).
But I suspect that the real probem is you're writing Class code like it's timeline code. I think you probably have strict mode off, which is why you're ot getting helpful compile-time errors which would help anyone who's familiar with AS3 (though probably not you) to figure out immediately that your code should look more like
class Main extends Sprite {
public var fish1:Fish = new Fish();
public var fish2:Fish = new Fish();
public var fish1:Fish = new Fish();
public function Main() {
addChild(fish1);
addChild(fish2);
addChild(fish3);
//not going to type this crap.
//positioning code (and addChild) is a waste of time.
//that's what the stage is for!
}
}

ActionScript code problems

I have only recently started to learn ActionScript 3.0 I was practising in Flash and i ran into this problem:
Scene 1, Layer 'Layer 1', Frame 1, Line13 1119: Access of possibly undefined property dosomething through a reference with static type flash.net:SharedObject.
What i am tring to do is use the SharedObject.send method to send a message obviously. I have edited some server side code in my main.asc file. And i am trying to pass in the doSomething function but i get that compile error.
Any advice would be appreciated for a novice like myself.
The code is below:
import flash.net.NetConnection;
import flash.net.SharedObject;
var nc:NetConnection = new NetConnection();
nc.connect("rtmp:/exampletest/");
var so:SharedObject = SharedObject.getRemote("foo", nc.uri, true);
so.connect(nc);
so.dosomething = new function(str) {
If you want to pass a function between SWFs, attach the function to the .data member of the SharedObject returned by SharedObject.getLocal/Remote, not the SharedObject itself.
So:
so.data.doSomething = yourFunction
...should work. I'm not exactly sure what you're trying to achieve, does this sound like a solution?

AS3 TypeError: Error #1009

I am trying to create a web application with multiple scenes, the error appears when I try to access the next scene with a button I created that contains multiple EventListeners for animation purposes.
The Button did bring me to the next scene, but the error still occurs. After tracing and debugging, the error seems to occur at the Mouse_Out event.
I am still very new to AS3, so can someone please explain to me where my code went wrong and if possible, correct the error for me or is there a better way of writting the code? Thanks in advance.
The Error Involved:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at fl.transitions::Tween/setPosition()
at fl.transitions::Tween/set position()
at fl.transitions::Tween()
at Portfolio_fla::MainTimeline/about_btnOut()
My Code:
import flash.events.MouseEvent;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
about_btn.buttonMode = true;
about_btn.mouseChildren = false;
about_btn.alpha = 0.3;
about_btn.addEventListener(MouseEvent.MOUSE_OVER, about_btnOver);
function about_btnOver(event:MouseEvent):void
{
var AboutAlphaOver:Tween = new Tween(about_btn,"alpha",Regular.easeIn,0.3,1,0.1,true);
}
about_btn.addEventListener(MouseEvent.MOUSE_OUT, about_btnOut);
function about_btnOut(event:MouseEvent):void
{
var AboutAlphaOut:Tween = new Tween(about_btn,"alpha",Regular.easeIn,1,0.3,0.1,true);
}
about_btn.addEventListener(MouseEvent.CLICK, about_btnClick);
function about_btnClick(event:MouseEvent):void
{
gotoAndPlay(1,"About");
}
Try to change your Tween code:
var AboutAlphaOut:Tween = new Tween(about_btn,"alpha",Regular.easeIn,1,0.3,0.1,true);
To:
var AboutAlphaOut:Tween = new Tween(event.currentTarget,"alpha",Regular.easeIn,1,0.3,0.1,true);
I'm not sure if it will work (I'm not too familiar with Flash IDE), but I think it's possible that you are getting error because other scene doesn't have a reference to a button. With event.currentTarget, you will search for reference in the event, so it should find it in any case.
BTW: You shouldn't name your variables starting by capital letter. That way you will more easily distinguish objects from classes.

Error #1034 while running flash application

Alright, I know that this error is occurring because of something to do with addchild() function. On frame 2. How do I know? Because when I remove the snippets of code that dynamically place an object onto the stage it works fine. The error is below and the source code for frame 2 is below the error messages.
Error Code
TypeError: Error #1034: Type Coercion failed: cannot convert 0 to flash.display.DisplayObject.
at fl.motion::AnimatorBase/play()
at fl.motion::AnimatorBase$/processCurrentFrame()
at fl.motion::AnimatorBase/handleEnterFrame()
at flash.display::MovieClip/nextFrame()
at Lemonade_fla::MainTimeline/begin()[Lemonade_fla.MainTimeline::frame1:27]
TypeError: Error #1034: Type Coercion failed: cannot convert 0 to
FRAME 2 CODE
flash.display.DisplayObject.
stop();
var guide_tut:guide = new guide();
addChild(guide_tut);
//stand.addEventListener(MouseEvent.CLICK, check);
addEventListener(Event.ENTER_FRAME, check);
function check(ev : Event) : void {
cash.text.text = cash1;
lemons_count.text.text = lemons1;
cups_count.text.text = cups;
straws_count.text.text = straws;
ice_count.text.text = ice;
}
shop_mc.addEventListener(MouseEvent.CLICK, shopnow);
function shopnow(event:MouseEvent):void{
nextFrame();
}
1) I ran your code it works fine, the only place the error could be generated is:
flash.display.DisplayObject.
should be: flash.display.DisplayObject;
but you don't need it.
and
var guide_tut:guide = new guide();
addChild(guide_tut);
I ran it with just an empty mc, it works fine.
2) no reason to have (i know its commented out) but there never a reason to have
stand.addEventListener(MouseEvent.CLICK, check) ever since check is EnterFrame
basically you're error is within either display, most likely you're not extending the class from MovieClip
public class guide extends MovieClip
instead of movieclip it can be sprite or other displayobject...