What is stupid problem with getters and setters?

Hello

Please don’t lie me! I am really mad because i have created own component frameworks for Openfl and haxe got many errors

Like getter and setter:

public var x(get, set):FLoat;
public function get_x():Float
{
     return _x;
}
public function set_x(value:Float)Float
public function get_x():Float
{
    return _x = value;
}
private var _x:Float = 0;

FlashDevelop says output:

Redefinition of variable width in subclass is not allowed. Previously declared at

Why does openfl 3.6.0 lie? What dpes ot happen with getters and setters?
I really want create custom rich component frameworks like haxeui, flexil-ui, stableui or waxe That is why I am creating rich component frameworks for my game…

How do i fix getters and setters?

Did you try to use override keyword?

Yes i tried override. But it says error for openfl test flash

package net.sourceskyboxer.components;
import openfl.display.DisplayObjectContainer;
import openfl.display.Sprite;
import openfl.events.Event;
/**
 * ...
 * @author Jens Eckervogt
 */
class Component extends Sprite
{
	private var _width:Float = 0;
	private var _height:Float = 0;
	private var _tag:Int = -1;
	private var _enabled:Bool = true;
	
	public static var DRAW:String = "draw";
	/**
	 * @param	parent
	 * @param	xpos
	 * @param	ypos
	 */
	public function new(parent:DisplayObjectContainer = null, xpos:Float = 0, ypos:Float = 0) 
	{
		super();
		setMove(xpos, ypos);
		init();
		if (parent != null)
		{
			parent.addChild(this);
		}
	}
	
	
	private function init():Void
	{
		addChildren();
		invalidate();
	}
	
	
	private function addChildren():Void
	{
		
	}
	
	private function invalidate():Void
	{
		draw();
		addEventListener(Event.ENTER_FRAME, onInvalidate);
	}
	
	/**
	 * 
	 * @param	xpos
	 * @param	ypos
	 */
	public function setMove(xpos:Float, ypos:Float):Void
	{
		x = Math.round(xpos);
		y = Math.round(ypos);
	}
	
	/**
	 * 
	 * @param	w
	 * @param	h
	 */
	public function setSize(w:Float, h:Float):Void
	{
		_width = w;
		_height = h;
		dispatchEvent(new Event(Event.RESIZE));
		invalidate();
	}
	
	public function draw():Void
	{
		dispatchEvent(new Event(Component.DRAW));
	}
	
	private function onInvalidate(evt:Event):Void
	{
		removeEventListener(Event.ENTER_FRAME, onInvalidate);
		draw();
	}
	
	/**
	 * 		getters and setters
	 */
	
	public var width(get, set):Float;
	
	override public function set_width(w:Float):Float
	{
		return w = _width;
		invalidate();
		dispatchEvent(new Event(Event.RESIZE));
	}
	
	override public function get_width():Float
	{
		return _width;
	}
	
	
	public var height(get, set):Float;
	
	override public function set_height(h:Float):Float
	{
		return h = _height;
		invalidate();
		dispatchEvent(new Event(Event.RESIZE));
	}
	
	override public function get_height():Float
	{
		return _height;
	}
	
	
	public var tag(get, set):Int;
	
	public function set_tag(value:Int):Int
	{
		return value = _tag;
	}
	
	public function get_tag():Int
	{
		return _tag;
	}
	
	
	public var x(default, null):Float;
	
	override public function set_x(value:Float):Float
	{
		return value = Math.round(x);
	}
	
	
	public var y(default, null):Float;
	
	override public function set_y(value:Float):Float
	{
		return value = Math.round(y);
	}
	
	
	public var enabled(get, set):Bool;
	
	public function set_enabled(value:Bool):Bool
	{
		return value = _enabled;
		mouseEnabled = mouseChildren = _enabled;
		tabEnabled = value;
		alpha = _enabled ? 1.0 : 0.5;
	}
	
	public function get_enabled():Bool
	{
		return _enabled;
	}
}

