[SOLVED] Changing color on class objects from main

howdy folks,

this is something that is strange that i cant figure out. but what i have is a super simple class, a player class. that has its color set using this.graphics.beginFill(0xaColorHexThing); etc etc to finish it off.

but on trying in the main.hx class to change to color, i cant seem to change at all.
say in the main.hx class, i would put player.graphics.whatever to try and change the color, and whilst it does play, it does nto change the color.
just so i have the 1 player class, that does get called in many times in the main.hx, but would want to change the color depending on the player numbers.

how can i access to change the color without having to create many of the same class and changing the color?

thanks

just make sure you graphics.beginFill then graphics.drawRect (or whatever you are going to draw), and graphics.clear if you have to, if re-using objects for multiple things

This should work:

var colors = [ 0xFF0000, 0x00FF00, 0x0000FF ];

for (i in 0...4) {
    
    player[i].graphics.beginFill (colors[i]);
    player[i].graphics.drawRect (0, 0, 100, 100);
    
}
1 Like

awesome balls. thank you very much.

should i still have it set like this in Player.hx? or should i set the beginFill blank?

thanks :wink:

public function new() {
	super ();
	this.graphics.beginFill(0x33C3F0);
	this.graphics.drawRect(0,0,100,15);
	this.graphics.clear();
	this.graphics.endFill();
}

that might be your problem, basically it means:

  • Set draw color to #33C3F0
  • Draw a rectangle
  • Clear all drawings
  • Use the default draw color

If you put clear at the top (or exclude it entirely) it should work

1 Like

i think i need some more help on this. im mixture of confused and lost :wink: though i think those are 1 and the same.

so i have made a duff project. just to test these things out, and im running into errors regarding player being in a for loop. it spits out saying arrays etc with player and how it cant be done.
ive tried many different ways about this, but am stopping right now to ask for more help :wink:

here is my Main.hx

package;
import openfl.display.Sprite;
class Main extends Sprite {	
	public var player:Player;
	public function new () {
		super ();
		var colors = [ 0xFF0000, 0x00FF00, 0x0000FF ];
		for (i in 0 ... 4) {
			player[i].graphics.beginFill(color[i]);
			player[i].graphics.drawRect(0,0,100,100);
		}
	}
}

and here is my Player.hx. this is the one im obviously trying to read into main.hx so i can change the color of the player[s] in the game.

package;
import openfl.display.Sprite;
class Player extends Sprite {
	public function new () {
		super ();
		this.graphics.clear();
		this.graphics.beginFill(0x00ff00);
		this.graphics.endFill();
	}	
}

thans for this. and please excuse my mass of confusion :wink:

Try this:

package;

import openfl.display.Sprite;

class Main extends Sprite {	
    
    public var players:Array<Player>;
    
    public function new () {
        
        super ();
        
        players = new Array ();
        
        players.push (new Player (0xFF0000);
        players.push (new Player (0x00FF00);
        players.push (new Player (0x0000FF);
        
        for (i in 0...players.length) {
            
            // do more global layout and setup code here
            
            players[i].x = 120 * i;
            addChild (players[i]);
            
        }
        
    }
    
}
package;

import openfl.display.Sprite;

class Player extends Sprite {
    
    public function new (color:Int) {
        
        super ();
        
        // do more per-player graphic and setup code here
        
        graphics.beginFill (color);
        graphics.drawRect (0, 0, 100, 100);
        
    }
    
}
1 Like

wow. im going take it out to dinner and ask it questions. there is much more to know and learn :wink:

thank you so much, it is highly appreciated :wink: