Flash CS4 AS3 Event not working - actionscript-3

I can't attach a simple event handler to a lousy movie clip. Not a single tutorial worked for me and I followed them carefully. Over an hour wasted for nothing... again! Here's what I did:
Layer1: created a symbol(movie clip). Added a rectangle. Draged it onto the layer. Added a name 'obj' in the Properties window. Exported it for ActionScript.
Layer2: Open Action Panel and wrote the following:
obj.addEventListener(MouseEvent.CLICK, move);
function move(event:MouseEvent):void {
obj.x = 200;
obj.y = 200;
}
I don't know what I might be overlooking. I tried with the import flash.events.Event; at the top. Although it wasn't present in the tutorials I've watched(on youtube).
P.S. Needless to say, I'm just starting with ActionScript 3.0 but I am reading about the basics on adobe.com

I think I got it. Adding a name for the movie clip symbol is one thing. But when we drag it onto the stage, then we have to click it. And THEN, in the properties tab of the object on the scene, we give it a (class) name. So that ActionScript can see and use it. Right-clicking on the movie clip in the Library tab and selecting Properties is NOT the properties we're looking for.
Edit: Thanks, akmozo. I just found that out. Took me long enough!
Edit2: Just wanted to point out something - when I save a project in Flash CS4 which has an event handler. The handler didn't work. The code itself, that is. I tried the same project in CS3 and it worked fine. So part of the problem was the program itself(Flash CS4).

Related

Adobe Animate Actionscript 3.0 "clicktogotonextscene" code not working

So I have the following code for a button called "n2" and as you can see it's supposed to take me to the next scene when I click it, but it isn't working. It is inside Scene 2 and I'm trying to get to the next one and whenever I test the scene and click it, I don't even get an error, it just doesn't work.
Btw, it's a GIF defined as a movie clip and then as a button. Could that be causing the problem?
n2.addEventListener(MouseEvent.CLICK, fl_ClickToGoToNextScene);
function fl_ClickToGoToNextScene(event:MouseEvent):void
{
MovieClip(this.root).nextScene();
}
If you put a trace in the function and comment out "MovieClip(this.root).nextScene();" you could then see if the function gets called on a button click.
I've not used animate before, but in flash you have to name your button/movieclip and then also name the instance. This allows you to create the object twice with two seperate names to be referred to by actionscript.
Not sure if this helps but thought I'd give you some things to try, good luck

Accessing a subelement in Flash

In Flash CS5 I have a button with an instance name "btn", which is made up of movie clips with instance names "mv1" and "mv2".
The question is: can I use gotoAndStop or something similar on the movie clips inside the button from the stage in which the button is instantiated. In pseudocode:
btn.mv1.gotoAndStop(3);
btn.mv2.gotoAndStop(7);
This is likely to be a very basic question, but the one I could not find any information on in the last half an hour.
Probably yes, if btn happens to be a MovieClip. For (almost?) any other display class (DisplayObject, DisplayObjectContainer, Sprite etc) this will fail in compilation.
If your compiler refuses to run your code, try this:
var mv1:MovieClip = btn.getChildByName("mv1") as MovieClip;
mv1.gotoAndStop(3);
Yes you can.
btn.mv1.gotoAndStop(3); will work perfectly fine.

flash as3: can children not run their own actionscript?

I thought I was being slick by having movieclips that I export for actionscript and then addChild later. I've made this one movieclip that loads html text through as, and it works fine when I drag it to the stage; but if I do
var trackListingBox:trackListingScreen = new trackListingScreen();
addChild(trackListingBox);
it either doesn't run the actionscript, or it's somehow broken. Can children not run their own action script?
Maybe try adding some code to your MovieClip which will fire when the movie clip is added to the stage. Something like this:
this.addEventListener(Event.ADDED_TO_STAGE,onAddedToStage);
function onAddedToStage(e:Event):void {
functionWhichLoadsHTML();
}
They can "run their own actionscript" fine. There's probably a bug in your code in the child clip, but I can't give any advice on it without actually seeing the code.
the problem was that the actionscript was loading before the items it was referring to, thereby giving me errors that items were not found.

AS3 preloader sorrows, unable to load symbols from library

I created an AS3 preloader, and placed the code for that on frame one.
I then made a symbol, and placed it in the library. It was set to NOT export on frame 1, and the fla's settings had all classes exported on frame two. There were no references to the object until frame two.
Then, flash crashed whenever I compiled without the "Export in frame one" box checked.
To fix this, a friend suggested I start my game logic on frame 3, so it will have properly loaded frame 2. That seemed to work fine, the class was instantiating properly.
Then, it turned out that it was not loading the movieclip, only instantiating the class. Again, this could be fixed by exporting in frame 1, but I really cannot afford to do that.
The same friend suggested I place an instance of the symbol on the stage on frame 3, and perform game logic on frame 4. They said this would initialize the movieclip properly.
However, this was not the case. How can I load the entire symbol, graphics and all, without exporting to frame 1? This single symbol will contain probably 10-20 MB of graphics, so it needs to be preloaded.
Thanks for the help!
EDIT: To make a long story short, all I need is some way to load a movieclip so it can be used and visible and everything.
EDIT: Is there any way to force-load a movieclip via AS3?
Hard to figure out from descriptions.
If you make a new .fla file, paste your large(10-20MB) clip on frame 2,
set your export frame as 2, then try to preload from frame 1 and access the large clip's content in frame 2, do you get the same error ?
say you have this in frame 1:
stop();
this.loaderInfo.addEventListener(Event.COMPLETE, onComplete);
function onComplete(event:Event):void{
gotoAndStop(2);
}
and in frame 2:
trace(myLargeClip);//where myLargeClip would be your 10-20MB clip
It should be ok, otherwise, in case tracing your large clip returns null, you might want to try to invalidate the stage:
on frame 2:
stage.addEventListener(Event.RENDER,onRender);
stage.invalidate();
function onRender(event:Event):void{
trace(myLargeClip);
}
Basically what I'm suggesting is:
Isolate the problem. See if your large clip is causing problems in a similar, but simplified scenario and why, then once you got a fix use it in your main fla.
Try the stage invalidation, although, since I don't fully understand your setup, it's just a wild guess.
HTH,
George

How is the Actionscript 3.0 sample Maze game's code included?

I can't find the line in the Adobe samples for a maze game where the actual .as code is included into the project. This is fundamental, since my own implementation is just using include "file name";, which isn't working. I also can't use import, since I then can't access the elements placed on the stage. Help, please? The files are available here http://download.macromedia.com/pub/developer/flash_as3_sample_game_1.zip
I tried to debug the code, but I can't even find the point where the constructor is called. The execution starts at "stop()" in frame 1 and then if you step into it, calls the enter frame event handler.
I guess Document Class is the thing you are looking for.
It is an entry point, the class which is instantiated once Flash Player loaded your SWF.