Output:

net/sourceskyboxer/components/Component.hx:95: characters 8-34 : Redefinition of variable width in subclass is not allowed. Previously declared at flash.display.DisplayObject
net/sourceskyboxer/components/Component.hx:110: characters 8-35 : Redefinition of variable height in subclass is not allowed. Previously declared at flash.display.DisplayObject
net/sourceskyboxer/components/Component.hx:138: characters 8-35 : Redefinition of variable x in subclass is not allowed. Previously declared at flash.display.DisplayObject
net/sourceskyboxer/components/Component.hx:146: characters 8-35 : Redefinition of variable y in subclass is not allowed. Previously declared at flash.display.DisplayObject
net/sourceskyboxer/components/Component.hx:119: lines 119-122 : Field get_height is declared 'override' but doesn't override any field
net/sourceskyboxer/components/Component.hx:104: lines 104-107 : Field get_width is declared 'override' but doesn't override any field
net/sourceskyboxer/components/Component.hx:112: lines 112-117 : Field set_height is declared 'override' but doesn't override any field
net/sourceskyboxer/components/Component.hx:97: lines 97-102 : Field set_width is declared 'override' but doesn't override any field
net/sourceskyboxer/components/Component.hx:140: lines 140-143 : Field set_x is declared 'override' but doesn't override any field
net/sourceskyboxer/components/Component.hx:148: lines 148-151 : Field set_y is declared 'override' but doesn't override any field
Build halted with errors.

for flash you need special getters and setters:

#if flash
    @:getter(width)
    private function get_width() : Float {
        return ...;
    }

    @:setter(width)
    private function set_width(value : Float) : Void {
        ...
    }
#else
    override private function get_width() : Float {
        return ...;
    }

    override private function set_width(value : Float) : Float {
        ...
        return value;
    }
#end

Thanks my guy your are good helper ;:slight_smile: If my frameworks is released than i will give you free framework because your good supportor

PS: I got error if i build as flash than flash player stopped to work :frowning:
I am surpised why does it happen?? I didn’t use lime or nme apps

Illegal override of Component in net.sourceskyboxer.components.Component.

What does it mean?

I didn’t put?

package net.sourceskyboxer.components;
import openfl.display.DisplayObjectContainer;
import openfl.display.Sprite;
import openfl.events.Event;
import openfl.events.EventDispatcher;
import openfl.display.DisplayObjectContainer;
/**
 * ...
 * @author Jens Eckervogt
 */
@:event(name = "resize", type = "openfl.events.Event")
@:event(name = "draw", type = "openfl.events.Event")
class Component extends Sprite
{
	private var _width:Float = 0;
	private var _height:Float = 0;
	private var _tag:Int = -1;
	private var _enabled:Bool = true;
	
	public static var DRAW:String = "draw";
	/**
	 * @param	parent
	 * @param	xpos
	 * @param	ypos
	 */
	public function new(parent:DisplayObjectContainer = null, xpos:Float = 0, ypos:Float = 0) 
	{
		super();
		setMove(xpos, ypos);
		openflInit();
		if (parent != null)
		{
			parent.addChild(this);
		}
	}
	
	
	private function openflInit():Void
	{
		addChildren();
		invalidate();
	}
	
	
	private function addChildren():Void
	{
		
	}
	
	private function invalidate():Void
	{
		draw();
		addEventListener(Event.ENTER_FRAME, onInvalidate);
	}
	
	/**
	 * 
	 * @param	xpos
	 * @param	ypos
	 */
	public function setMove(xpos:Float, ypos:Float):Void
	{
		x = Math.round(xpos);
		y = Math.round(ypos);
	}
	
	/**
	 * 
	 * @param	w
	 * @param	h
	 */
	public function setSize(w:Float, h:Float):Void
	{
		_width = w;
		_height = h;
		dispatchEvent(new Event(Event.RESIZE));
		invalidate();
	}
	
