Add MovieClip with Class to Stage - actionscript-3

I'm new to AS3 and seem to be overlooking something related to classes and/or targeting. I have a MovieClip in my Library that is linked to a class called 'cloud' (yes cloud is just a picture of a rain cloud). I want to add cloud to the stage. I understand there are two ways to do this:
Drag it directly across from the Library to the Stage.
Use AS3 to addChild to the Stage.
The class works when I publish my swf using the "drag it directly across from the Library to the Stage" approach. But it doesn't work when I try using the AS3 approach.
The class is as follows:
package {
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
public class cloud extends MovieClip {
public function cloud() {
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyMapping);
}
private function keyMapping(event:KeyboardEvent):void {
if (event.charCode == 13) {
MovieClip(parent).testme.text = "hello world!";
}
}
}
}
The AS3 I have been using on Frame 1 of my main timeline:
stop();
var cloud_mc:cloud = new cloud();
addChild(cloud_mc);
==================================================================================
SUGGESTED FIX
Class:
package {
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
public class cloud extends MovieClip {
public function cloud() {
}
public function tralala() {
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyMapping);
}
private function keyMapping(event:KeyboardEvent):void {
if (event.charCode == 13) {
MovieClip(parent).testme.text = "hello world!";
}
}
}
}
AS3:
stop();
var cloud_mc:cloud = new cloud();
addChild(cloud_mc);
cloud_mc.tralala();

Use the linkage name of the library object, not the class that it extends.
Your library object extends the cloud class which extends movie clip. If you instanciate the cloud class you miss the library object.
The process is described in detail here:
http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b8ea63-7fee.html

Related

Main class was not the first class which was called in ActionScript 3.0

I have a weird problem in a game which I want to create. At first I have created a project without external classes.
On the root I have three Characters and one Level. Also there is a script for the key listeners and I have eventListeners to register the level, levelElements, coins and the characters. Then I have a CharacterControl MovieClip in the library. This MovieClip contains the character behaviour. As example walk, jump, idle, gravity if not colliding to the ground. There are also different events and eventListeners.
The scripts are on the timeline. If I call in both timelines a trace-function, the root was called before the CharacterController.
After that in my next exercise I created a document class Main. Now there are all root scripts. And for the CharacterController I also copied the timeline code and put it into an external class.
Now my problem is that the CharacterController class is called before the main class gets called. This leads to the problem that the eventListener and events can't get called in right order. There are happening a few errors. No Coin and no Character collides on the ground or a plattform. Everything is falling down.
How can I achieve that the Main gets called at first? Should I remove the characters and create them by script?
EDIT:
Ok, I give a short example which shows the basic problem without the complex code of my game.
package {
import flash.display.MovieClip;
public class Main extends MovieClip {
public function Main() {
trace("main was called");
}
}
}
package {
import flash.display.MovieClip;
public class My_Circle extends MovieClip {
public function My_Circle() {
// constructor code
trace("circle was called");
}
}
}
Here are some pictures of the configuration and structure of my project:
I need Main called as first. I think it's a basic problem in as3.
You'd make the class file of your stage Main.as in the properties pane.
Edit: Interesting. Just replicated this. I believe then that flash/air constructs elements so they're ready to be put on the stage instead of constructing the stage first and elements after. You should put the code you want to execute for your circle in some sort of init function and execute it in.
package
{
import flash.display.MovieClip;
public class Main extends MovieClip
{
public function Main()
{
super();
trace("Hello");
(circle_mc as Circle).init();
}
}
}
Circle:
package
{
import flash.display.MovieClip;
public class Circle extends MovieClip
{
public function Circle()
{
super();
}
public function init():void
{
trace("World");
}
}
}
ok, I figured out a simple solution how you can make it by code. At first I think it's a better solution to create the object (circle, character, whatever) by code.
The timeline code:
import flash.events.Event;
Main.setStageRef(this.stage); //Super-important
stop();
The Main class code:
package {
import flash.display.MovieClip;
import flash.display.Stage;
import flash.display.DisplayObject;
import flash.events.Event;
public class Main extends MovieClip {
public static var stageRef : Object = null;
var movieClipExample : My_Circle;
public function Main() {
this.addEventListener(Event.ENTER_FRAME,this.afterMainTimeLine);
stage.addEventListener("myEvent", myEventFunction);
}
public function afterMainTimeLine(e:Event) {
trace("Hello");
movieClipExample = new My_Circle;
movieClipExample.init();
stageRef.addChild(movieClipExample);
removeEventListener(Event.ENTER_FRAME, this.afterMainTimeLine);
this.addEventListener(Event.ENTER_FRAME,this.updateFunction);
}
public function updateFunction(e:Event){
movieClipExample.moveFunction();
}
public function myEventFunction(_event: Event) {
trace("myEvent was triggered");
}
//Getter/setter for stage
public static function setStageRef(_stage : Object) : void
{
stageRef = _stage;
}
public static function getStageRef() : Object
{
return stageRef;
}
}
}
The Object code (as example My_Circle):
package {
import flash.display.MovieClip;
import flash.display.Stage;
import flash.display.DisplayObject;
public class My_Circle extends MovieClip {
public function My_Circle()
{
stageRef = Main.getStageRef();
trace("World");
this.x = x;
this.y = y;
var evt = new Event("myEvent", true);
stageRef.dispatchEvent(evt);
}
public function init():void
{
trace("Created Circle");
this.x = 300;
this.y = 100;
}
function moveFunction(){
this.y += 1;
}
}
}
The output is following:
Hello
World
myEvent was triggered
Created Circle
Maybe this could be helpful for others.
Just one thing. For different objects from the same class it's better to use an array. The position (maybe) should be random.

