embedding swf files in flashdevelop and accessing the symbols/buttons - actionscript-3

in trying to access a button symbol in an swf file which i embedded into the mianmenu class called PlyBtn but i always get errors and im not sure if im enbedding it right or using the right code to find it within the swf file,
//heres my mainmenu class
package
{
import flash.display.Bitmap;
import flash.display.MovieClip;
import flash.display.Sprite;
/**
* ...
* #author Andrew Dean
*/
public class MainMenu extends MovieClip
{
[Embed(source = "GameMenu.swf")]
public var MainM:Class;
public function MainMenu()
{
var MMenu:MovieClip = new MainM() as MovieClip;
addChild(MMenu);
}
}
}
main class
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
/**
* ...
* #author Andrew Dean
*/
public class Main extends Sprite
{
public var MMenu:MainMenu
public function Main()
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
MMenu = new MainMenu;
stage.addChild(MMenu);
addEventListener(MouseEvent.CLICK, startgame);
}
public function startgame(e:Event):void
{
if (e.target == MMenu.PlyBtn)//problem
{
}
}
}
}

Export you swf as a swc and then add it to your flash develop project as a library.
Not only will you have a better workflow, you will also get intellisense code completion for your included art classes.
In flash, just make sure that the symbols you want to use have a class name and set for export in frame 1.

Related

How to pass variables from .fla file to .as file in as3

I have a .fla file names test.fla and I have this variable in it:
import Main;
var my_var;
stage.addEventListener(MouseEvent.CLICK, onLoaded);
function onLoaded(e:Event):void
{
my_var = "Maziar";
//trace(my_var);
}
I have a .as file called Main.as.
I want to pass my_var from test.fla to the Main.as.
I will really appreciate, if you can help me in this matter!
It is noticeable that I have used the method mentioned in "Actionscript 3 : pass a variable from the main fla to external as file", but it does not work for me!!!
I wrote in my Main.as:
package
{
import flash.display.Sprite;
import flash.geom.Point;
import flash.events.MouseEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.Event;
public class Main extends Sprite
{
public function Main()
{
if (stage)
{
init();
}
else
{
addEventListener(Event.ADDED_TO_STAGE, init);
}
addEventListener(Event.ENTER_FRAME, waitForMyVar);
}
private function waitForMyVar(e:Event):void
{
if (my_var != null)
{
trace(my_var);
removeEventListener(Event.ENTER_FRAME, waitForMyVar);
}
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
}
...
}
}
Thanks in advance!
It's important to note that the constructor Main in your ActionScript document file is run before the code found within the frame. When you are attempting to access the my_var variable in your AS document it has not yet been declared in the frame.
So, we need to wait for Flash to run the frame. This can be done using an Event.ENTER_FRAME listener.
Example:
Timeline Code (.fla file)
var my_var:String = "my variable";
Document Code (.as file)
package {
import flash.display.MovieClip;
import flash.events.Event;
public class Main extends MovieClip {
public function Main() {
addEventListener(Event.ENTER_FRAME, waitForMyVar);
}
private function waitForMyVar(e:Event):void {
trace(my_var);
removeEventListener(Event.ENTER_FRAME, waitForMyVar);
}
}
As a side note, it appears my_var is not assigned a value until the user has clicked the stage. An adjustment could be made in the waitForMyVar function to wait for my_var to be non-null.
Example:
if(my_var != null) {
trace(my_var);
removeEventListener(Event.ENTER_FRAME, waitForMyVar);
}
Hope this helps!
Use static class members.
public class Main extends Sprite
{
static public var globalVar:* = 1;
public function doWhatever():void
{
trace(globalVar);
}
}
Then in FLA:
import Main;
var M:Main = new Main();
// or use sprite instance of Main
M.doWhatever();
Main.globalVar = "Hello World!";
M.doWhatever();

AS3 debugger stops responding while trying to load image into sprite using Loader

I'm trying to create a simple Menu in AS3. There is a sprite called startButton, which when pressed will call the function startGame, and that's it! But, not so easy. I'm using flashdevelop IDE so I'm trying to call a loader to get a .png image file for the spite startButton. But, it doesn't work. There are no error messages, the debugger just does not respond. Any help? Here is the code for both files
Main code:
package {
//Other Files
import Menu;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.ui.Mouse;
public class Main extends Sprite {
//Game values
public static var gameWidth:int = 750;
public static var gameHeight:int = 750;
public function Main() {
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
addChild(Menu.startButton);
Menu.startButton.addEventListener(MouseEvent.CLICK, startGame);
stage.addEventListener(Event.ENTER_FRAME, update);
}
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
}
//Function starts game
public function startGame(evt:MouseEvent):void {
removeChild(Menu.startButton);
}
//Updates every 60 seconds
public function update():void {
trace("Updated");
}
}
}
And Menu Image code:
package {
//Other files
import Main;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
public class Menu extends Sprite {
public static function imageLoaded():void {
startButton.addChild(loader);
//initizlize values for startButton Bitmap
startButton.x = (Main.gameWidth / 2) - (startButton.width / 2);
startButton.y = (Main.gameHeight / 2) - (startButton.height / 2);
}
//create startButton Bitmap
public static var startButton:Sprite = new Sprite();
public static var loader:Loader = new Loader();
loader.load(new URLRequest("lib/menustartbutton.png"));
loader.addEventListener(Event.COMPLETE, imageLoaded);
}
}
By the way, I wait for the loader to successfully load the image before working with it, just in case the image takes more time and it draws errors.
The problem is that you misuse static. all static methods/properties are initialized before the classes themselves. As a result static can receive values but they cannot run any code. Running code has to happen after all classes are ready to go which is not the case when static is initialized. In your case startButton and loader are created correctly but the next line never runs 'loader.load'.
Don't misuse static, you are obviously trying to use static to make you code writing and life easier but at the end because you are misusing it you will always end up with more problems.