	public function draw():Void
	{
		this.dispatchEvent(new Event(Component.DRAW));
	}
	
	private function onInvalidate(evt:Event):Void
	{
		removeEventListener(Event.ENTER_FRAME, onInvalidate);
		draw();
	}
	
	/**
	 * 		getters and setters
	 */
	
	#if flash
		@:setter(width)
		private function set_width(w:Float ):Float 
		{
			return w = _width;
			invalidate();
			this.dispatchEvent(new Event(Event.RESIZE));
		}
		
		@:getter(width)
		private function get_width():Float 
		{
			return _width;
		}
	#else
		@:setter(width)
		override private function set_width(w:Float ):Float 
		{
			return w = _width;
			invalidate();
			this.dispatchEvent(new Event(Event.RESIZE));
		}
		
		@:getter(width)
		override private function get_width():Float 
		{
			return _width;
		}
	#end
	
	#if flash
		@:setter(height)
		private function set_height(h:Float ):Float 
		{
			return h = _height;
			invalidate();
			this.dispatchEvent(new Event(Event.RESIZE));
		}
	
		@:getter(height)
		private function get_height():Float 
		{
			return _height;
		}
	#else
		@:setter(height)
		override private function set_height(h:Float ):Float 
		{
			return h = _height;
			invalidate();
			this.dispatchEvent(new Event(Event.RESIZE));
		}
	
		@:getter(height)
		override private function get_height():Float 
		{
			return _height;
		}
	#end
	
	public var tag(get, set):Int;
	
	private function set_tag(value:Int):Int
	{
		return value = _tag;
	}
	
	private function get_tag():Int
	{
		return _tag;
	}
	
	
	#if flash
		@:setter(x)
		private function set_x(value:Float ):Float 
		{
			super.x = Math.round(value);
			return value;
		}
	#else
		@:setter(x)
		override private function set_x(value:Float ):Float 
		{
			super.x = Math.round(value);
			return value;
		}
	#end
	
	#if flash
		@:setter(y)
		private function set_y(value:Float ):Float 
		{
			super.y = Math.round(value);
			return value;
		}
	#else
		@:setter(y)
		override private function set_y(value:Float ):Float 
		{
			super.y = Math.round(value);
			return value;
		}
	#end
	
	public var enabled(get, set):Bool;
	
	private function set_enabled(value:Bool):Bool
	{
		return value = _enabled;
		mouseEnabled = mouseChildren = _enabled;
		tabEnabled = value;
		alpha = _enabled ? 1.0 : 0.5;
	}
	
	private function get_enabled():Bool
	{
		return _enabled;
	}
}

If i use openfl build windows and app started fine "WORKED"
If i use openfl build html and site started fine "WORKED"
If i use openfl build flash and app started fine “FAILED

And i got error message:

VerifyError: Error #1053: Illegal override of Component in net.sourceskyboxer.components.Component.
	at Main()
	at DocumentClass()
	at Type$/createInstance()
	at ApplicationMain$/start()
	at ApplicationMain$/init()
	at lime.app::Event_Void_Void/dispatch()
	at lime.app::Preloader/start()
	at openfl.display::Preloader/display_onComplete()
	at flash.events::EventDispatcher/dispatchEventFunction()
	at flash.events::EventDispatcher/dispatchEvent()
	at NMEPreloader/onLoaded()
	at openfl.display::Preloader/start()
	at lime.app::Preloader/current_onEnter()

But I didn’t use Lime and NME Frameworks - Only OpenFL/Flash frameworks

1 Like

Getters and setters are different on flash vs native in openfl… it is a little annoying to be fair, but since openfl has no control over the flash runtime it makes sense. This should work (im using something similar in haxeui v2):

#if !flash
    
private override function get_width():Float {
}
private override function set_width(value:Float):Float {
}
    
private override function get_height():Float {
}
private override function set_height(value:Float):Float {
}
    
#else
    
