1151: A conflict exists with definition mcPlayer in namespace internal - actionscript-3

Every time I try to run my ActionScript 3 program, I get this error:
1151: A conflict exists with definition mcPlayer in namespace internal.
Here is my script:
package
{
import flash.display.MovieClip;
public class Knight extends MovieClip
{
var mcPlayer:MovieClip;
public function Knight()
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown)
stage.addEventListener(KeyboardEvent.KEY_UP, keyUp)
}
private function keyUp(e:KeyboardEvent):void
{
trace(e.keyCode)
}
private function keyDown(e:KeyboardEvent):void
{
}
}
}
How can I fix it?

If I understand things correct, your Library contains some object with class Knight and inside this object there's a child MovieClip of instance name mcPlayer. The thing is, all designed elements go to namespace public, while all the class fields/methods with no explicitly stated namespaces go to internal.
Always declare designed elements as public:
public var mcPlayer:MovieClip;

Related

Flash AS3 - Trying to access public function from another class throws error?

This is my Main.as:
package {
import flash.display.MovieClip;
public class Main extends MovieClip {
public function Main() {
// Code here
}
public function myFunc() {
trace('!!!!');
}
}
}
When I try accessing it from another class using the following code, Flash throws me Error #2136:
package {
import flash.display.MovieClip;
import Main;
public class MyClass extends MovieClip {
public var m:Main;
public function MyClass() {
m = new Main();
m.myFunc();
}
}
}
EDIT: One more thing. The second class is attached to a MovieClip and exported on the first frame. I thought it made no difference, but someone in the comments have told me it does and apparently that's what's causing the error in the first place. In that case, how can I access the public function from a class attached to a MC?
Next time post the error message itself, most of us do not have Flash errors memorized by id. ;)
From the docs:
2136 The SWF file %1 contains invalid data.
This rings a bell for me. Your Main class is probably your document class. The document class is a special class that cannot be instantiated.
To access properties and methods of the document class instance from other code, you simply need a reference to the document class instance.
There are many ways you could get a reference, as it is really just a code dependency design question. Two common, easy solutions are:
1. Use the root property of any display object that is added as a child of the document class instance. Since the root property is typed to DisplayObject you need to cast to your document class to access its methods and property, for example: Main(root).myFunc().
2. Use the singleton pattern and assign a static public reference to the document class instance:
public class Main {
public static var main:Main;
public function Main() {
main = this;
}
public function myFunc():void { }
}
// usage:
Main.main.myFunc();

Acces of undefined property cerc

I have this code in AS3:
package clase
{
import flash.display.MovieClip;
import flash.events.Event;
/**
* ...
* #author cry
*/
public class CercNegru extends MovieClip
{
var growthRate:Number = 2;
cerc.addEventListener(Event.ENTER_FRAME,grow);
public function CercNegru()
{
}
private function grow(e:Event):void
{
trace("asdda");
}
}
}
When you run this program receive error:
Line 12 1120: Access of undefined property cerc.
Line 12 1120: Access of undefined property grow.
I put an image to understand better :
Can you help me to solve this problem please?
Thanks in advance!
The errors are because in class files, all functional code needs to live inside a function.
So take this line, which is just floating in the class:
cerc.addEventListener(Event.ENTER_FRAME,grow);
And move into the constructor (assuming you want it to run right away when you instantiate the class):
public function CercNegru()
{
cerc.addEventListener(Event.ENTER_FRAME,grow);
}
In class files, the constructor (which is the function whose name exactly matches the class name), is what get's called you use the new keyword.
So doing new CercNegru() will call that function.
NOW, I'm also assuming that this class file is attached to FlashPro library object, and you have something on the timeline with an instance name of cerc. (if that is not the case, then that is the reason for your error)
Timeline stuff though, isn't always available in the constructor, so you may need to wait until the instance has been added to the screen.
public var cerc:MovieClip; //you may want to create a reference to the timeline item, so you get compile time checking
public function CercNegru()
{
this.addEventListener(Event.ADDED_TO_STAGE, addedToStage);
}
private function addedToStage(e:Event):void {
this.removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
//this is the equivalent of timeline code now
cerc.addEventListener(Event.ENTER_FRAME,grow);
}
package clase
{
import flash.display.MovieClip;
import flash.events.Event;
/**
* ...
* #author cry
*/
public class CercNegru extends MovieClip
{
var growthRate:Number = 2;
var cerc:DisplayObject; // ADD THIS
public function CercNegru()
{
cerc.addEventListener(Event.ENTER_FRAME,grow);
}
private function grow(e:Event):void
{
trace("asdda");
}
}
}
As the error says cerc is undefined. So you should define it. Assuming that your 'cerc' is a Sprite,
var cerc:Sprite;

