Reputation: 1187
My problem is like this question: ArgumentError: Error #1063
The difference is I am getting this error after my Document Class loads and I click on a button to start the Game() class. The PowerUp() class generates an error automatically without being called by the program. I found the first parameter in the PowerUp() class constructor is not receiving a value.
public function PowerUp(tar:Object,s:Number=1, a:String="bonusShots",t:String="Multi Shot", l:int=100):void {/// more code
I need to put the movieclip "thePlayer" from the stage, into the first parameter of the PowerUp() class constructor.
When I add the "thePlayer" into the constructor I get another error " object not a compile time constant".
var thePlayer: MovieClip=thePlayer;
public function PowerUp(tar:Object=thePlayer,s:Number=1, a:String="bonusShots",t:String="Multi Shot", l:int=100):void {/// more code
Question: How do I incorporate the 'thePlayer' movieclip into the constructor of the PowerUp class?
Here is the code for the PowerUp Class
public class PowerUp extends Sprite{
protected var type:String;// type of power up
protected var strength:Number;// power of power up
protected var attribute:String;// attribute effected by power up
protected var target:Object;// target to buff/boost/perk up/ empower/improve/enhance
protected var lifespan:int;// the life span of a perk
protected var maxLifeSpan:int; // max time to live grahic
const BAR_WIDTH=100;// width of rect
const BAR_HEIGHT=15;// height of rect
public function PowerUp(tar:Object,s:Number=1, a:String="bonusShots",t:String="Multi Shot", l:int=100):void {
type=t;
strength=s;
attribute=a;
target=tar;
lifespan=l;
maxLifeSpan=l;
perkName.text=t;
perkName.blendMode="invert";
// apply perk to target
target[attribute]+=strength;
}// end constructor
Upvotes: 0
Views: 768
Reputation:
You can't provide arguments for the constructor for the classes that are constructed automatically. As you've seen, you can only use compile time constants for default values. In other words, only what compiler understands as being a value when it parses your code.
Your best option is to avoid passing arguments in constructor and treat it as a technical function required from you by the runtime, and, instead do the initialization job in some other function you can call yourself.
For example, suppose this is your document class:
public class DocumentClass extends Sprite {
public function DocumentClass() {
super();
}
public function powerUp(tar:MovieClip):void {
this.tar = tar;
}
}
and elsewhere, where you need to initialize this class you would call the powerUp
function.
Upvotes: 1
Reputation: 3851
var thePlayer: MovieClip=thePlayer;
Here you are assigning a variable as itself. Try using something like:
var _thePlayer:Player = new Player();
Player must exist somewhere, either as a class or a shared library item.
If you are always going to be passing a Player object to the PowerUp class you might as well cast the first argument as Player.
public function PowerUp(tar:Player,
Upvotes: 1