Access the main timeline in a swc - actionscript-3

I have some animation that was created in the main timeline in an FLA
I want to publish this as a SWC for importing via Flash Builder.
However- I can't find a way to do this. Any ideas?
The only workaround I know of so far is to copy all the frames into a movieclip symbol and then set that to "export for actionscript" This isn't ideal since then I need to reposition the entire animation so it's at 0,0- and since there are objects moving outside the bounds of the stage, etc. I'll have to do it by eye. Not ideal :\ so a better solution is much appreciated!

You could embed a .swf
package {
import flash.display.MovieClip;
import flash.display.Sprite;
public class Demo extends Sprite{
[Embed (source="test.swf")]
private var Test : Class
public function Demo() {
var test : MovieClip = new Test() as MovieClip;
addChild(test);
}
}
}

Related

Flash reference error: "Cannot create property __id0_ on MyMovieClip"

I'm using Flash Builder as the main flash dev tool, and use Flash as assets creating tool.
Basically this works fine, but one day I made a 3D tween in Flash timeline, and then link the asset "MyMovieClip" with my actionscript code, like this:
package
{
import flash.display.MovieClip;
[Embed(source="somefile.swf", symbol="MyMovieClip")]
public class MyMovieClip extends MovieClip
{
public function MyMovieClip()
{
super();
}
}
}
Then this Error came up: "Error #1056: Cannot create property __id0_ on MyMovieClip".
I'm sure there's no "__id0_" in my fla file.
So I deleted the 3D tween animation layer and re-published the fla file, no Error encountered.
Any clues? Is that impossible to make a movieclip asset with 3D timeline animation?
Make your MyMovieClip class a dynamic class:
package
{
import flash.display.MovieClip;
[Embed(source="somefile.swf", symbol="MyMovieClip")]
public dynamic class MyMovieClip extends MovieClip
{
public function MyMovieClip()
{
super();
}
}
}
Otherwise, you may have a conflict with an instance on your timeline and property within the somefile.swf movie clip.

How to implement custom google search for ActionScript 3.0

I was trying to implement custom google search in my course-ware that developed in flash. I define a class named 'Main' (Main.as) and put my search code there. But the problem is, that Main class has a conflict with the other codes containing in my course-ware (i've combo box & other basic navigation in course-ware). i have no idea how to resolve it. is there any way to put that code in timeline layer? help please.. thanks. here is my Main class:
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.net.navigateToURL;
import flash.net.URLRequest;
public class Main extends Sprite
{
public function Main():void
{
searchButton.addEventListener(MouseEvent.MOUSE_UP, google);
addEventListener(KeyboardEvent.KEY_DOWN, google);
searchTerms.addEventListener(MouseEvent.MOUSE_DOWN, selectText);
}
private function google(e:*):void
{
if(e.type == "mouseUp")
{
navigateToURL(new URLRequest("http://www.google.com/search?q=" + searchTerms.text));
}
else if(e.keyCode == Keyboard.ENTER)
{
navigateToURL(new URLRequest("http://www.google.com/search?q=" + searchTerms.text));
}
}
private function selectText(e:MouseEvent):void
{
searchTerms.setSelection(0, searchTerms.length);
}
}
}
From what you shared & the messages here, I assume you are trying to add code through the flash IDE while at the same time having a document class called Main, for your application.
There are lots of ways you may get around this.
Assuming you want keep your timeline code unaltered while adding an instance of the Main class :
Add an empty movie clip to the library, say SearchClass.
Go to properties of the movie clip & click export for actionscript.
Set the class to Main. Do make sure where Main.as lies outside with respect to the fla.
Add this movieclip onto the stage, on any frame or layer.
Do remember to clear the document class field.
As a side note, You should also rename the class Main to something meaningful, like SearchClass.
If you wonder about setting the class vs base class,
We use base class only when you wish to extend the features of the class (by adding UI elements for eg).
You could also simply call the class directly from the timeline code as :
var main:Main = new Main();
addChild(main);
Just make sure the Main.as file lies next to the fla... ie make sure the path is available to compiler.

How to convert a two-file preloader and game swf with Document class into single swf?