ActionScript 3 - Stage object is null for first operation

I have basically been trying to create a chess game, and it has some basic functionality. I have encountered some runtime errors that I can't seem to fix. I've tried searching around for solutions, and I have found several, but none of them seem to work for me.
I then created a new flash project to see if the error still occurs, just to make sure that it's not another part of my program that is causing the issue.
The stage has a pentagon object (a MovieClip) on it, which has an instance name of PentaGray and a class of Gray. There is an intermediary class called PentaClass.
The Main (document) class:
import flash.display.MovieClip;
import PentaClass;
import Gray;
public class Main extends MovieClip
{
// public var Natsu:String;
public function Main()
{
FairyTail();
}
public function FairyTail()
{
trace("PentaGray = " + PentaGray);
trace("PentaGray's name is " + PentaGray.name);
PentaGray.TraceThis();
}
}
PentaClass class:
public class PentaClass extends Main
{
public function PentaClass()
{
}
public function TraceThis():void
{
trace("Fairy Tail :D");
}
}
Gray class:
public class Gray extends PentaClass
{
public function Gray()
{
// TraceThis();
}
}
Running the program like this causes this to be printed to the output window:
PentaGray = null
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Main/FairyTail()
at Main()
at PentaClass()
at Gray()
at flash.display::Sprite/constructChildren()
at flash.display::Sprite()
at flash.display::MovieClip()
at Main()
PentaGray = null
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Main/FairyTail()
at Main()
If I change the code in the Main class to:
if (PentaGray)
{
trace("PentaGray's name is " + PentaGray.name);
PentaGray.TraceThis();
}
Then this is output:
PentaGray = null
PentaGray = [object Gray]
PentaGray's name is PentaGray
Fairy Tail :D
Also, if I run the function from Gray by uncommenting the statement in its constructor, it works properly, but for my game, I need to run a subclass function from the document class.
What I would like to to know is, why is PentaGray null when the program starts? I thought that the objects that are placed on the stage using the IDE are initialised before any written code runs, but this doesn't seem to be the case.
I've tried using event listeners (ENTER_FRAME and a Timer) but they still didn't solve the issue.
How can I change the program so that the object isn't null when certain code, e.g. the FairyTail() function, is executed? Have I not written my code properly or is this an issue with Flash/ActionScript itself?
By putting the call to FairyTail() in the Main class in the Main function, you have put that in the 'constructor' for the main object. The constructor of the Main object is the very first thing that has to run, because nothing can be created until the main object is created. And when the main object is created, the constructor is called first.
If you want something to run right when PentaGray is created, put it in the constructor of PentaClass:
public class PentaClass extends Main
{
public function PentaClass()
{
Main.FairyTail();
}
}
Or, if you just want it to happen at the start, you can just put it after the class declaration in the main file.
#main document
import flash.display.MovieClip;
import PentaClass;
import Gray;
public class Main extends MovieClip {
//...
}
Main.FairyTail();
By the way, it doesn't make sense to me that the PentaClass extends the Main class. Why don't you just make another class for it to extend?
Off course, your stage is not ready. Use the bootstrap done below in your main class.
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);
FairyTail();
}
Cheers

AS3: one behaviour to many objects

