Can't find this bug! "Cannot access private field..."

Hello,

I would post this on StackOverflow but Haxe is not that popular so I’m trying here. Thanks in advance for the help!

My error is "src/Main.hx:62 characters 19-23 : Cannot access private field isColorOn"
My modules are Main.hx and GridItem.hx

=======================================================
GridItem.hx


package src;

import openfl.display.Graphics;
import openfl.display.Sprite;
import openfl.events.MouseEvent;

class GridItem extends Sprite { 

	var circle:Sprite;   
	var offColor:UInt = 0xF1F1F1;
	var onColor:UInt = 0xD3D3D3; 
	var circleRadius:UInt = 11;  
	var isColorOn:Bool = false;
	
	public function new() { 
	
		super();
		
		circle = new Sprite(); 
		this.addChild(circle); 
		colorOff();   
	}
	
	public function setRadius(circleRadius:UInt) {
	
		this.circleRadius = circleRadius;
	}
	
	function colorCircle(color:UInt) { 	 

		var g:Graphics = circle.graphics; 
		g.clear(); 					
		g.beginFill (color);
		g.drawCircle (circleRadius, circleRadius, circleRadius); 
		g.endFill();											
	}
	
	public function getColorState():Bool {

		return isColorOn;              
	}
	
	public function colorOff() {

		isColorOn = false;      
		colorCircle(offColor);  
	}
	
	public function colorOn() {  
		isColorOn = true;      
		colorCircle(onColor); 
	}	
}

=======================================================
Main.hx


package;

import openfl.display.Sprite;
import openfl.events.MouseEvent;
import src.GridItem;


class Main extends Sprite {
	
	var isMouseDown:Bool;
	var isWantedColor:Bool;
	
	public function new () {
		
		super ();
		
		init();
	}
	
	function init() {
	
		var gridItem:GridItem; 
		var numRows:UInt = 10;
		var numCols:UInt = 10;
		var _circleRadius:Int = 11; 
		var circleMargin:Int = 1;  
		var offset:Int = _circleRadius * 2 + circleMargin * 2; 
		
		for (row in 0...numRows) {
			
			for (column in 0...numCols) {
				
				gridItem = new GridItem();
				gridItem.setRadius(_circleRadius);
				gridItem.x = offset * column; 
				gridItem.y = offset * row;

				gridItem.addEventListener (MouseEvent.MOUSE_DOWN, onCircleDragStart);
			
				gridItem.addEventListener (MouseEvent.MOUSE_OVER, onCircleDragOver);
				

				this.addChild(gridItem);
			}
		}
	}

	function setColor(gridItem:GridItem) {

		if (isWantedColor) {
			gridItem.colorOn();
		}
		else {
			gridItem.colorOff();
		}
	}
	
	function onCircleDragStart(e:MouseEvent):Void {  
	
		isMouseDown = true;       
		var gridItem:GridItem = cast e.currentTarget;
		isWantedColor = !gridItem.isColorOn; 
		setColor(gridItem);
		stage.addEventListener(MouseEvent.MOUSE_UP, onCircleDragEnd);
	}

	function onCircleDragOver(e:MouseEvent):Void {
	
		if (isMouseDown) {

			var gridItem:GridItem = cast e.currentTarget;
			setColor(gridItem);
		}
	}
	
	private function onCircleDragEnd(e:MouseEvent):Void {
	
		isMouseDown = false;
		stage.removeEventListener(MouseEvent.MOUSE_UP, onCircleDragEnd);
	}
}

Variables are defined as “private” by default, so if you dont specify it will only be accessible inside the class… add an access modifier to change that, ie:

public var isColorOn:Bool = false
1 Like

Also, just quickly, it might be nicer to use getter and setters in general and dump the whole “colorOn”, “colorOff” stuff, eg:

private var _isColorOn = false;
public var colorOn(get, set):Bool;
private function get_colorOn():Bool {
    return _isColourOn;
}
private function set_colorOn(value:Bool):Bool {
    _isColorOn = value;
    colorCircle(_isColorOn == true ? onColor : offColor); 
    return value;
}

Then you can just use myCircle.colorOn = true/false

1 Like

Okay thanks! I will give it a try!

Solved!

I had it working before so I knew it was something small and stupid.
in the line I’ve now changed to:
isWantedColor = !gridItem.getColorState();
I was putting onColor (the bool) instead of the function above, forgetting the latter’s existence and mistaking the bool for a function. I even convinced myself that assigning the bool to the other bool should work, but obviously you need getter/setter things to access them.

Although I already know about the “getters and setters” practice and have used it before (in C++ mainly), I still may not be doing it right. I will still see if I can improve my code based on your replies :slight_smile:

Thanks!

1 Like