Initializing a Vector


(David Williams) #1

I’m trying to pass colors into a vector, like this:

	public function Button(_graphic:Image, _colors:Vector.<uint> =  Vector.<uint>([0xFF0000, 0x770000, 0xFF5555, 0x333333]), _x:Number = 0, _y:Number = 0, _f:Function = null)
	{
		this._buttonImage = _graphic;
		this.colors = _colors;
		super(_x, _y);
		setHitbox(_graphic.width, _graphic.height);
		this.callback = _f;
		graphic = this.buttonImage;
	}

But, I’m getting the build error:

C:\Users\Daivd\Desktop\FBLA\FBLAproject\src\Button.as(19): col: 67 Error: Parameter initializer unknown or is not a compile-time constant.
public function Button(_graphic:Image, _colors:Vector.<uint> =  Vector.<uint>([0xFF0000, 0x770000, 0xFF5555, 0x333333]), _x:Number = 0, _y:Number = 0, _f:Function = null)
^(pointing to = Vector.<uint> ([]) bit.)
Build halted with errors (fcsh).
(fcsh)
Done(1)

I’ve never worked with Vectors before, but this seems to be what everyone does and it works for them, but for some reason it isn’t working for me. Anybody see what I’m doing wrong?


(Bora Kasap) #2

if an entity have no hitbox, then it also have no width and height. so you can’t define hitbox width with graphic width if you didn’t defined it by another way before… (actually i’m not sure about that, i may know somthing wrong… because that doesn’t make sense…)

and, maybe color codes needs to be strings?, try to put them in “0xFF0000”, “0xFF0000” like that,

and one last thing, i don’t know programming good enough but may your function needs “function();void” ‘void’ parameter?

i’m so noob, i’ve just said some noob things…


(Bora Kasap) #3

and maybe this:

what you used: _colors:Vector. = Vector.([0xFF0000, 0x770000, 0xFF5555, 0x333333])

what i suggest: _colors:Vector. = new Vector.([0xFF0000, 0x770000, 0xFF5555, 0x333333])


(David Williams) #4

When I pass a graphic when instantiating a new button, I can then use _graphic.width/height to get the dimensions of that image.

This is the constructor of a class, so a return value is not needed.

I actually tried this, it yielded the same result. What’s strange to me is if I remove the “= Vector.([])” part, and just pass it when I declare a new variable, like so:

var _b:Button = new Button(Assets.buttonGraphic,  Vector.<uint>([0x0000ff, 0x000077, 0x5555ff, 0x333333]), 5, 20);
add(_b);

It works. So, for some reason, it isn’t letting me give it a “default” value if you will.


(Jacob Albano) #5

The issue is that these are both runtime values:

// casting an array to a vector
Vector.<uint>([0x0000ff, 0x000077, 0x5555ff, 0x333333])

// constructing a vector with an array
new Vector.<uint>([0x0000ff, 0x000077, 0x5555ff, 0x333333])

Default parameters must be compile-time values, like literal numbers, strings, or booleans.

Here’s what I would suggest:

	private static const defaultColors::Vector.<uint> = new Vector.<uint>([0x0000ff, 0x000077, 0x5555ff, 0x333333]);

	public function Button(_graphic:Image, _colors:Vector.<uint> =  null, _x:Number = 0, _y:Number = 0, _f:Function = null)
	{
		// set this.colors to the default if the argument isn't null
		this.colors = _colors || defaultColors;

		this._buttonImage = _graphic;
		super(_x, _y);
		setHitbox(_graphic.width, _graphic.height);
		this.callback = _f;
		graphic = this.buttonImage;
	}