How to draw line with texture instead of color? - cocos2d-x

I have lot of sprites on screen ( cars looking from top ) and I am drawing polylines ( like call ccDrawPoly(redBlue, 8, false) ). I need to replace line color with texture ( I have image of water and I want to draw line with water texture water.png ).
How to make this ?

PaolaJ, I could suggest a hack
Create Sprite Object
Give it some width like 2,4,8 or 16 (depending on your texture)
Sprite Height could be same as line length.
Use sprite->getTexture()->setTexParameters({GL_LINEAR, GL_LINEAR,GL_REPEAT,GL_REPEAT}) to repeat your texture.
use rotation to place the sprite properly.
make sure texture is power of 2.
On the other hand, proper way would be textured polygon as LearnCocos2d suggested.

Related

Is it more efficient to draw a circle with ccCircle?

I'm hoping to create about 50-80 'balls' on the screen and each ball is an object of a class extending CCNode. Each ball will be about 20 x 20px.
Would it be more efficient to
override void draw() and use ccDrawCircle()
or make a picture and make a CCSprite with it (it'll needed be added to the node)?
Most effective way to draw multiple nodes, that use single material (texture) is CCSpriteBatchNode.
// we create base bach node with texture
CCSpriteBatchNode *batch = CCSpriteBatchNode::create(textureFile, 50 /* initial capacity of node */);
// then we create our sprites with the same texture
CCSprite::sprite = CCSprite::create(textureFile);
batch->addChild(sprite);
CCSpriteBatchNode can contain only CCSprites with the same texture, that used for batch node, but all sprites are drawed in single function call (batch drawing).
Color and opacity of every sprite can be changed. To colorize sprites you can create white-colored texture of ball. When you use setColor method, white color will be transformed to color, that specified in setColor

actionscript: creating a soft mask using vector shape

I have an hourglass like vector shape and I'd like to use it to mask an image. I'd like to feather the edges - have a soft falloff in transparency that follows the contours of the hour glass. Any ideas how I can do this?
I tried using a gradient fill on a closed shape (using beginGradientFill() and curveTo() functions) but that falloff doesn't follow the contour of the vector shape, it can only go one direction.
Maybe there is a better solution but until somebody comes up with it... I assume you could do the following:
Draw whatever shape you want to use as mask into a transparent bitmap.
Scale a bit the bitmap down (or use a matrix while drawing its bitmapdata).
Apply a blur filter to it.
Put the bitmap's center to the masked clip's center so they are aligned.
Set the masked clip's cacheAsBitmap property to true.

drawing a line: is there exists a limits of thickness in Graphics.lineStyle()?

I'm developing a simple a graphical editor for my flash-based app. In my editor there's a posibility of scaling, range of scaling is big (maximum scale is 16.0, minimum scale is 0.001 and default scale is 0.2). So it's quite possible that a user can draw a line with thickness 0.1 or 300.0, and it looks that line possible thickness (in Graphics.lineStyle()) has upper border. As I found out from livedocs maximum value is 255. So if thickness is greater then 255.0 there'is drawn a line of thickness 255.0. Whether mentioned upper border exists and how big is it. Here're my questions:
Right now I'm drawing lines with drawPath() or lineTo() methods. Natural walkarround if thickness is greater then 255.0 is to draw a rectange instead of segment and two circles on the ends of segment (instead of lineTo()). Or even to draw two thin segments and two half-circles and fill interior. Maybe there's more elegant/quick solution?
Another question is if the thickness of line is big but less then 255.0 (e.g. 100.0), what is faster drawing a line with lineTo() or drawing two thin segments and two half-circles and fill interior?
And finally, maybe someone knows a good article/book where I can read what's inside all methods of flash.display.Graphics class (or even not flash specific article/book on graphics)?
Any thoughts are appreciated. Thank you in advance!
I agree with f-a that putting the line in a container would probably be better and more efficient than drawing a rectangle and extra circles.
I don't think that the math would be too difficult to work out. For efficiency you should probably only do this if the line style is going to be over 255.
To setup the display object to hold your line I would start by halving the width of your line (the length can stay the same). Then create a new sprite and draw the line in the sprite at half size (e.g. if you wanted 300, just draw it at 150). It would be most simple to just start at (0,0) and draw the segment straight so that all of your transformations can be applied to the new sprite.
From here you can just double the scaleY of the sprite to get the desired line weight. It should keep the same length and the ends should also be rounded correctly.
Hope this helped out!
A cool resource for working with the graphics class is Flash and Math. This site has several cool effects and working examples and source code.
http://www.flashandmath.com/

adding transparent sprite over another sprite

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

Actionscript 3.0 drawRect works weird

I have a BitmapData object named myBitmapData. It was loaded of PNG of size 104x104. This PNG represents a red circle on the transparent background.
There is also a Sprite object named myBackground. I want render that red circle into myBackground.
myBackground.graphics.beginBitmapFill(myBitmapData);
myBackground.graphics.drawRect(0, 0, myBitmapData.width, myBitmapData.height);
myBackground.graphics.endFill();
addChild(myBackground);
Everything is fine. I see a red circle in the left top of myBackground.
But when I change the third line to
myBackground.graphics.drawRect(0, 52, myBitmapData.width, myBitmapData.height);
and expect my circle to be translated 52 pixels down, I actually obtain something strange (for me :)): there are two red half-circles (they form like hourglass).
So, the question is: how do I render myBitmapData into the random position of myBackground?
P.S.
In the case of
myBackground.graphics.drawRect(0, 104, myBitmapData.width, myBitmapData.height);
it is circle again :)
This is caused by beginBitmapFill's default repeat = true parameter. There's an example in the docs. Disabling the repetition won't work though, you'd just get a half circle then.
There are several ways to fix this:
Use a Matrix with a translation (displacement) as argument in beginBitmapFill.
Draw the rectangle at 0,0 on another Sprite, and move that sprite to where you want it on the background.
Don't draw directly to the background, but to another bitmap using copyPixels. Then fill the background with that bitmap.