@:setter(width)
public function set_width(value:Float) {
}
@:getter(width)
public function get_width() {
}
	
@:setter(height)
public function set_height(value:Float) {
}
	
@:getter(height)
public function get_height() {
}

#end

Hi !
had a long experimenting time to override native flash/!flash getter/setter…
and already posted a solution to this.
the further access/return value of the native setter is to be considered too :

    #if flash @:getter(x) #end public #if !flash override #end function get_x():Float
    {
        // your invalidation system by example...
        return #if flash super.x; #else super.get_x(); #end
    }
    #if flash @:setter(x) #end public #if !flash override #end function set_x(_f:Float): #if flash Void #else Float #end
    {  
        // your invalidation system by example...
        #if flash super.x = _f; #else super.set_x(_f); #end
        #if !flash return _f; #end
    }

Hope it helps.
Cheers.

You can compress the code a little further:

#if flash @:getter(x) #else override #end
public function get_x():Float
//...
#if flash @:setter(x) #else override #end
public function set_x(_f:Float): #if flash Void #else Float #end

Normally I don’t like putting “override” before “public,” but in this case, I think it’s worth it.

1 Like

…and far more readable this way, you’re true.

Thanks for explanations for override and normal. I will try later.

@Stephane, @IanHarrigan1982 and @player_03 Thanks for support If my whole completed frameworks like MinimalComps for Haxe I promise that I will add Menu and MenuItem ( look like Ghostwire AspireUI )

Because I really wait for good userinterface like Haxe-UI but Haxe-UI is incompatible to Away3D So sad because I want components for my map editor like so close AwayBuilder

How does component work if I use “top”, “left”, “right” and “bottom” like Flex SDK Components?

If my frameworks release than I will give you free because you support my code to fix and find good solutions. that is why I want give you big thanks for support.

Good supporters: @player_03, @Stephane , @restorer and @IanHarrigan1982!

Do not worry - I want release my whole completed framework than you get free components.

Why does it happen?

I have tried why do you not say truth. But i create Label than it happens hide???

package net.sourceskyboxer.components;
import openfl.display.DisplayObjectContainer;
import openfl.display.Sprite;
import openfl.events.Event;
import openfl.events.EventDispatcher;
/**
 * ...
 * @author Jens Eckervogt
 */
@:event(name = "resize", type = "openfl.events.Event")
@:event(name = "draw", type = "openfl.events.Event")
class Component extends Sprite
{
	private var _width:Float = 0;
	private var _height:Float = 0;
	private var _tag:Int = -1;
	private var _enabled:Bool = true;
	
	public static var DRAW:String = "draw";
	/**
	 * @param	parent
	 * @param	xpos
	 * @param	ypos
	 */
	public function new(parent:DisplayObjectContainer = null, xpos:Float = 0, ypos:Float = 0) 
	{
		super();
		setMove(xpos, ypos);
		openflInit();
		if (parent != null)
		{
			parent.addChild(this);
		}
		
		if (xpos != 0)
		{
			xpos = 0;
		}
		
		if (ypos != 0)
		{
			ypos = 0;
		}
	}
	
	
	private function openflInit():Void
	{
		addChildren();
		invalidate();
	}
	
	
	private function addChildren():Void
	{
		
	}
	
	private function invalidate():Void
	{
		draw();
		addEventListener(Event.ENTER_FRAME, onInvalidate);
	}
	
	/**
	 * 
	 * @param	xpos
	 * @param	ypos
	 */
	public function setMove(xpos:Float, ypos:Float):Void
	{
		x = Math.round(xpos);
		y = Math.round(ypos);
	}
	
	/**
	 * 
	 * @param	w
	 * @param	h
	 */
	public function setSize(w:Float, h:Float):Void
	{
		_width = w;
		_height = h;
		dispatchEvent(new Event(Event.RESIZE));
		invalidate();
	}
	
	public function draw():Void
	{
		this.dispatchEvent(new Event(Component.DRAW));
	}
	