How to call an MovieClip on stage AS3

Trying to addChild() inside a movie clip in the stage from one of my classes. The code looks like this
package {
import flash.display.SimpleButton;
import flash.events.MouseEvent;
import flash.display.MovieClip;
public class createFlask extends SimpleButton {
public function createFlask() {
addEventListener(MouseEvent.CLICK, createAFlask);
}
private function createAFlask(e:MouseEvent):void
{
var Flask = new flask ;
Flask.x = stage.width/2;
Flask.y = stage.height/2;
stage.experiment.addChild(Flask);
}
}
This gives an error as
Access of possibly undefined property experiment through a reference
with static type flash.display:Stage.
Any solutions?
Just omit "stage".
Instead use
experiment.addChild(Flask);
That will work.

Send data from Flash to Starling class

I want to send data from mainClass (Flash class) to my Starling class. Here is the code of two classes. I need to pass data between them.
package
{
import flash.display.Sprite;
import flash.events.Event;
public class mainClass extends Sprite
{
private var myStarling:Starling;
public function mainClass ()
{
super();
stage.addEventListener(Event.RESIZE,onResize);
}
private function shangedOr(e:StageOrientationEvent):void
{
// code
}
private function onResize(e:Event):void
{
myStarling = new Starling(Main,stage);
myStarling.start();
}
}
}
Main.as class of starling :
package
{
import starling.core.Starling;
import starling.display.Sprite;
public class Main extends starling.display.Sprite
{
private var theme:AeonDesktopTheme;
public function Main()
{
super();
this.addEventListener(starling.events.Event.ADDED_TO_STAGE,addToStage);
}
private function addToStage(e:starling.events.Event):void
{
this.theme = new AeonDesktopTheme( this.stage );
drawComponent();
}
private function drawComponent():void
{
//code
}
}
}
Is there a way to pass data between Flash and Starling? I created an Event class to listen from Flash, then dispatch from Starling class to get the data I need from the Flash class but it didn't work.
Fast solution , useful in some cases: make static function in mainClass and call it when your Main instance is ready (in addToStage for example)
definately we can't write any event or class to interact with flash and starling for this issue we can use CALL_BACK Function. so that you can interact or send data from core flash to starling and starling to core flash. Function call won't give any error.

Why can't I get access to an external class in actionscript 3?

I am working on a breakout game in adobe flash. I defined a document class, BreakOut.as, and set it to .fla file. I wrote another class Player.as, but I failed to get access to Player.as in my BreakOut.as. Here is the code:
BreakOut.as:
package
{
import flash.display.MovieClip;
import flash.display.Sprite;
public class BreakOut extends MovieClip
{
public function BreakOut()
{
var background:Background;
background= new Background();
addChild(background);
var playerone:Player;
playerone=new Player();
playerone.x=50;
playerone.y=50;
addChild(playerone);
}
}
}
Player.as:
package
{
import flash.display.MovieClip;
public class Player extends MovieClip
{
public function Player()
{
player.graphics.beginFill(0x000000);
player.graphics.drawRect(0,0,20,100);
}
}
}
Adobe flash keeps telling me: Access of undefined property Player. Well, Background.as is another class, and I can get access to it without any problems. But it just won't work on Player.as.
player.graphics.beginFill(0x000000);
player.graphics.drawRect(0,0,20,100);
By this, if you are trying to initialize Player by drawing a rectangle, you should be rather using this:
this.graphics.beginFill(0x000000);
this.graphics.drawRect(0,0,20,100);
Do note that Player.as should also be in the same path as the fla's classpath.

Cannot access ActionScript 3 Sprite.graphics.x methods

I have this code:
package graphics {
import flash.display.Sprite;
import flash.events.*;
public class Ball extends Sprite {
public function Ball(_stage){
_stage.addChild(this);
drawBall();
}
private function drawBall(){
graphics.beginFill(0x0000CC);
graphics.lineStyle(2,0x000000,1);
graphics.drawCircle(0,0,10);
graphics.endFill();
}
}
}
ADDED:
and the class that I pass to mxmlc:
package {
import flash.display.Sprite;
import graphics.*;
[SWF(width='1024', height='768', backgroundColor='#FFFFFF', frameRate='30')]
public class Application extends Sprite {
public function Application(){
var ball:Ball = new Ball(this);
}
}
}
Except that when I compile, I get the following error:
ball.as(11): col: 14 Error: Call to a possibly undefined method beginFill.
graphics.beginFill(0x0000CC);
^
Along with the other three graphics.x() calls.
I am probably doing something wrong here, but I do not know what. Do you?
Just use this.graphics.method to avoid confusions created by your package name.
Edit - after comment.
After some messing around, it seems that though this.graphics traces as a correct Graphics object as expected, the this.graphics.method is looked in the Ball class. Don't know what messes this up like this, but it can be solved with a simple casting.
package graphics {
import flash.display.Graphics;
import flash.display.Sprite;
import flash.events.*;
public class Ball extends Sprite {
public function Ball(_stage) {
_stage.addChild(this);
drawBall();
}
private function drawBall() {
var g:Graphics = this.graphics as Graphics;
g.beginFill(0x0000CC);
g.lineStyle(2,0x000000,1);
g.drawCircle(0,0,10);
g.endFill();
}
}
}
Good luck
I am afraid the problem must be elsewhere in your code. If I take your class and clean it up so it compiles like so:
package
{
import flash.display.Sprite;
public class Ball extends Sprite
{
public function Ball()
{
drawBall();
}
private function drawBall():void
{
graphics.beginFill(0x0000CC);
graphics.lineStyle(2,0x000000,1);
graphics.drawCircle(0,0,10);
graphics.endFill();
}
}
}
$ mxmlc Ball.as
Loading configuration file [...]/flex-config.xml
/private/tmp/actionscript/Ball.swf (666 bytes)
It draws a blue ball in the top left corner of the screen. So your problem with graphics must be related to code which you have not shown here.
Edit: Based on the new information:
Your namespace graphics for the class Ball conflicts with the property name graphics. You need to rename the package and the directory it lives in.
Your class can not add itself to the stage. It has no reference to the stage until it has been added to the display list. Your application class needs to both create the ball and add it to the stage.
Also, it's just stage, not _stage.