AS3 Run gotoAndStop from a class

I have the problem with Actionscript 3.0 in Adobe Flash. I can't run "gotoAndStop" from a class (not document class).
With the help of the Internet I tried several things, but none of them worked:
1)
MovieClip(root).gotoAndStop(3);
2)
package
{
import flash.display.MovieClip;
public class CustomClassName extends MovieClip
{
public static var mainTimeline:MovieClip;
public function CustomClassName()
{
// constructor code
}
}
}
3)
public class np extends SimpleButton {
var _root:MovieClip;
public function np() {
this.addEventListener(Event.ADDED_TO_STAGE,init);
this.addEventListener(MouseEvent.CLICK,nextF);
}
private function init(e:Event):void{
_root = MovieClip(this.root);
}
private function nextF(e:MouseEvent):void{
_root.addEventListener(Event.RENDER,renderF);
stage.invalidate();
_root.nextScene();
}
private function renderF(e:Event):void {
_root.gotoAndStop(5);
}
}
I have these imports:
import flash.display.MovieClip;
import flash.display.Graphics;
import flash.display.Stage;
import flash.events.Event;
And if I run these lines of code:
trace('frame:',currentFrame);
super(this).gotoAndPlay(2);
trace('frame:',currentFrame);
... I get 0 as currentFrame as a result.
I have a class where I want to run gotoAndStop(2).
And in my .fla file I have these in the first frame:
stop();
import Buzzer.*;
var buzzerClip:Buzzer = new Buzzer();
stage.addChild(buzzerClip);
But the code doesn't run the gotoAndStop function. And actually no error will be returned. Does someone has another idea?
The property root is null until the display object has been added to the display list.
So to adjust your first attempt:
public function MyDisplayObject()
{
init();
}
private function init():void
{
addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
}
private function addedToStageHandler()
{
MovieClip(root).gotoAndStop(3);
}

AS3 Architecture : How to tween a single instance of an object on multiple calls?

