Actionscript 3 game Character (Flashdevelop) - actionscript-3

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

Related

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/

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.

In Flash Pro, how to get the Properties panel to expose the properties you define on your ActionScript class?

I'm dabbling with game design and trying to create some characters for the game. Right now I've just created a single MovieClip that contains a rectangle. The MovieClip symbol extends a class that I've created in Flash Builder that implements the logic of a monster. I can then drag an instance of this monster symbol from the library to the stage and the code works when I run the simulation. So far, so good.
Now I want to create several monsters, all slightly different:
public class Monster extends MovieClip
{
public var isFriendly:Boolean = true;
public var strength:int = 10;
public var catchPhrase:String = "Booyah!";
public function Monster()
{
}
}
One way to do this is to write a new class for each monster that extends Monster and sets the properties I want in the constructor (I'd also have to create a unique symbol in the library for each of these variations too). However, this seems to be overkill if my monsters only differ by their property values.
Looking at the Flash Professional use interface, I see that at the very bottom of the Properties panel is a section that looks like a small table headed by 'Properties/Value'. Can I use this to somehow set the properties of my classes from within the Flash Professional UI? I can't find any info on how this is used.
Okay, I figured it out. The key is converting my symbol into a flash Component.
First I edited my ActionScript class to export the properties I wanted to set (including the Inspectable tag):
public class Monster
{
private var _catchPhrase:String;
public function Monster()
{
}
public function get catchPhrase():String
{
return _catchPhrase;
}
[Inspectable(name = "catchPhrase", type = String, defaultValue = "Booyah!")]
public function set catchPhrase(value:String):void
{
_catchPhrase = value;
}
}
Then I right clicked on the Monster symbol in my library and selected 'Component Definition...'. This brought up the Component Definition dialog. I then entered the name of my ActionScript class in the Class field and clicked the checkmark to validate it. Flash then automatically generated the properties I needed.
I also found this tutorial helpful:
http://redbjarne.wordpress.com/actionscript-3-0-custom-components-from-hell/

Dynamically Loading a Movie Clip AS3

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).

Access children of embedded aswf

I am embedding an swf file that has some children on its timeline. Like this:
[Embed(source="assets/skyscraper200x600.swf")]
private var Skyscraper :Class;
All children in the swf have an instance name, I double checked that when creating the swf in Flash CS5.
I am trying to access those children by name like this:
_bg = MovieClip(new Skyscraper());
_pig = MovieClip(_bg.getChildByName("chara_pig"));
_arrow = MovieClip(_bg.getChildByName("arrow_banner"));
However, both _pig and _arrow end up being null.
What's even stranger is that when I look at the Skyscraper object in the debugger, it shows a rather strange class name and a Loader as its only child (which in turn has no children). What's up with this?
.
I can access them like above if I do not embed the swf, but load it with a Loader. But I cannot do it in this case. I need to embed the swf.
So, how can you access children of embedded swfs?
I am not talking about accessing classes in the library of the embedded swf, but the instances on the timeline.
Here is a solution. You can also see the steps who helped me find this solution (describeType is your friend) :
public class Demo extends Sprite {
[Embed(source="test.swf")]
private var Test:Class
public function Demo() {
//first guess is that embed SWF is a MovieClip
var embedSWF:MovieClip = new Test() as MovieClip;
addChild(embedSWF);
//well, emebed SWF is more than just a MovieClip...
trace(describeType(embedSWF));//mx.core::MovieClipLoaderAsset
trace(embedSWF.numChildren);//1
trace(describeType(embedSWF.getChildAt(0)));//flash.display::Loader
var loader:Loader = embedSWF.getChildAt(0) as Loader;
//the content is not already loaded...
trace(loader.content);//null
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(){
var swf:MovieClip = loader.content as MovieClip;
var child:MovieClip = swf.getChildByName("$blob") as MovieClip;
//do nasty stuff with your MovieClip !
});
}
}
At the end of this tutorial http://jadendreamer.wordpress.com/2010/12/20/flash-as3-embedding-symbols-from-external-swf-game-tutorial there is an example of how it can be done
One solution is to embed the swf as an octet stream and reconstitute its bytes. However, I seem to remember reading somewhere that if you just set the mimeType to "application/x-shockwave-flash", you get a MovieClip that works as normal.