I want add Label on CCLayerColor, but I did not get...
// on "init" you need to initialize your instance
bool SplashScreen::init()
{
//////////////////////////////
// 1. super init first
if ( !CCLayerColor::initWithColor(Colors::GetMainAccentColor4B()) )
{
return false;
}
auto visibleSize = Director::getInstance()->getVisibleSize();
auto origin = Director::getInstance()->getVisibleOrigin();
auto label1 = CCLabelTTF::create("Hello World", "Helvetica", 12,
CCSizeMake(245, 32), kCCTextAlignmentCenter);
this->addChild(label1, 1);
return true;
}
Colors::GetMainAccentColor4B() - works fine - it's my class, that give me colors
Screenshot
Try with this
auto label = LabelTTF::create("Hello World", "Helvetica", 100, Size(245, 32), TextHAlignment::CENTER);
label->setPosition(Point(visibleSize.width * 0.5, visibleSize.height * 0.5));
this->addChild(label);
It should draws a label on center's screen. Don't use deprecated method or class like CCLabelTTF in cocos2d-x 3.0.
Related
I just want to create a gradient mask –which is generated dynamically through AS3 code– for an object (a pentagon in my following example,) and I simply, cannot! [The SWF file of what I've tried.]
The code works just fine unless it ignores the alpha gradient of the dynamically created Sprite for being used as a gradient mask (it's being treated as a solid mask), while the exact code acknowledges the "on-stage" created object (through the user interface) for being a gradient mask!
I think the runtime just cannot cache the object as bitmap and hence the ignorance! However, I'm stuck at making that happen! So please, shed some lights on this, any help is greatly appreciated in advance :)
var X:Number = 100, Y:Number = 35, W:Number = 350, H:Number = 150;
var mat:Matrix = new Matrix();
mat.createGradientBox(W, H, 0, X, Y);
var gradientMask:Sprite = new Sprite();
gradientMask.graphics.beginGradientFill(GradientType.LINEAR, [0, 0], [1, 0], [0, 255], mat);
gradientMask.graphics.drawRect(X, Y, W, H);
gradientMask.graphics.endFill();
pentagon1.cacheAsBitmap = true;
pentagon2.cacheAsBitmap = true;
onStageGradient.cacheAsBitmap = true;
gradientMask.cacheAsBitmap = true;
pentagon1.mask = gradientMask;
pentagon2.mask = onStageGradient;
stage.addEventListener(Event.ENTER_FRAME, _onEnterFrame);
function _onEnterFrame(e:Event):void {
pentagon1.x += 7;
pentagon2.x += 7;
if (pentagon1.x > 500) {
pentagon1.x = 0;
pentagon2.x = 0;
}
}
You have to add the gradientMask to the display list to have it in effect.
pentagon1.parent.addChild(gradientMask);
I want to collide 2 sprites using Physics Engine.
But my contact listener is not responding. and no warning/error on log or runtime.
So here is what i did :
Declaration
1)cocos2d::PhysicsWorld* m_world;
2)void setPhyWorld(cocos2d::PhysicsWorld* world){m_world = world;}
3)bool onContactBegin(cocos2d::PhysicsContact& contact);
Implementation
For PhysicsWorld:
Scene ->
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::createWithPhysics();
scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
auto layer = HelloWorld::create();
layer->setPhyWorld(scene->getPhysicsWorld());
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
PhysicsWorld->
void HelloWorld::createPhysicsWorld(){
auto body = PhysicsBody::createEdgeBox(visibleSize, PHYSICSBODY_MATERIAL_DEFAULT, 3);
auto edgeNode = Node::create();
edgeNode->setPosition(Point(visibleSize.width/2,visibleSize.height/2));
edgeNode->setPhysicsBody(body);
this->addChild(edgeNode);
}
Creating Sprite->
void HelloWorld::createCar(){
car = Sprite::create("car.png");
auto body3 = PhysicsBody::createBox(car->getContentSize());
body3->setDynamic(false);
body3->setCategoryBitmask(2);
body3->setCollisionBitmask(2);
body3->setContactTestBitmask(true);
car->setPhysicsBody(body3);
// position the sprite on the center of the screen
car->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
// add the sprite as a child to this layer
this->addChild(car, 1);
car->setAnchorPoint(Vec2(0.5, 0));
car->setFlippedX(true);
}
void HelloWorld::createRocket(){
rocket = Sprite::create("rocket.png");
auto body2 = PhysicsBody::createBox(rocket->getContentSize());
body2->setDynamic(false);
body2->setCategoryBitmask(1);
body2->setCollisionBitmask(1);
body2->setContactTestBitmask(true);
rocket->setPhysicsBody(body2);
// position the sprite on the center of the screen
rocket->setPosition(Vec2(visibleSize.width/2 + origin.x,origin.y));
// add the sprite as a child to this layer
this->addChild(rocket, 1);
}
Collision->
auto contactListener = EventListenerPhysicsContact::create();
contactListener->onContactBegin = CC_CALLBACK_1(HelloWorld::onContactBegin, this);
this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener,this);
ContactListner->
bool HelloWorld::onContactBegin(cocos2d::PhysicsContact& contact)
{
CCLOG("onContactBegin -------> ");
return true;
}
Well it is tricky thing that you miss in your code.one body should be setDynamic to true.
body->setDynamic(true);
cheers!
Well, it turned out that it was bit ugly mistake.
body3->setContactTestBitmask(true);
this method's argument is a bitmask not 'true'/'false' field, so your 'true' mask was treated as it was 1 while your bodies mask is 2. The solution is:
body3->setCategoryBitmask(1);
body3->setCollisionBitmask(3);
body3->setContactTestBitmask(1);
body2->setCategoryBitmask(3);
body2->setCollisionBitmask(1);
body2->setContactTestBitmask(1);
I Just Comment out body2->setDynamic(false); inside createRocket Method and setGravityEnable(false); and it works now.
void HelloWorld::createRocket(){
rocket = Sprite::create("rocket.png");
rocket->setPosition(Vec2(visibleSize.width/2 + origin.x,origin.y - 50));
rocket->setTag(3);
auto body2 = PhysicsBody::createBox(rocket->getContentSize());
//body2->setDynamic(false);
body2->setTag(3);
body2->setGravityEnable(false);
body2->setCollisionBitmask(3);
body2->setContactTestBitmask(true);
rocket->setPhysicsBody(body2);
this->addChild(rocket, 1);
}
I wanted to mask some nodes of my DisplayObject tree. I couldn't make masking work in my big project. So I build a simple example for me and I saw it works actually pretty good. But I can't figure out why it doesn't work in my big project. All my visible objects are Sprites or from classes that extend Sprite.
masking in big project doesn't work
I can see the normal state of my nodeToBeMasked
when I add the mask to this node I can see the mask
but when I set the mask to be the mask, I continue to see everything (pure nodeToBeMasked is masked but not the children - which would be much more important)
masking in simple example works fine
How can masking stop working ?
Code: (that doesn't work, big project)
// custom class extends Sprite
override protected function onAddToStage(event:Event):void
{
trace(stage); // stage exists
var maskSprite:Sprite = new Sprite();
maskSprite.graphics.beginFill(0xffff00, 1);
maskSprite.graphics.drawCircle(0, 0, 64);
maskSprite.graphics.endFill();
maskSprite.x = 64;
maskSprite.y = 64;
if (true)
{
this.addChild(maskSprite); // doesn't help
this.mask = maskSprite; // I can see EVERYTHING here, inside and outside the cirle
}
else
addChild(maskSprite); // I can see the mask here
}
Code: (that works)
[SWF(frameRate="60",backgroundColor="0xffffff",width="128",height="128")]
public class MaskTest extends Sprite
{
public function MaskTest()
{
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(event: Event): void
{
trace(stage);
// this
graphics.beginFill(0x00ff00, 1);
graphics.drawRect(8, 8, 112, 112);
graphics.endFill();
// extra childs <- ^^
for (var i: int = 0; i < 100; i++)
{
var child: Sprite = new Sprite();
child.graphics.beginFill(uint(Math.random() * 0x1000000), 1);
child.graphics.drawRect(Math.random() * 64, Math.random() * 64, 64, 64);
child.graphics.endFill();
addChild(child);
}
// mask
var maskSprite: Sprite = new Sprite();
maskSprite.graphics.beginFill(0xffff00, 1);
maskSprite.graphics.drawCircle(0, 0, 64);
maskSprite.graphics.endFill();
maskSprite.x = 64;
maskSprite.y = 64;
// switch to check the mask
if (true)
this.mask = maskSprite;
else
addChild(maskSprite);
}
}
Update:
I found out that my custom class (that is the root and is only responsible for Event.ENTER_FRAME updates) was causing the problem. I don't know why but by disabling the z update in all my project solved the children of my maskedNode not being masked.
The mask MUST be added to the parent to work, not only used as
obj.mask = myMask; //This will not work alone
To make it work, it must be added to the parent object display list
obj.addChild(myMask);
obj.mask = myMask;
//this wasthe problem in my big project
maskSprite.z = 0; // avoid this with masks
Just a wild guess from me:if you use the properties z, rotationX, rotationY, rotationZ (maybe some more) the sprite is shifted to the 3D space and the masking is only working in 2D.
I have experimented with Flash 3D a bit. The transition from 2D to 3D seemed very smooth. You can't see when they "turn".
Trying to tween the alpha of a graphics object in AS3 using GreenSock, but the functions are not working. Trying to tween to from alpha 0 to 0.7 in 2 seconds. The fromTo(); method doesn't work either. I don't want to, but would I have to instead, use an incremental for loop to do this?—As this wouldn't give me control over the time of the tween.
public function overlayBox():void {
var overlaySquare:Sprite = new Sprite();
overlaySquare.graphics.beginFill(0x00000);
overlaySquare.graphics.drawRect(0, 0, displayRes, displayRes);
overlaySquare.graphics.endFill();
overlaySquare.x = xScreenPos;
overlaySquare.y = yScreenPos;
TweenMax.from(overlaySquare, 2, {autoAlpha:0});
TweenMax.to(overlaySquare, 2, {autoAlpha:0.7});
addChild(overlaySquare);
trace("overlaySquare index: " + getChildIndex(overlaySquare));
}
EDIT: I've fixed the fade from alpha 0 to 0.7 by replacing the TweenMax functions from above to this:
overlaySquare.alpha = 0;
TweenMax.to(overlaySquare, 5, {alpha:0.7});
However, there's a problem with the alpha tween when it's run with the rest of the program. The tween "flashes" and instantly becomes 0.7 (it looks like it's "jumping" from 0 to 0.7) as soon as you can see it. The problem has been isolated to the function which is being called after overlayBox(); An overview of the program: an image is loaded in using loader. Inside loader, there is a myTimer.start();. This is used to run the rest of the program once the image has been loaded. The overlayBox(); is the first method that follows and runs fine. The next method, textAnimation();, is what breaks it, and I have no idea why:
public function textAnimation():void {
//set text format
textFormat.font = "Helvetica Neue Light";
textFormat.size = 28;
textFormat.bold = false;
textFormat.color = 0xFFFFFF;
//textFormat.letterSpacing = 5;
//set text size
var size18bold:TextFormat = new TextFormat();
size18bold.size = 36;
size18bold.bold = true;
// pass text format
textOne.defaultTextFormat = textFormat;
textTwo.defaultTextFormat = textFormat;
var xScreenPosStart:Number = xScreenPos + 440;
var xScreenPosEnd:Number = xScreenPos - 300;
textOne.text = "Blah blah blah";
textOne.autoSize = TextFieldAutoSize.LEFT;
textOne.x = xScreenPosStart;
textOne.y = yScreenPos + 240;
TweenMax.to(textOne, 14, {x:xScreenPosEnd, ease:SlowMo.ease.config(1, 0), repeat:-1});
textTwo.text = "Blah blah blah";
textTwo.autoSize = TextFieldAutoSize.LEFT;
textTwo.x = xScreenPosStart;
textTwo.y = yScreenPos + 140;
TweenMax.to(textTwo, 12, {x:xScreenPosEnd, ease:SlowMo.ease.config(1, 0), repeat:-1, delay:4});
//add to stage
addChild(textOne);
trace("textOne index: " + getChildIndex(textOne));
addChild(textTwo);
trace("textTwo index: " + getChildIndex(textTwo));
textOne.setTextFormat(size18bold);
}
Instead of using TweenMax.from, set the alpha manually.
overlaySquare.alpha = 0;
TweenMax.to( overlaySquare, 2, { alpha : .7 } );
Give an initial value for alpha And use tweenMax.To only
Have you activated the auto alpha plugin? If not, then do it by
import com.greensock.TweenLite;
import com.greensock.plugins.TweenPlugin;
import com.greensock.plugins.AutoAlphaPlugin;
//activation is permanent in the SWF, so this line only needs to be run once.
TweenPlugin.activate([AutoAlphaPlugin]);
If you don't need auto alpha then just use alpha Property
I tried this:
private function _Show() : void {
var overlaySquare:Sprite = new Sprite();
overlaySquare.graphics.beginFill(0x00000);
overlaySquare.graphics.drawRect(0, 0, 200, 200);
overlaySquare.graphics.endFill();
overlaySquare.x = 100;
overlaySquare.y = 100;
TweenMax.to(overlaySquare, 2, {alpha:0.7});
addChild(overlaySquare);
trace("overlaySquare index: " + getChildIndex(overlaySquare));
}
This works.
I have this code
public function TalentBox(x:int, y:int, arg_color:int = 0xFFFFFF):void
{
this.graphics.beginFill(arg_color);
this.graphics.lineStyle(1.0, 0x000000, 0.7);
this.graphics.drawRect(0, 0, 7, 13);
this.alpha = 1.0;
this.x = x;
this.y = y;
this.graphics.endFill();
}
Where I construct the class (that extends from sprite). Then I need to have a function that changes the color of the sprite. Currently I have this
public function setColor(arg_color:int):void
{
color = arg_color;
this.graphics.beginFill(color);
this.graphics.drawRect(0, 0, 7, 13);
this.graphics.endFill();
}
And It seems to work but this is creating a new rect. Which I do not want.
And I have tried ColorTransform, and that changes everything, even the border, which is not what I wanted. And I am not able to colortransform and then set the border color.
So how can I change the color of a sprite without changing the border color?
I have found the answer.
You create two sprites in the class. The body and the border. Set those individually and then change the color with transform only on the body sprite.
Here is the modified constructor
public function TalentBox(x:int, y:int, arg_color:int = 0xFFFFFF):void
{
body.graphics.beginFill(arg_color);
body.graphics.drawRect(x + 1, y + 1, 6, 12);
body.graphics.endFill();
border.graphics.beginFill(0xFFFFFF);
border.graphics.lineStyle(1.0, 0x000000, 0.7);
border.graphics.drawRect(x, y, 7, 13);
border.graphics.endFill();
this.addChild(border);
this.addChild(body);
}