Override native getter / setter in flash and native targets

Hello,

I’m trying to override the width property of a sprite.
After many tests I have the expected result working on flash and IOS… But I do not find any other way than using with conditionnal compilation… I’m probably missing someting.
Any idea to have an unique code running on all targets ?

#if flash
@:getter(height)
public function getHeight():Float {
    return _height;
}

@:setter(height)
public function setHeight(value:Float):Void {
    _height = value;
}
#else
override function get_height():Float {
    return _height;
}
override function set_height(value:Float):Float {
    _height = value;
    return _height;
}
#end

Thank you.

Jonas

Hope it can help you !

public function set_cx(_f:Float):Float
{
	#if flash
	super.x = _f ;
	return super.x;
	#elseif js
	return super.set_x(_f);
	#elseif (cpp || neko)
	return super.set_x(_f);
	#end
}
public function get_cx():Float
{
	#if flash
	return super.x;
	#elseif js
	return super.get_x();
	#elseif (cpp || neko)
	return super.get_x();
	#end
}
public function set_cy(_f:Float):Float
{
	#if flash
	super.y = _f;
	return super.y;
	#elseif js
	return super.set_y(_f);
	#elseif (cpp || neko)
	return super.set_y(_f);
	#end
}
public function get_cy():Float
{
	#if flash
	return super.y;
	#elseif js
	return super.get_y();
	#elseif (cpp || neko)
	return super.get_y();
	#end
}


//Width / Height
#if flash	// Overriding native width getter. 
@:getter(width) public function get_width():Float
{
	return super.width;
}
#elseif (js || cpp || neko) 
public override function get_width():Float
{
	var width:Float = super.get_width();
	return width; 
}
#end
#if flash	// Overriding native width getter. 
@:getter(height) public function get_height():Float
{
	return super.height;
}
#elseif (js || cpp || neko) 
public override function get_height():Float
{
	var height:Float = super.get_height();
	return height; 
}
#end

Thank you. So there is no crossplateform solution for getter / setter…

Would something like this work?

@:getter(width) private #if !flash override #end function get_width ():Float {
    
    return 100;
    
}

If you override a Haxe method, you can just override the “get_” or “set_” method, but this is different than overriding a built-in ActionScript class property, which works with @:getter or @:setter

3 Likes

Yes. Perfect for getter but more complicated for setter because the 2 targets don’t need the same returns :

@:setter(width) #if !flash override #end function set_width(value:Float) {
    this._width = value;
    #if !flash return this._width; #end
}

I will look later if it is possible to use a preprocessor for this stuff.

Thank you.

2 Likes

A “type building” macro would work.

1 Like