I'm learning AS3 and creating a simple 'Asteroids' game.
I have written a simple class of linear movement:
package {
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
public class lin extends MovieClip {
private var vx:Number=0;
private var vy:Number=0;
public function lin(x:Number,y:Number,sr:Number,spd:Number)
{
this.rotation=sr;
vy+=Math.sin(degToRad(sr))*spd;
vx+=Math.cos(degToRad(sr))*spd;
this.x=x+vx;
this.y=y+vy;
addEventListener(Event.ENTER_FRAME,loop,false,0,true);
}
public function loop(evt:Event)
{
y+=vy;
x+=vx;
if (outOfBounds())
kill();
}
public function outOfBounds():Boolean
{
return (x>stage.stageWidth || x<0 || y>stage.stageHeight || y<0);
}
public function kill():void
{
if(parent)
parent.removeChild(this);
removeEventListener(Event.ENTER_FRAME,loop);
}
public function degToRad (deg:Number)
{
return deg * Math.PI / 180;
}
}
}
And I need to set this behaviour of movement to several objects (LaserBeam, Asteroids)
I created a new MovieClip with class 'LaserBeam' and wrote this:
package {
import flash.display.MovieClip;
import flash.display.Stage;
import lin;
public class LaserBeam extends MovieClip {
public var LaserBeamInstance:lin;
public var LaserSPD=15;
public function LaserBeam(x,y,r) {
LaserBeamInstance=new lin(x,y,r,LaserSPD);
}
}
}
But when I try to run my game, it says:
Line 1 1203: No default constructor found in base class lin.
What should I do to make many different MovieClips share one behaviour?
Thanks in advance!
UPD: all project files is here
Try calling super() from your lin constructor. This is because it inherits from MovieCLip. Properties such as this.rotation won't be initialized until you call the MovieClip constructor with super()
You should also make the class name 'Lin' to follow the standard naming convention.
public function Lin(x:Number,y:Number,sr:Number,spd:Number)
{
super();
this.rotation=sr;
vy+=Math.sin(degToRad(sr))*spd;
vx+=Math.cos(degToRad(sr))*spd;
this.x=x+vx;
this.y=y+vy;
addEventListener(Event.ENTER_FRAME,loop,false,0,true);
}
I think you should try:
public function lin(x:Number = 1,y:Number = 1,sr:Number = 1,spd:Number = 1)
I don't know exactly what's happening, but I think that you have pointed you class "lin" as base class for some of your symbols in your library.
So, that symbols try to construct themselves by calling "lin" constructor, but of cause without any parameters.
BTW, Golden rule:
-variable names should start from lowercase letter
-names of classes and constructors from uppercase
In answer to your question:
What should I do to make many different MovieClips share one behaviour?
You should read about OOP patterns. This book is a great introduction.
http://www.amazon.com/First-Design-Patterns-Elisabeth-Freeman/dp/0596007124
In response to your error, I think you are not giving us the whole picture. The error refers to class lin being extended, but in the code there are no classes extending lin.

Reference MovieClip After it is Added to Stage as a Child

I am currently having problems referencing a MovieClip child which I add to the Stage from the Document Class. Basically when the MovieClip child is added to the Stage from the Document Class, I want a certain MovieClip already on the Stage to reference it once it is on the Stage.
Also, if it is possible, I don't want the MovieClip referencing the child being added to the Stage to have parameters linking it with the Document Class, because I plan on nesting this MovieClip within another MovieClip later on in the future.
Here is the code for the MovieClip class which is referencing the child once it is added to the Stage:
package com.gameEngine.assetHolders
{
import com.gameEngine.documentClass.*;
import com.gameEngine.assetHolders.*;
import com.gameEngine.assetHolders.Levels.*;
import flash.display.*;
import flash.events.*;
public class FallingPlatform extends MovieClip
{
public var _document:Document;
// Trying to reference "_player"
public var _player:Player;
public var fallState:Boolean;
public var platformYSpeed:Number = 0;
public var platformGravityPower:Number = 0.75;
public function FallingPlatform()
{
this.addEventListener(Event.ADDED_TO_STAGE, initFallingPlatform);
// constructor code
}
public function initFallingPlatform(event:Event)
{
this.addEventListener(Event.ENTER_FRAME, dynamicFall);
this.addEventListener(Event.ENTER_FRAME, hitTest);
}
public function dynamicFall(event:Event)
{
if (this.fallState)
{
this.platformYSpeed += this.platformGravityPower;
y += this.platformYSpeed;
}
}
// Trying to reference "_player"
public function hitTest(event:Event)
{
if (this.hitTestPoint(_player.x, _player.y + 1, true))
{
this.fallState = true;
}
}
}
}
The player is initialized in the Document class, right? So for me, the best option is either passing the player reference in the constructor of your FallingPlatform class like this
public function FallingPlatform (thePlayer:Player) {
this._player = thePlayer
}
or having a setter method to pass it to it. In this way, you're not tying the structure of your code
public function set player (thePlayer:Player):void {
this._player = thePlayer
}
Hope it helps!
If you set a document class for a fla file every movieclip on the stage can be accessed by it's instance name - just as you wold create a var with its name.
Event more, you can do something like that:
If you place two movieclips on the stagefor example mc1 and mc2 you can add them as variables to the document class.
package{
public class DocClass{
public var mc1:MovieClip;
public var mc2:MovieClip;
[...]
}
}
and than you can access those movieclips from your class with code hints form your IDE (flash or flashbuilder)
the opposite is also availible: define variables in your class and than access them in flash
! it works best when your document class extends a Sprite, I haven;t tested it on extending from a MovieClip but it should also work