	private function onInvalidate(evt:Event):Void
	{
		removeEventListener(Event.ENTER_FRAME, onInvalidate);
		draw();
	}
	
	/**
	 * 		getters and setters
	 */
	
	// WIDTH
	#if flash @:getter(width) #else override #end
	public function get_width():Float
	{
		return _width;
	}
	
	#if flash @:setter(width) #else override #end
	public function set_width(_f:Float): #if flash Void #else Float #end
	{
		 #if !flash return _f = _width; #end
		invalidate();
		this.dispatchEvent(new Event(Event.RESIZE));
	}
	
	
	// HEIGHT
	#if flash @:getter(height) #else override #end
	public function get_height():Float
	{
		return _height;
	}
	
	#if flash @:setter(height) #else override #end
	public function set_height(_f:Float): #if flash Void #else Float #end
	{
        #if !flash return _f = _height; #end
		invalidate();
		this.dispatchEvent(new Event(Event.RESIZE));
	}
	
	
	// Y
	#if flash @:getter(y) #else override #end
	public function get_y():Float
	{
		return #if flash super.y; #else super.get_y(); #end
	}
	
	#if flash @:setter(y) #else override #end
	public function set_y(_f:Float): #if flash Void #else Float #end
	{
		#if flash super.y = _f; #else super.set_y(_f); #end
        #if !flash return _f; #end
	}
	
	// X
	#if flash @:getter(x) #else override #end
	public function get_x():Float
	{
		return #if flash super.x; #else super.get_x(); #end
	}
	
	#if flash @:setter(x) #else override #end
	public function set_x(_f:Float): #if flash Void #else Float #end
	{
		#if flash super.x = _f; #else super.set_x(_f); #end
        #if !flash return _f; #end
	}
	
	public var tag(get, set):Int;
	
	private function set_tag(value:Int):Int
	{
		return value = _tag;
	}
	
	private function get_tag():Int
	{
		return _tag;
	}
	public var enabled(get, set):Bool;
	
	private function set_enabled(value:Bool):Bool
	{
		return value = _enabled;
		mouseEnabled = mouseChildren = _enabled;
		tabEnabled = value;
		alpha = _enabled ? 1.0 : 0.5;
	}
	
	private function get_enabled():Bool
	{
		return _enabled;
	}
	
	/**public var ssbTop(get, set):Float;
	
	private function get_ssbTop():Float
	{
		return this.y;
	}
	
	private function set_ssbTop(value:Float):Float
	{
		return value = this.y;
	}
	
	public var ssbLeft(get, set):Float;
	
	private function get_ssbLeft():Float
	{
		return this.x;
	}
	
	private function set_ssbLeft(value:Float):Float
	{
		return value = this.x;
	}**/
}

And I create Label:

package net.sourceskyboxer.components;
import openfl.display.DisplayObjectContainer;
import openfl.text.TextField;
import openfl.text.TextFormat;
import openfl.text.TextFieldAutoSize;
import openfl.events.Event;
import openfl.events.EventDispatcher;
/**
 * ...
 * @author Jens Eckervogt
 */
@:event(name = "draw", type = "openfl.events.Event")
class Label extends Component
{
	private var _autoSize:Bool = true;
	private var _text:String = "";
	private var _tf:TextField;
	
	/**
	 * 
	 * @param	parent
	 * @param	xpos
	 * @param	ypos
	 * @param	text
	 */
	public function new(parent:DisplayObjectContainer=null, xpos:Float=0, ypos:Float=0, text:String = "") 
	{
		this._text = text;
		super(parent, xpos, ypos);
	}
	
	override private function openflInit():Void
	{
		super.openflInit();
		mouseEnabled = false;
		mouseChildren = false;
	}
	
