Dynamically Loading a Movie Clip AS3 - actionscript-3

I'm trying to add a movie clip to the stage dynamically. In the library of the main .fla I have a movie clip called "menuBar". I have a class called menuBar.as with the following code:
package
{
import flash.display.MovieClip;
public class menuBar extends MovieClip
{
public function menuBar()
{
var menuBar:MovieClip = new MovieClip ;
stage.addChild(menuBar);
menuBar.x = 319.9;
menuBar.y = 10.4;
}
}
}
The class path is set correctly, but I'm stuck at this point. I don't know how to get the movie clip onto the stage. Thanks in advance for any help.

In the library you have to go to the properties of your clip menuBar and export it for actionscript, just check the corresponding case, in class name field enter MenuBar for exemple. Check the export in frame 1 option too. After that, anywhere in your code you can create a MenuBar with this code:
var myMenuBar:MenuBar = new MenuBar();
addChild( myMenuBar );
if you enter as class name MyOtherItem you could create it with this code:
var myItem:MyOtherItem = new MyOtherItem();
addChild( myItem );
Hope this helps
PS: try using capital letter for first letter of class names and lowercase letter for first name of variables name it's more clear for everyone. (ex: MyClassName, myVarName)

You should do the other way. You are trying to make menuBar class to add itself to stage, but the stage is only known to Main, not menuBar. You should declare a variable in the Main class, on the timeline or elsewhere, and then you add that one to stage.
// timeline code. Put this in Main
var menubar:menuBar=new menuBar(); // declare
menubar.x=320;
menubar.y=10; // position
addChild(menubar); // add
The menuBar constructor should remain empty, as you are apparently designing your menu bar in the Flash CSx editor.
Also note that you might not need to add your menu bar to stage, you can easily just add it to the main class addChild(menubar) instead of stage.addChild(menubar).

Related

How do I access dynamic text on the stage from inside a movieclip which is inside another moveclip in AS3?

Before asking, I've read other articles for similar issues and have not been able to get it working. I have a dynamic text on the stage with the instance name of txtX. Then in action script in frame 1 of the main timeline I add an existing movie clip, mc1, using code:
var mc = new MC();
addChild(mc);
I then add another movieclip, mc2, with action script on frame 1 of the main timeline I add it as a child to mc1.
var mc2 = new MC2();
mc.addChild(mc2);
Now, in the class (created class for export AS) for mc2 I am trying to modify the text of the dynamic text but cannot for the life of me figure out how to reference it. I want to say:
stage.txtX.text = "blah blah";
or even
parent.parent.txtX.text = "blah blah";
but I usually get an error similar to:
Access of possibly undefined property txtX through a reference with static type flash.display:DisplayObjectContainer.
The error above is for the parent.parent.txtX.text line. Please tell me what I'm doing wrong. Thanks. Also I know that the variable names are nonsensical but it's just for my example. In my code the names make much more sense.
When you put a text field on your main timeline and give it an instance name of txtX, it actually is not a direct child of stage. The stage would be it's grandparent (the main/root timeline would be it's parent).
As such, if mc1 & mc2 are it's siblings (also on the main timeline), you could access your text field like so:
MovieClip(parent).txtX.text = "Hello";
Alternatively, you can access the root (main) timeline with the root keyword:
MovieClip(root).txtX.text = "Hello";
And that should work on any timeline no matter how deep/nested.
I figured it out finally. Not sure if it was the correct way to do it but in the parent MoveieClip, I created a TextField in code:
import flash.text.TextField;
import flash.text.TextFormat;
public var txtX:TextField;
txtX = new TextField(); txtX.x=0; txtX.y=0; addChild(txtX);
public function changeTxt( t )
{
tf = new TextFormat();
tf.size=4;
tf.color = 0xFFFF00;
txtX.text = t;
txtX.setTextFormat(tf);
}
Then in my MovieClip child class I called the parent's method like this:
MovieClip(parent).changeTxt( "Hello" );
A lot more work than I'd hoped for but it seems to work.

Can't cast embeded MovieClip to MovieClip type

