cocos2d draw something on top of the sprite - cocos2d-x

I am very new to cocos2d. I have read the basic concept of sprite and action. I was wondering after I created a sprite (from an image file). and I want to draw some numbers on the sprite on the fly, is that something doable? or you can not draw anything on the sprite after creating from an image?
Thanks for your reply in advance.

If you want to display text or numbers over a Sprite, you can add a UIText widget as a child.
auto sprite = Sprite::create("image.png");
addChild(sprite);
auto text = Text::create();
text->setString("Test");
// Position the text in the center of the sprite
text->setPosition(Vec2(sprite->getContentSize().width*.5,
sprite->getContentSize().height*.5));
sprite->addChild(text);

draw sprite using Render Texture and then you can add as a child of the sprite .

Related

Showing a mask of MovieClip with AS3

Is it possible to display a mask object using AS3?
I have a MovieClip called myMC, then I mask myMC with the MovieClip called myMask. MOVIE_CLIP and MASK are library MovieClips.
var myMC:MovieClip = new MOVIE_CLIP();
var myMask:MovieClip = new MASK();
myMC.mask = myMask;
Of course, myMC won't show.
What I want is that myMC is only displayed in myMask, and not outside it, with myMask reamining visible.
To be quite honest with you, this isn't possible AFAIK. A mask, by definition, is invisible. What you would need is to have the mask be the SHAPE you want, and then have an additional MovieClip that shows whatever visual elements you would have shown on the mask.
Case and point, if you wanted to use a glass-pane graphic as your mask, you would need to have a glass-pane graphic, the mask with the same dimensions, and then the MovieClip you're masking underneath that mask.
I hope that helps.

cocos2d-x addchild will not display child image at first time

I have a button, when click the button, it will add a sprite with into the scene, but the sprite will not display the image, it is just all black, but I can see the sprite boundary, but after I minimize the window and display window again, it will be ok. what happened about this?
local removeSprite = mainScene:getChildByTag(tag)
mainScene:removeChild(removeSprite)
local replaceSprite = CCSprite:new("hello.png")
replaceSprite:setTag(tag)
mainScene:addChild(replaceSprite)
I use quick-cocos2d-x, but it is the same as the cocos2d-x c++, I first remove the old sprite, and add a new sprite.
If mainScene is a ccscene object , then you should not do this.
ccsprite object is better added to a cclayer object and then that layer added to the scene.
Also, instead of doing this:
local removeSprite = mainScene:getChildByTag(tag);
mainScene:removeChild(removeSprite);
just do this:
mainScene:removeChildByTag(tag);
I don't know quick-cocos2dx but this is what i do in c++

How to add/get Sprite to/from UIComponent

How to add Sprite to BorderContainer (UIComponent)?
var sprite:Sprite = new Sprite();
sprite.graphics.lineStyle(10,0);
sprite.graphics.moveTo(40,40);
sprite.graphics.lineTo(60,60);
mybordercontainer.addChild(sprite);
//mybrodercontainer is id of BorderContainer created in mxml
This code doesnt work. I cant see Sprite on my BorderContainer. How can I add Sprites on UIComponents, so I can see them? I tried this and it kinda worked:
var comp:UIComponent = new UIComponent();
comp.addChild(sprite);
myborderconteiner.addElement(comp);
But I dont think, that this is a right way to add Sprites to UIComponents. Is there another method to do that?
Second problem:
When I have few Sprites added to my UIComponent (lines/circles/images or others) how can I receive an object Sprite from that UIComponent, which is containing all Sprites added before to that UIComponent?
I need to create Bitmap from that Sprite and do some things.
I hope I make myself clear
Use the SpriteVisualElement as container. That should serve nicely as a container or Sprite substitute. You could also draw in the UIComponent itself.

Make bitmap partially invisible AS3

I am trying to make a object (movie clip containing textfields and bitmaps) and I need to make it partially transparent at times (like making half of it disappear). How is it possible?
// only things inside the rect will be visible
DisplayObject.scrollRect = new Rectangle(...);
A MovieClip is also a DisplayObject.

Tweening a Rounded Rectangle in Actionscript 3

I would like to tween between a short rounded rectangle and a tall rounded rectangle. (I only want deal with the height - no other parameters). I am programming with actionscript 3. My tweening engine is TweenLite.
I have been tweening a sprite that contains a rounded rectangle. The tweened sprite produces distortion. I suppose that I have been scaling the original image, rather than the height of the rounded rectangle?
Here is a simple example of my code:
-
Draw the rounded rectangle:
roundRect = new Sprite();
roundRect.graphics.beginFill(0x000000);
roundRect.graphics.drawRoundRect(0,0,50,15,4,4); //Original Height: 15
roundRect.graphics.endFill();
addChild(roundRect);
Then I listen for a mouse click event on the rounded rectangle.
The mouse event triggers a function with the following code:
TweenLite.to(this.roundRect, 1, {height:120}); //Final Height: 120
-
I would like to tween the height of the rounded rectangle itself. I would hope that this would not produce the unwanted distortion. Is there any way to achieve this?
Thank you.
This can be achieved with "9-slice scaling".
Below are two tutorials on how to setup a Movieclip to use the 9-slice guides, one is done through the IDE (using the guidelines) and the other through code (by defining a rectangle called grid and assigning this to the movieclip's scale9Grid property).
http://www.sephiroth.it/tutorials/flashPHP/scale9/
http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00001003.html
Once the scale9Grid property has been correctly assigned you can scale (and Tween) the movieclip as intended without any distortion.
It might also be worth reading: http://www.ovidiudiac.ro/blog/2009/05/scale9grid-work-and-fail/ which describes various scenarios when scale9grid does and doesn't work. (mainly to do with having nested children and non-vector graphics inside of the grid).
Hope this helps.
As an alternative, and since its only a rounded rectangle, you could also do something like this:
var rectHeight = 15;
var roundRect = new Sprite();
addChild(roundRect);
updateRect();
function updateRect() {
roundRect.graphics.clear();
roundRect.graphics.beginFill(0x000000);
roundRect.graphics.drawRoundRect(0,0,50,rectHeight,4,4);
roundRect.graphics.endFill();
}
roundRect.addEventListener("click", click);
function click(e) {
TweenLite.to(this, 1, {rectHeight:120, onUpdate:updateRect});
}