convert action script 2 to action script 3 - actionscript-3

OK I read the forums and seems like anyone asking about converting AS2 to AS3 gets flamed with posts like "why would you use AS2, just rewrite it in AS3 from scratch". So please do not answer like this, I just need help figuring out this particular snippet of the code. This is AS2, I have no clue what it is trying to do.
if (_root.components == undefined) {
_root.components = "";
}
_root.components = _root.components + _root.getInstanceAtDepth(this.getDepth()) + " ";
I googled the individual methods 'getInstanceAtDepth' and 'getDepth', and understand they deal with the layering of the movie. Trouble is '_root.components'. If this is referring to a movieclip as 'root', then there is no method or property named 'components'. You can put components on the stage and make them a part of the movieclip fla file but again this does not seem to explain what the code is doing. Does anyone have any idea what _root could be referring to here, what object has a property 'components'?
Thanks,

I think that you want to understand that AS2 code not to convert it.
That code is apparently used inside a component(Button, MovieClip)'s code and it's just trying to concatenate the name of that component to a global string (_root.components).
For example, if I've a button called button and a MovieClip called movieclip, when I execute that code using their onPress() functions, I'll get something like this :
_level0.button _level0.movieclip
Hope that can help.

Related

How to change bitmapdata in AS3

I'm building a spritesheet class. Hopefully this will be low hanging fruit to someone, but I'm stumped.
I have a spritesheet (.png) that I've loaded at runtime and placed a section of it on the stage using this code from within the Spritesheet class .as file ouside of the constructor method:
private function onLoad(e:Event):void
{
var loaderBmp:Bitmap = Bitmap(_loader.content);
_bmpData.copyPixels(loaderBmp.bitmapData, new Rectangle(0,0,80,80),new Point(0,0));
}
That works fine. I get my slice of the .png file displaying nicely. In my case, the spritesheet is meant for animating a character, so I need to update the BitmapData and I'm not having any luck. Here is what I'm trying this within my Main class in a function I use to alter the frame of the animation depending on the state of the character:
c._thisSpriteSheet._loader.content.bitmapData.copyPixels(loaderBmp.bitmapData, new Rectangle(0,20,50,30),new Point(0,0));
loaderBmp is a variable who's value is var loaderBmp: Bitmap = Bitmap(_spriteSheet._loader.content);
c is a reference to the Runner object that is the character.
_spriteSheet is a property of the Runner class of type Spritesheet.
_loader is a property of the c._spriteSheet and is the Loader object used when the spritesheet was instantiated.
It doesn't throw an error, but it also doesn't replace the original bitmapData object with the new one. I thought maybe this meant that I need to create a new BitmapData object and use that in the copyPixels method, but that returned the same results (nothing). When I step through the code in debug mode, everything looks like it is working, but my display object does not update with the new bitmapData. What am I tripping on?
Thanks for looking!
Well, probably no one will read this since I'm answering it so quickly, but I literally spent 3 days trying to figure this out. Sometimes trying to ask the question in a concise way helps one answer their own question, and moments later, voila!
So in case anyone has a similar issue, what I was doing wrong was that I was trying to access the BitmapData object via the Loader that originally loaded it. Then it dawned on me that I could simply reference the BitmapData directly via that property of the SpriteSheet class I had made. I think this will be pretty confusing for someone else to follow. If a moderator sees this and thinks it's junk, I don't mind it getting erased, but thought I'd keep it up anyway. The new code looked like this:
c._thisSpriteSheet._bmpDSheet.copyPixels(loaderBmp.bitmapData, new Rectangle(0,20,50,30),new Point(0,0));
and _bmpDSheet is the bitmapdata property of the class.

How can I transfer code inside of a symbol in Flash to my document class?

I am pretty new to coding in general so I'll admit my understanding of inheritance is poor. I have this code that works when it's embedded in a symbol in my Fla doc, but I can't figure out how to transfer it to the Main document.
stop();
stage.focus = input_txt;
var outputText:String;
input_txt.visible = true;
input_txt.addEventListener(KeyboardEvent.KEY_DOWN, pressEnter);
function pressEnter(e:KeyboardEvent):void {
if(e.charCode == 13) {
captureText();
this.nextFrame();
}
}
function captureText():void {
outputText=input_txt.text;
}
All that code occurred on frame2 of the office_mc symbol, and then frame 3 has this:
output_txt.text = outputText;
I'm aware that I needed to reference the txt objects as office_mc.input_txt inside the main document since they are embedded. I'm also aware that outputText would need to be a global variable. But no matter how I tried to move things around I kept having an error due to something being null. For a little background information, I'm just trying to have user input displayed in a different area within this symbol.
Could someone explain or give an example of how to execute this code in a document class? Also I'm just totally confused about how to communicate between classes in general so if anyone could point me towards tutorials or example code that could help with that understanding, it'd be greatly appreciated. :)
Don't code in timeline at all (this is a good practice).
You should use only classes which eventually points to objects from the timeline.
Here you have some good tutorials:
How to Use a Document Class
AS3 OOP (good tutorial)

