I need to disable transparency in a Sprite or SpriteBatch. I can't find any methods to do that for a Sprite, and when I call .DisableBlending() on a SpriteBatch, and then I call .EnableBlending(), but doesn't get enabled again until it ends.
Code outline:
batch.begin();
// draw non-transparent sprite.
batch.disableBlending();
batch.draw(sprite);
//draw transparent sprites
batch.enableBlending();
batch.draw(sprite2);
batch.end();
So what happens is sprite2 also isn't transparent. Is there any way to "update" a SpriteBatch's blending? Or, if it's impossible, disable transparency on a texture or Sprite?
Related
I am making a rectangle :
rect = new Rectangle();
rect.x = 40;
rect.y = 100;
rect.setSize(100,100);
and then also drawing some texture on the position of rectangle like:
batch.draw(myTexture, rect.x , rect.y, rect.width, rect.height);
Now what i want to do is also see the shape of rectangle like i must see some boundary so that i can see my rectangle boundary as well as my texture. And i need this for debugging purposes as the texture is complex like somewhere the background is transparent so i want to remove the collision from those places and that is why i am thinking if i could see both the rectangles and the sprite it would be great.
Although i tried doing this:
shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
shapeRenderer.setColor(Color.RED);
shapeRenderer.rect(0,0,100,100);
shapeRenderer.end();
I thought the above code would help me draw the texture as well as see the rectangle but turns out i could see only the rectangle's boundary and not the texture.
Here is the output:
In the image , you can see a red rectangle and that is where i was trying to debug my bar (a bar of yellow color was also supposed to be shown here) but my yellow doesn't appear as this red-boundary rectangle overlapped my bar.
To be clear, i want to see the shapeRenderer right above the texture and be able to see the both of them
So i found the answer,
As you know i wanted to appear a rectangle over my texture so that i could see both - my texture and my shapeRenderer shape but instead i could see only my shape and not the texture.
Problem was that What is was doing is i began the batch and inside that i wrote the code for drawing to the batch and also the code for drawing the shapeRenderer to the batch like this:
batch.begin();
batch.draw(texture,0,0,0,0);
shapeRenderer.begin();
shapeRenderer.rect(0,0,0,0);
shapeRenderer.end();
batch.end();
The above code replaces my texture as it can only draw one thing at the coordinates 0,0 so first it draws the texture at 0,0 and then the shaperenderer at 0,0.
The solution was:
To draw the shapeRenderer separately and not within the batch.begin() and batch.end() like:
batch.begin();
batch.draw(texture,0,0,0,0);
batch.end();
shapeRenderer.begin();
shapeRenderer.rect(0,0,0,0);
shapeRenderer.end();
I want to draw a large red circle with a black outline and a smaller concurrent green circle with no outline. But when I draw the small green circle, it has the same lineStyle as the big red circle. There does not seem to be any kind of endLineStyle method. Setting the lineStyle thickness to 0 does not work either. I realize there are a number of other things I could do like draw them as separate shapes, or make the small green circle's lineStyle also green, but I was wondering if there was some way get rid of the lineStyle without doing that.
var s:Shape = new Shape();
s.graphics.lineStyle(4,0x000000);
s.graphics.beginFill(0xff0000);
s.graphics.drawCircle(100,100,80);
s.graphics.endFill();
s.graphics.beginFill(0x00ff00);
s.graphics.drawCircle(100,100,40);
s.graphics.endFill();
addChild(s);
s.graphics.lineStyle();
The first argument is thickness, null by default.
A call with zero thickness - s.graphics.lineStyle(0); or alpha - s.graphics.lineStyle(1, 0, 0); has the same effect
I want to fill no color to canvas i.e. I want the background div color should keep on displaying in the div. I have googled it but didn't find any solution to keep fillStyle as no color.
Also I tried omitting the fillStyle but it leave me with black color.
Any options available?
Thanks
You need to use this to clear the canvas:
context.clearRect(0, 0, canvas.width, canvas.height);
(or the rectangle you need).
Also make sure you have no CSS rules set for the canvas element's background (which might be the case if your canvas is not transparent on init as canvas is transparent as default).
If you need to clear a non-regular shape you can use composite mode:
context.globalCompositeOperation = 'destination-out';
(xor and source-out can also be used too to a certain degree too to remove existing pixels but with some variations).
This will clear the canvas in the form of the next shape (Path) you draw (or use an image which solid pixels will clear the canvas).
I don't know what you're trying to do, but it seems you want to make your canvas transparent. You could do that in JavaScript:
context.fillStyle = "rgba(0, 0, 200, 0)";
Or in CSS, where foo would be the id of the canvas element. Note that this would make the element per se transparent, not just it's contents.
#foo {
opacity: 0;
}
You need to clear your canvas, not draw transparency... just think about that. You can't clean a glass, by using clear paint, the same is applied to the <canvas> element.
http://jsfiddle.net/Z6XTg/1/
this canvas demo does a very simple
ctx.clearRect(0,0,canvas.width,canvas.height);
before it draws anything, thus maintaining the canvas transparency.
I need to place a transparent sprite over another sprite. The overlaying sprite will acept some mouse events. When a user move mouse over upper sprite a curve will be drawn. After it'll be processed it will be drawn on the base sprite (and erased on upper).
The idea I have now is to place the sprite, draw a rectange of size equal to sizes of sprite and set alpha to 0.
The question is a bit dump: maybe the proposed solution is not the best. Is there a better way to set width and height (as far a I understand Sprite.width = w; will not help)?
Thank you in advance!
You can't set dimensions directly, while you can draw over that Sprite. So you can do like this:
graphics.beginFill(0,0); // zero alpha fill
graphics.lineStyle(0,0,0); // invisible lines
graphics.drawRect(0,0,width,height);
graphics.endFill();
This way your Sprite can have its alpha remaining at 1, to not hide anything that's its child. Then, whatever curve you would decide to draw in that Sprite, you can draw within a child Shape object, via graphics.moveTo and graphics.lineTo.
UPDATE: According to comments below, setting alpha to 0 won't work with newer Flash player versions, so alpha should be set to a nonzero amount for the events to register on the overlapping sprite.
graphics.beginFill(0x808080,0.01); // almost zero alpha fill
I'm using TweenMax to make all tweens, fade in a projet. Now my question is : is it possible to tween only linestyle from a Sprite ?
For example, something like that :
TweenMax.to ( my_sprite.graphics.lineStyle, 0.5, { tint : 0xFF0000 } );
Thank you.
From what I have seen, if you wish to alpha the stroke and the fill separately, you need to redraw the shape.
ie. this post - http://www.actionscript.org/forums/showthread.php3?t=219378
Alternatively, depending on the complexity of the Sprite, you can just make the outline its own Sprite and tween it interdependently.