Change sprite color.

1,358 views
Skip to first unread message

Gage Herrmann

unread,
Nov 2, 2012, 5:36:25 PM11/2/12
to haxe...@googlegroups.com
I am using nme to make sprites for a game. I used Sprite.graphics.beginFill(color, alpha) to fill a rect. That works fine. I'm having trouble changing the color property though. What color and alpha properties is beginFill changing? If I change the sprite's color transform properties, the color remains the same. How do I change the color of a sprite?

Also, Actuate.transform will not work for me since my target is HTML5 and it is not working on that target.

j...@justinfront.net

unread,
Nov 2, 2012, 6:13:01 PM11/2/12
to haxe...@googlegroups.com
You can graphics.clear(); and redraw the rectangle this is probably the best approach, obviously in haxe javascript it's different although jeash is hiding the details, but it calls a clearRect....
with javascript canvas you can get the CanvasRenderingContext2D from the canvas using 
var surface = untyped canvas.getContext('2d');
and then
surface.clearRect( 0, 0, 100, 100 ); // where x, y, width, hight of area

If you were targeting flash/c++ then 
I would suggest using filters.
Or you can use ColorTransform
They probably work fine also for jeash but for pure haxe html5 you can use CSS
Or you can adjust pixels directly as I explain in this recent tutorial.
For javascript I find feffects works well, actuate should work for Jeash?


On 2 Nov 2012, at 21:36, Gage Herrmann wrote:

I am using nme to make sprites for a game. I used Sprite.graphics.beginFill(color, alpha) to fill a rect. That works fine. I'm having trouble changing the color property though. What color and alpha properties is beginFill changing? If I change the sprite's color transform properties, the color remains the same. How do I change the color of a sprite?

Also, Actuate.transform will not work for me since my target is HTML5 and it is not working on that target.

Greg Dove

unread,
Nov 2, 2012, 6:39:05 PM11/2/12
to haxe...@googlegroups.com
I don't know if nme works exactly the same as flash here, but in flash you can't modify the existing ColorTransform instance, you need to assign a new instance (the instance that you get from the getter is a cloned copy of the internal instance I think)

So - at least in flash - you can do something like:
var c:ColorTransform=mySprite.transform.colorTransform;
c.alphaMultiplier=.1;
s.transform.colorTransform=c;

Are you trying the same thing in nme?Like I said I don't know if it works 100% the same.


Greg Dove
Dove Software Development Ltd
http://greg-dove.com



On Sat, Nov 3, 2012 at 10:36 AM, Gage Herrmann <gage.h...@gmail.com> wrote:
I am using nme to make sprites for a game. I used Sprite.graphics.beginFill(color, alpha) to fill a rect. That works fine. I'm having trouble changing the color property though. What color and alpha properties is beginFill changing? If I change the sprite's color transform properties, the color remains the same. How do I change the color of a sprite?

Also, Actuate.transform will not work for me since my target is HTML5 and it is not working on that target.

--

Jan_Flanders

unread,
Nov 2, 2012, 7:13:28 PM11/2/12
to haxe...@googlegroups.com


On Friday, November 2, 2012 10:36:25 PM UTC+1, Gage Herrmann wrote:
 How do I change the color of a sprite?
 

       
var s = new Sprite();
        s
.graphics.beginFill(0xFF0000);
        s
.graphics.drawCircle(50,50,50);
        addChild
(s);
       
       
var colorTransform = new ColorTransform();
        colorTransform
.color = 0x0000FF;
       
        s
.transform.colorTransform = colorTransform;

I tried with NME and it works for flash, neko and cpp but not for jeash/html5 (The color does not change)

Jan

Niel Drummond

unread,
Nov 2, 2012, 7:35:00 PM11/2/12
to haxe...@googlegroups.com
I don't think colortransforms were ever implemented on jeash, finding a performant and cross-browser solution for that is not so easy.. you can change a bitmapData color channel though

- Niel

--

j...@justinfront.net

unread,
Nov 2, 2012, 7:55:04 PM11/2/12
to haxe...@googlegroups.com

var canvas = s.graphics.jeashSurface;
var surface = untyped canvas.getContext('2d');
var imageData = surface.getImageData( 0, 0, canvas.width,
canvas.height );
var data = imageData.data;
for( i in 0...(canvas.width*canvas.height) )
{
data[ i*4 + 0 ] = 0;
data[ i*4 + 1 ] = 255; // change to green
data[ i*4 + 2 ] = 0;
// don't adjust alpha channel.
}
surface.putImageData( imageData, 0, 0 );

Jan_Flanders

unread,
Nov 2, 2012, 7:55:10 PM11/2/12
to haxe...@googlegroups.com, niel.d...@grumpytoad.org
Hey Niel,

While trying ColorTransform for Jeash/html5 I noticed that Jeash does not compile with haxe-svn

Simple example:

package be.haxer.test;
class Main extends flash.display.Sprite
{
   
public static function main()
   
{
   
}
}
C:\Documents and Settings\Administrator\Desktop\test>haxelib run nme build project.nmml html5

C:\Program Files\Motion-Twin\haxe\lib/nme/3,4,3/jeash/display/Stage.hx:259: characters 2-53 : jeash.Event should be jeash.events.Event
C:\Program Files\Motion-Twin\haxe\lib/nme/3,4,3/jeash/display/Stage.hx:548: characters 27-48 : jeash.events.Event should be jeash.Event
C:\Program Files\Motion-Twin\haxe\lib/nme/3,4,3/jeash/display/Stage.hx:548: characters 27-48 : For function argument 'evt'
C:\Program Files\Motion-Twin\haxe\lib/nme/3,4,3/jeash/net/URLLoader.hx:221: characters 9-38 : Missing return Dynamic

Could you check/fix it?

From IRC:
[23:57]filt3rek|2Jan_Flanders: it's because of last imports changes
[23:58]filt3rek|2there is js.Event and js.events.Event or something like that and it take the wrong one because of shadowing ...


Cheers,

Jan

Michel Romecki

unread,
Nov 3, 2012, 6:18:07 AM11/3/12
to haxe...@googlegroups.com
Hi all,
You can still use colorTransform on the bitmap object in HTML5 and it works fine.

2012/11/3 Jan_Flanders <adn...@gmail.com>


Jan

Gage Herrmann

unread,
Nov 3, 2012, 8:07:02 AM11/3/12
to haxe...@googlegroups.com
I ended up drawing a new rectangle on top of the old one without clearing it first. That way I can fade in the new color by tweening the alpha channel of the new box. The box doesn't stay on the screen for more than a few seconds, should I still be worried about memory with there now being two of each of these boxes? There will only be three on screen at once. My guess is that it's probably not that big of an issue.

Thanks.
Reply all
Reply to author
Forward
0 new messages