Actionscript 3 problems with "Call to a possibly undefined method"

Thanks partly to info from people here, I'm getting more comfortable with Actionscript 3, but I've got a problem that is very puzzling.
The program (done entirely in AS3, no Flash) has several different screens. One does music and it's working very well. Another does video. Obviously when somebody goes from music to video we need to make sure the music is turned off. There is a Main screen that handles going from one to the other.
I started out doing it this way, and the reason I got "Call to a possibly undefined method" is obvious. The following is in the Main class, with the "private var" part inside the class but external to the functions and the "music = new MusicPanel" part in one of the functions:
private var music:MusicPanel;
music = new MusicPanel(trackNames.songNames, trackNames.numSongs);
When switching to the video panel, I added a public function in MusicPanel called StopMusic and called it when the user went to the video panel:
if(music != null)
music.StopMusic();
That got the error:
Call to a possibly undefined method StopMusic
I was checking to make sure music was not null, but that error didn't seem like a bad thing. So I changed the code to:
private var music:MusicPanel = new MusicPanel();
and added a function that would get the song names and number of songs to the music class. That did not help- I got the same error, and in fact the function that tried to put the song names and number of songs got the error also.
At the same time, the Video panel does not give me that error, even though I have laid it out in exactly the same way.
private var video:VideoPanel = new VideoPanel;
video.PlayVideo();
I do a fair amount of setup on the music screen when it gets called as a new class, I do less setup for the video screen. I'm not sure if that makes a difference.
Clearly there is something I don't understand here. Anybody got any idea what's going on? I've looked at a number of questions about this, but have not found an answer. I think I'm doing this right, but the compiler thinks I'm doing it wrong, so I must be doing it wrong.
Later Note: One answer mentioned the difference between Sprite and MovieClip. I gave that a try, changing to MovieClip does not help, and the VideoPanel, which works, extends Sprite.
What is the base class of MusicPanel, Sprite or MovieClip? If Sprite, change to MovieClip and see what that does. In AS3 Sprites are not dynamic, so can't take properties that are not inherent in the class.
It appears that the problem is coming either from Actionscript 3 or from FlashDevelop. I built a new module (SongsPanel) which is very close to the same as the original MusicPanel. It works. If I add a public function in MusicPanel the compiler comes back with the same error. If I add a public function in SongsPanel everything works.
Does FlashDevelop keep track of errors in some hidden file? I'm guessing that's what's going on, and that there is a bug in the way that is done.
PITA!- but at least it now works.

Using movie clips/symbols in AS3 - access of undefined property

I'm following (or rather not following!) the tutorial found here: http://www.flashuser.net/flash-tricks/tips-tricks-10-using-drag-drop-in-actionscript-3-0.html
I've drawn a shape (its a rectangle). I've then right clicked and selected "Convert to symbol". Its of the Movie Clip type and its name is item1. (I haven't selected "export for actionscript").
My actionscript on the first frame looks like this... simply:
item1.initX = 0;
This gives the error access of undefined property item1.
I have no idea how to remedy this. Downloading the source from the link appears to be exactly the same as my attempt, yet theirs works.
Any idea why this doesn't work. No other tutorials or help anywhere seems to show my issue. I've tried everything I can think of including checking the "export for actionscript" and all sorts. No luck.
What am I doing wrong? I bet its really really simple! :p
Thanks
You have to instantiate the symbol first.
Select your symbol if it's on the stage, go to the Properties panel, and give it an instance
name : item1 . Here is what the part you've missed in the tutorial : http://screencast.com/t/kk4ZlVl5
If it's not on the stage, you must select "export for actionscript" and instantiate it in your code:
mc = new item1();

is there an actionscript function or method to return an elements instance name?

I'm getting into actionscript3 and was wondering if there was a way to make an 'onclick' type function that returns an id or an instance name.
For example in jQuery you can do the following, which is great for then passing the id into an array or whatever you choose.
$('.menuButton').click(function(){
var collectedID = $(this).attr('id');
Is there an equivalent to this in AS3? I'm assuming grabbing the instance name would be the goal? I haven't run across ids in Flash yet. Thanks a lot everybody.
Flash doesn't have elements or ids. An instance name can be retrieved simply by callong foo.name.
Possibly the closest thing to what you want to do is add a listener for MouseEvent.CLICK to the main time line and thentrace event.target.name. play around with it and see what you find.
The precise style of coding you see in jquery doesn't really apply in AS3. It might have worked better in as2, as as2 was much closer to JS than as3.
In AS3 you hardly ever use the instance name of a DisplayObject. You would most likely pass the reference to the object.
private function clickHandler(e:MouseEvent):void
{
myArray.push(e.target);
}
addEventListener(MouseEvent.CLICK, clickHandler);