I have a game that I've made that runs in two files, one the preloader and one the swf. When I use these two files together they work fine, however some flash portals require only one SWF, so I'm trying to find the shortest-path way to convert it to use only one.
The game swf uses a Document Class that includes the package, class constructor, and tons of functions, and runs fine. I have lots of movieclips on the stage that I refer to in my code. The code is something like this (abridged):
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
...
public class Game extends MovieClip {
var EnemyArray:Array;
var HeroArray:Array;
...
public function Game() { // class constructor
EnemyArray = new Array();
addEventListener(Event.ENTER_FRAME,onEnterFrame);
mainRestartButton.addEventListener(MouseEvent.CLICK, RestartLevel);
...
}
public function KeyPressed(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.SPACE)
{
attack();
...
}
} // End of class
} // End of package
I also have another swf, my preloader, which has the following code in frame 1:
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.ProgressEvent;
import flash.events.Event;
var myLoader:Loader = new Loader();
myLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onLoading);
var myURL:URLRequest = new URLRequest("Game.swf");
myLoader.load(myURL);
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
function onLoading(event:ProgressEvent):void {
var loaded:Number = event.bytesLoaded / event.bytesTotal;
percent_txt.text = "Loading: " + (loaded*100).toFixed(0) + "%";
}
function onComplete(event:Event):void {
myLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onLoading);
myLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
this.addChild(myLoader);
}
I've been through the gamut of solutions online and none of them seem to work with the way that my code is set up, such as: How to create Preloader in AS3
I tried his option 1 by taking all the code in my Document Class other than the package and constructor and pasted it into frame 2. However it returns "Error #1009: Cannot access a property or method of a null object reference." for any references to items on the stage, such as:
mainRestartButton.addEventListener(MouseEvent.CLICK, RestartLevel);
As he mentions, "This works well if you organized your project in a way that the most of the assets (images, etc) are in the Flash IDE Library and are not loaded on the first frame (you can check that in each library item's properties)." My project is NOT organized in such a way.
What is the easiest way for me to reorganize my FLA/code so they can have a preloader in a single swf?
Thanks so much for your time and help!
I use FlashDevelop (no FLAs) so I'm not sure if it'll work for you but I use the solution described here.

What I might be doing wrong in this code?

I am relatively new to flash. I am trying to create a square grid and add it to the movie. When I open the actionscript panel by pressing F9 and when I type the following code,
var square:SquareClip = new SquareClip();
addChild(square);
Things are working fine (the squareclip is appearing in the movie).
Instead when I do this however, I deleted the above code and just create a new instance of Main,
new Main
and inside Main.as
package{
//----
//IMPORT
//
import flash.display.*;
import flash.events.*;
import flash.text.*;
//Class creation
public class Main extends MovieClip {
//PROPERTIES
public function Main():void {
layout_in_grid();
}
private function layout_in_grid():void{
trace("layout_in_grid");
//create a new Square
var square:SquareClip = new SquareClip();
addChild(square);
trace("Square added");
}
}
}
And when I run the code, my square is not coming. I am doing something wrong very basically. Please help me.
You need to add Main to the displaylist:
var myMain : Main = new Main();
addChild(myMain);
You could also set Main as your document class.
#Mattias is correct. But you should set this as the Document Class as he suggested - When you've selected the stage, in the properties there will be an input box allowing you to enter the name of the class.
If your file is in the same location as the FLA and called 'Main.as' you enter in the box:
Main
If the file is within a folder structure e.g. com/company/projects/Main.as - enter:
com.company.projects.Main
--
Kudos on learning the OOP way!

Access main stage from class definition file (as3)

I'd like to access the stage of the main timeline from w/i a class that extends a movieclip. Basically, I have a button in the main timeline that makes a HUD appear. The HUD is an extended MovieClip class. When people click on a button in the HUD, I'd like to remove the object from the stage of the main MovieClip.
#curro: I think your confusion may come from the fact that I am running this code from a class definition file. Clicking on a button w/i this object should remove it from the DisplayList of the MainTimeline. Here's the code from the class definition file:
package classes {
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Answers extends MovieClip {
public function Answers(){
listen();
}//constructor
//initiatlize variables
public var answersArray:Array = new Array();
private function listen():void {
submit_btn.addEventListener(MouseEvent.CLICK, function(e:MouseEvent){
answersArray.push(answer_txt.text);
e.currentTarget.parent.parent.stage.removeChild(this);
});//listen
}//listen
}//class Definition
}//package
trace(e.currentTarget.parent.parent) gets me the MainTimeline, and trace(e.currentTarget.parent.parent.stage) appears to return the main stage, but I cannot use removeChild w/o getting an error that I am trying to coerce the stage to be a DisplayObject (which it ought to be).
What's on the stage of the MainTimeline: A single button that, when clicked, adds an instance of the Answers class to the stage.
What's part of the Answers class that's not in the code?
I first created Answers as a MovieClip object in the main library. It has 3 parts:
a TextField named "answer_txt"
a "clear_btn" that clears the answer_txt
a "submit_btn" that submits the text of answer_txt and then removes the entire Answers object from the MainTimeline (at least, that's what I want it to do).
your class definition is really weird. Looks like a mixture of as2 and as3.
Try with this:
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.text.*;
import fl.controls.Button;
public class Answers extends MovieClip
{
public var answersArray:Array = new Array();
public function Answers()
{
submit_btn.addEventListener(MouseEvent.CLICK, remove);
}
private function remove(e:MouseEvent)
{
answersArray.push(answer_txt.text);
this.parent.removeChild(this);
}
}
}
This works on my computer. Your code doesn't. I think it has something to do with the listen method. The class isn't still instatiated and you are making it work.
Hey, I can't make head or tail from the code. Where does submit_btn come from? Is it a property of the class? What about answer_txt?
You don't need to access e.currentTarget... to remove "this" simply:
this.parent.removeChild(this);
If you add that movieclip to the stage then you can access the stage from that class as simple as in the document class
stage
Otherwise you can't access stage from that class. But you can access it by sending the stage as argument when instantiate the class.