I am new to actionscript and am having a trouble displaying a single instance of an object, using FlashDevelop.
I have a main.as in which I am displaying an image as background. Then I display a rectangle containing some text that tweens as the mouse hovers a target (appearing/disappearing on the stage). The rectangle is in a class TextBox.as .
I know my code is quite messy because it creates a new instance of the rectangle everytime I reach the target (calling the tween). But if I try to switch it around it gives me errors. Also I cannot seem to remove my rectangle (with removeChild()) once it is created, it cannot find the child.
Could anyone indicate me what is the architecture I should use so that only one instance of the rectangle is created?
Here's a bit of my code:
//IMPORT LIBRARIES
import Classes.TextBox;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import com.greensock.TweenLite;
// Setup SWF Render Settings
[SWF(width = "620", height = "650")]
public class Main extends Sprite
{
//DEFINING VARIABLES
[Embed(source="../lib/myimage.jpg")]
private var picture:Class;
private var myTween:TweenLite;
//CONSTRUCTOR
public function Main():void
{
addChild(new TextBox);
addChild(new picture);
addEventListener(MouseEvent.MOUSE_OVER, appear);
}
//ROLLDOWN FUNCTION
public function appear(e:MouseEvent):void
{
trace("Appear");
var text:TextBox = new TextBox();
addChild(text);
addChild(new picture);
if (picture) {
removeEventListener(MouseEvent.MOUSE_OVER, appear);
//addEventListener(Event.COMPLETE, appearComplete);
myTween = new TweenLite(text, 1, { y:340 , onComplete:appearComplete, onReverseComplete:disappearComplete} );
}
}
Thanks in advance.
i dont know what tweening you want to achieve but you should reuse your textbox instance, e.g.:
import Classes.TextBox;
import com.greensock.TweenLite;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
[SWF(width = "620", height = "650")]
public class Main extends Sprite {
[Embed(source="../lib/myimage.jpg")]
private var pictureClass:Class;
private var picture:Bitmap;
private var textbox:TextBox;
public function Main():void {
if (stage)
init();
else
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
picture = new pictureClass();
textbox = new TextBox();
addChild(picture);
addChild(textbox);
addEventListener(MouseEvent.MOUSE_OVER, tween);
}
public function tween(e:MouseEvent):void {
removeEventListener(MouseEvent.MOUSE_OVER, tween);
TweenLite.to(textbox, 1, { y:340, onComplete:reverse } );
}
private function reverse():void {
TweenLite.to(textbox, 1, { y:0, onComplete:tweenComplete } );
}
private function tweenComplete():void {
addEventListener(MouseEvent.MOUSE_OVER, tween);
}
}

Function in Sprite does not run to display

Function X1 simply does not run- no trace result and programme so basic.
Board.as is called- I checked.
Simple sprite display not working.
Main.as
package
{
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.*;
import flash.text.TextField;
import flash.ui.Keyboard;
import Start;
import Board;
/**
* ...
* #author Michael
*/
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init():void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
var Board1:Sprite = new Board();
stage.addChild(Board1);
Board1.visible = true;
var Start1:Sprite = new Start();
Start1.x = 32;
Start1.y = 32;
addChild (Start1);
stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);
function myKeyDown(e:KeyboardEvent):void{
if (e.keyCode ==Keyboard.SPACE)
{
removeChild(Start1);
Start1 = null;
}
}
}
Board.as
package
{
import flash.display.Bitmap;
import flash.display.Graphics;
import flash.display.Sprite;
/**
* ...
* #author Michael
*/
public class Board extends Sprite
{
[Embed(source="../lib/Board.jpg")]
private var BoardClass :Class
public function X1():void
{
var boardclass:Bitmap = new BoardClass () as Bitmap;
trace("Project is running fine!");
this.addChild(boardclass);
}
}
}
You are not calling the function X1(). Change the code part in your Main.as class where you create the Board to this:
var Board1:Sprite = new Board();
Board1.X1();
stage.addChild(Board1);
A few more tips for your code:
1) You don't need Board1.visible = true;, it's visible by default
2) Change the name of Board1 to board1 or just board. It's a standard to call classes with first capital letter.
EDIT:
If you want X1() to be run as you create the object, call this function in the constructor of Board.as. Constructor is a function that is run when you create an object. For Board.as it would be like this:
public function Board():void
{
X1(); // this function will be called when you create a new Board object
}