I'm having trouble trying to embed a MovieClip in an ActionScript file I'm composing in FlashBuilder.
public class ItRock extends Item
{
public static const ID:String = "rock";
[Embed (source="/../art/menu/console.swf", symbol="itRock")]
private var IconClass:Class;
public function ItRock(game:Game)
{
super(ID, game);
var icon = new IconClass();
// var icon : MovieClip = new IconClass();
// var icon : MovieClip = new IconClass() as MovieClip;
addChild(icon);
}
}
My console.swf file contains a symbol called itRock which is of type MOvieClip and set to Export for ActionScript. In my code, I want to create an instance of this symbol and add it as a child of my Item class(which extends Sprite). However, when I create an instance of the embedded class, I create an object with the type name of console_swf$831ea9c30fe7882fadc388b74e115654-652499362. I can add it as a child fine, but if I try to cast it to a MovieClip implicitly I get an error that cannot be converted to a MovieClip. If I try to cast explicitly, I just get null.
Any idea what I'm doing wrong here?
Okay, it looks like Flash will automatically convert your MovieClips to Sprites when it can. So my above code will work if I change the MovieClip classes to Sprite.
The below website describes this. It suggested that adding an extra frame to your symbol will force Flash to treat is as a MovieClip and not a Sprite - however, I've created a simple two frame animation and its still exported as a Sprite. (It even plays the animation - I thought Sprites were supposed to be just static images with no animation?)
http://chrismweb.com/2011/03/20/problems-with-embedding-swfs-in-actionscript-or-flex/

Actionscript 3 game Character (Flashdevelop)