	override private function addChildren():Void
	{
		_height = 24;
		_tf = new TextField();
		
		_tf.height = _height;
		_tf.embedFonts = Style.embedFonts;
		_tf.selectable = false;
		_tf.mouseEnabled = false;
		_tf.defaultTextFormat = new TextFormat(Style.fontName, Style.fontSize, Style.LABEL_TEXT);
		_tf.text = _text;
		addChild(_tf);
		draw();
	}
	
	
	override function draw():Void
	{
		super.draw();
		_tf.text = _text;
		if(_autoSize)
		{
			_tf.autoSize = TextFieldAutoSize.LEFT;
			_width = _tf.width;
			this.dispatchEvent(new Event(Event.RESIZE));
		}
		else
		{
			_tf.autoSize = TextFieldAutoSize.NONE;
			_tf.width = _width;
		}
		_height = _tf.height = 18;
	}
	
	
	public var text(get, set):String;
	
	private function set_text(t:String):String
	{
		return t = _text;
		if(_text == null) _text = "";
		invalidate();
	}
	
	private function get_text():String
	{
		return _text;
	}
	
	
	public var autoSize(get, set):Bool;
	
	private function set_autoSize(b:Bool):Bool
	{
		return b = _autoSize;
	}
	
	private function get_autoSize():Bool
	{
		return _autoSize;
	}
	
	
	public var textfield(default, null):TextField;
	
	private function get_textField():TextField
	{
		return _tf;
	}
}

And try Main:

package;
import openfl.display.Sprite;
import net.sourceskyboxer.components.Label;
/**
 * ...
 * @author Jens Eckervogt
 */
class Main extends Sprite 
{
	public function new() 
	{
		super();
		
		var lbl:Label = new Label(this, 40, 40, "Hello World!");
		addChild(lbl);
	}
}

I try build “openfl test flash” = Nothing see label…
HTML working! Windows/CPP working! Neko working!

What does it happen with your improvement???

Why not in Flash??? Please explain me! I can not understand that why do you use bad return set_x(_f); I don’t know that.

I am really mad to you because your code doesn’t work for me if i use flash and cpp than it doesn’t want to “override” remove all override only public What does it get fuck?

enter link description here

Than flash develop won’t work with “override” because I am using version of fd now 5.1.1

I can not understand Flash swf can not show if who create content of swf than it is like “hidden” ???

Other build-targets like HTML5, cpp, neko, Android and other… have not problem…

But FLASH won’t output if you open swf? Has flash error if it is incompatibly?

Only use “override” if you’re overriding something.

#if flash @:setter(text) #end //No "override."
private function set_text(t:String): #if flash Void #else String #end
{
        _text = t;
	if(_text == null) _text = "";
	invalidate();
        #if !flash return _text; #end
}

#if flash @:getter(text) #end //Still no "override."
private function get_text():String
{
	return _text;
}

Also be aware that in Flash, a setter cannot return a value. It must return Void. However, in anything besides Flash, a setter must return a value.

I can not understand why does it happen with flash sees always “hidden”

I can not understand why does flash not work if i use default font "_sams"
Than it shows error message. like Post #3

No thanks! Doesn’t work with Flash :frowning:

But I have installed Flash Player Standalone. Why does it not work ?

I am mad now…

Sorry, what?

That should be “_sans.”

Also, embedFonts must be false when using built-in fonts.

Don’t declare var x(get, set):Float or var y(get, set):Float. Those are already declared.

SourceSkyBoxer

With the methods we gave you - that work, I’m pretty sure of that :wink: - you do not have to work with Composition. I mean, in your framework, if Label is a TextField, it should extend TextField and not Sprite (by your Component) (in Composition vs Heritage talkings), so you should consider create a special TextField class which include your own behaviors…

That’s what I do for my components set.
EComponent extends Sprite.
ETextField extends TextField.
EShape extends Shape.
All of them implements an IComponent interface.

I do not really know for the performance, but personaly, I won’t duplicate all properties with a " _ " (_text, _x, _y, _width, etc…) to work with an overriden getter/setter.
The hints we gave you, allow you to directly use the normal properties (text, x, y, width, height).