I am currently trying to make a game in Flashdevelop. The language I am using is Actionscript 3.
How can I implement my design for the character?
I tried to embed the image file, but flashdevelop gives me errors.
public class Player extends MovieClip
{
[Embed(source="../Images/Main Character.png")]
public var floor:int = 684;
This is the error: An Embed variable must not have an existing value.
Add next code after Embed tag:
public var MainCharacter:Class;
To add this image to the stage write:
var bmp:Bitmap = new MainCharacter() as Bitmap;
addChild(bmp);
There's really no need for MovieClip here, just extend Sprite if you need a container.
If you do not want to create another child like #subdan shows in his answer, you can tie the class directly to the embedded content, by placing the embed line right above the class line:
[Embed(source="../Images/Main Character.png")]
public class Player extends Bitmap

Making a simple tamagoci game getting no compiler errors but receiving no output

Kind of new Actionscript and I'm just trying to make a simple tamagoci game. I've wrote all the code out but and receiving no compiler errors but for some reason I'm also not receiving any output messages for my mouse event listeners. Here is all my code, I really can't find the problem and any help would be greatly appreciated. Thanks.
package{
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Main extends MovieClip{
public var feedButton:MovieClip;
public var tamagoci:MovieClip;
public var disButton:MovieClip;
public var dietButton:MovieClip;
public function Main() {
this.init();
}
private function init():void {
this.feedButton.addEventListener(MouseEvent.MOUSE_DOWN, onfeedMouseDownHandler);
this.disButton.addEventListener(MouseEvent.MOUSE_DOWN, ondisMouseDownHandler);
this.dietButton.addEventListener(MouseEvent.MOUSE_DOWN, ondietMouseDownHandler);
}
private function onfeedMouseDownHandler(event:MouseEvent)void{
this.tamagoci.scaleX += 0.1;
this.tamagoci.scaleY += 0.1;
}
private function ondisMouseDownHandler(event:MouseEvent)void{
this.tamagoci.gotoAndPlay(5);
}
private function ondietMouseDownHandler(event:MouseEvent)void{
this.tamagoci.scaleX -= 0.1;
this.tamagoci.scaleY -= 0.1;
}
Are you using Flash Professional?
You're declaring your variable types in your class here;
public var feedButton:MovieClip;
public var tamagoci:MovieClip;
public var disButton:MovieClip;
public var dietButton:MovieClip;
But then in your constructor, all you are doing is running init();
public function Main() {
this.init();
}
So, this could one of a few things. The most likely is that you have declared your variables, but you haven't initialised them. You've created the variables to hold your objects, but according to your code, they're empty. More specifically, a variable or class property that doesn't assign an object to a variable of an object type will contain a default value of null.
You could prove this in your code by simply putting a condition inside your init(); method;
if(tamagoci == null){
trace("I haven't been assigned an object of type class yet!")
}
So it could be 1 of these 3 things:
1: If you have written your own classes for these class properties/variables, then you need to instantiate them with the new keyword. The general syntax is;
variable_name = new ClassName(parameter_1, parameter_2);
If you are using classes you have written yourself, you have to create an instance of the object, assign it to a variable, and then add it to the stage using addChild();. For example, lets say you've written your own Tamagoci class;
tamagoci = new Tamagoci();
tamagoci.x = 100; // set the x location
tamagoci.y = 200; // set the y location
addChild(tamagoci);
Notice the use of Tamagoci. This is just an example, but this is the class name, which shouldn't be confused with variable/property name. It could just have easily been;
tamagoci = new MovieClip();
But then, this is just an empty MovieClip. It needs a property to display on the screen. A Shape, A Bitmap, or another container class object like MovieClip or Sprite (container classes allow you to nest display objects inside them). But on a basic level, it must contain a visual component to appear on the stage.
2:
Have you made Main your document class? This is the class which will get automatically called when your Flash movie plays. To set this, click on your stage, and in the properties dialogue box on the right, under PUBLISH, type in the name of your class, which is "Main".
3:
If you have created MovieClips in your library in Flash Professional, then you need to go to your library, right click the MovieClips, and select properties. From there, you need to make sure Export for Actionscript is ticked.
Now, if you click on your MovieClips on the stage, then open the Properties tab in the top right of Flash Professional's default layout, then right at the top should be a text field, and if you hover over it, Instance name will pop up as a tool tip. This is where you name your stage objects. Once that is done, you have access to them in your timeline.
If this is how you've done this, then you don't need to declare the variables in your main class, as they are already declared on your stage by Flash Professional and instantiated automatically.

How to run an animation from within another class in Actionscript 3.0

I'm new to ActionScript 3.0 and I am trying to work my way around Object Orientated Programming.
I've designed an application where there are 3 buttons on the stage and a logo. What I want to happen is when a button is clicked, the logo will fade out. Now I understand I need to manipulate the alpha properties of the logo but not sure how I would run the code once the button is clicked. I have my Main.as file loading the properties of the application. So once the FLA file runs, Main.as sets up the buttons and the logo. Now the buttons are put into a display holder and run from menuHolder.as which in turn gets the design of the buttons from button.as - so far with me?
To keep my code clean I have seperated my code into their own files (the design of the buttons into its own .as file, the arrangement of the menu buttons into its own .as file, the logo creation into its own .as file) so that the Main.as file just acts as a loader for placing everything into location on the stage.
So my question is that I want to create an animation for the logo (a fade out animation) where the code would be stored in say animationFade.as for example. Then when a button is clicked, the animation plays and the corresponding page is navigated to.
Here is my code for the setup of the menuBottons.as:
package
{
import flash.display.Sprite;
import flash.text.TextField;
import flash.events.MouseEvent;
import flash.text.TextFormat;
public class menuButtons extends Sprite
{
private var aboutMeBtn:button;
private var galleryBtn:button;
private var contactMeBtn:button;
public function menuButtons()
{
aboutMeBtn = new button();
aboutMeBtn.y = 0;
aboutMeBtn.label = "About Me";
addChild(aboutMeBtn);
aboutMeBtn.addEventListener(MouseEvent.CLICK, onButtonClick);
galleryBtn = new button();
galleryBtn.y = 75;
galleryBtn.label = "Gallery";
addChild(galleryBtn);
galleryBtn.addEventListener(MouseEvent.CLICK, onButtonClick);
contactMeBtn = new button();
contactMeBtn.y = 150;
contactMeBtn.label = "Contact Me";
addChild(contactMeBtn);
contactMeBtn.addEventListener(MouseEvent.CLICK, onButtonClick);
}
public function onButtonClick(event:MouseEvent):void
{
//code to initiate the animationFade.as file
}
}
}
it's this //code to initiate the animationFade.as file that i cannot grasp. It may be something very simple but my head seems to be spinning!
Hopefully I have provided enough information. Like I say I am just learning at the minute and trying to build up slowly.
Thank you
The best way to do this would be to dispatch an event from your button click function. Then, create a listener in your Main.as file that handles the event and tells the logo to fade out.
Maybe something like:
From menuButtons.as
public function onButtonClick(event:MouseEvent):void
{
dispatchEvent(new Event("hide_logo", true));
}
You can read up on the Event Class documentation to learn more about the parameters.
From Main.as (after you create your menuButtons instance)
menuButtonsInstance.addEventListener("hide_logo", hideLogoHandler);
function hideLogoHandler(e:Event):void {
// Fade out logo
TweenLite.to(logo, 1, {alpha:0}); // requires TweenLite
}
This example requires TweenLite from Greensock.
On a side note, for best practice your class names should start with an uppercase character. So "menuButtons" class would be "MenuButtons". Then, instance names would start with a lowercase character. Makes it easier to read if you do something like:
var menuButtons:MenuButtons = new MenuButtons();