Cocos2d-x: Hide Sprite in 1s - cocos2d-x

I'm newbie in Cocos2d-x.
I'm developing a simple game.
I want to move a sprite from the right to the left.
During it moving, i want that sprite hide in a distance in 1s.
Sequence: 1s:visible,1s:invisible,1s:visible
Example: it run from position A to D
Between A and D we have B and C => ( A->B->C->D)
When sprite in A->B it visible, then B->C it'll be hide, then C->D it visible again.
How can i do it?
Thanks for all your helps.

For show/hide with delay you can use this code (I've wrote it right here, so it may not be compiled after simply copy and paste to your project =) )
float delay = 1f;
CCAction* hideAction = CCHide::create();
CCAction* showAction = CCShow::create();
CCActionInterval* showHideAction = CCSequence::create( CCDelayTime::create(delay),
hideAction,
CCDelayTime::create(delay),
showAction);
CCAction* foreverAction = CCRepeatForever::create(showHideAction);
yourNode->runAction(foreverAction);
To move your node(sprite in your case) you can use both CCMoveTo and CCMoveBy action.
For example
float moveDuration = 5f;
CCPoint targetPos = CCPointMake(someX, someY);
CCAction* moveAction = CCMoveTo::create(moveDuration, targetPos);
yourNode->runAction(moveAction);

Try this action
CCHide * hideAction = CCHide::create();

You can use CCSpawn, this can run two actions at the same time.
You can try this code:
CCAction* action = CCSpawn::createWithTwoActions(CCMoveTo::create(1,CCPointMake(x,y)),
CCFadeOut::create(1));
you_sprite->runAction(action);
update:
You can use CCRepeatForever to run fadein/fadeout, and after move action done, stop this forever action.
Here is the code:
CCSequence* move = CCSequence::create(CCMoveTo::create(3, CCPointMake(1, 1)),
CCCallFunc::create(this, callfunc_selector(SomeClass::some_func)),
NULL);
CCRepeatForever* forever = CCRepeatForever::create(CCSequence::create(CCFadeIn::create(1),
CCFadeOut::create(1)
NULL));
your_sprite->runAction(move);
your_sprite->runAction(forever);
Here is the callback function (invoked after move action);
void SomeClass:some_func(){
your_sprite->stopAllActions();
}

Sprite->runAction(Sequence::create(MoveTo::create(1.0f, Vec2(200,200)),Hide::create(),MoveTo::create(1.0f, Vec2(200,400)),Show::create(),NULL));

SpriteName->runAction(Sequence::create(Hide::create(),NULL));
Here only Sprite Hide.

Related

Particle Effect Changing Color at runtime

I need to change color of my particle effect color according to some user event in my game for that this is what i am doing:
float temp[] = new float[4];
temp[0] = 0.937f;
temp[1] = 0.325f;
temp[2] = 0.314f;
pe.getEmitters().first().getTint().setColors(temp);
pe.start();
and in render i am doing this:
pe.draw(batch, Gdx.graphics.getDeltaTime());
but unfortunately i am getting this error:
java.lang.ArrayIndexOutOfBoundsException: length=4; index=4
at com.badlogic.gdx.graphics.g2d.ParticleEmitter$GradientColorValue.getColor(ParticleEmitter.java:1313)
at com.badlogic.gdx.graphics.g2d.ParticleEmitter.activateParticle(ParticleEmitter.java:439)
at com.badlogic.gdx.graphics.g2d.ParticleEmitter.addParticle(ParticleEmitter.java:154)
at com.badlogic.gdx.graphics.g2d.ParticleEmitter.draw(ParticleEmitter.java:299)
at com.badlogic.gdx.graphics.g2d.ParticleEffect.draw(ParticleEffect.java:74)
at com.approduction.game.GameScreen.render(GameScreen.java:218)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:459)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1557)
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1263)
i dont know what i am doing wrong, i read the documentation and has done everything according to it any help would be a savior... Thanks in advance..
Your float array has the wrong length.
You actually don't need to create a new array. You can avoid this problem altogether by filling your colors into the array it already has like this:
float temp[] = pe.getEmitters().first().getTint().getColors();
temp[0] = 0.937f;
temp[1] = 0.325f;
temp[2] = 0.314f;
pe.start();

how to connect a number variable to dynamic text in actionscript 3.0?

i know this might be simple but i have been searching everywhere for a fix but i just cannot find it!
i want to make something like a health #, so when you press whatever button the dynamic text # will go up or down. on my test project i have two layers, the first with the following code
var hp:Number = 100;
health.text = String hp;
hp being the variable, and health being the dynamic text. then i have the next layer with the button with:
function button(e:MouseEvent):void
{
hp -= 10;
}
without that second chunk of code, the dynamic text will appear, but once that is added it will disappear and the button is function-less.
how do i make this work??? once again sorry if this is a dumb question, i'm just very stumped.
The accepted answer is good, but I wanted to point out that your original code was actually very close to being correct, you just needed parenthesis:
health.text = String(hp);
For most objects String(object) and object.toString() has the same effect, except that object.toString() throws an error if object is null (which could be desirable or undesirable, depending on what you expect it to do).
This is not correct:
health.text = String hp;
use:
health.text = hp.toString();
and:
function button(e:MouseEvent):void
{
hp -= 10;
health.text = hp.toString();
}

cocos2d-x Why i can't set sprite hierarchy when using SpriteBatchNode?

Hey i try to set sprite hierarchy to parent root , taking the example from SpriteTest.cpp
from SpriteSkewNegativeScaleChildren example.
but in my code i allso add the sprites to SpriteBatchNode .
like this :
auto cache = SpriteFrameCache::getInstance();
cache->addSpriteFramesWithFile("leds/sprites.plist", "leds/sprites.png");
auto batch = SpriteBatchNode::create("leds/sprites.png");
Sprite* Sprite_all_side_connector = Sprite::createWithSpriteFrameName("all_side_connector.png");
batch->addChild(Sprite_all_side_connector);
Sprite* Sprite_one_side_connector = Sprite::createWithSpriteFrameName("one_side_connector.png");
batch->addChild(Sprite_one_side_connector);
Sprite* Sprite_purple_stick = Sprite::createWithSpriteFrameName("purple_stick.png");
batch->addChild(Sprite_purple_stick);
Sprite* Sprite_red_stick = Sprite::createWithSpriteFrameName("red_stick.png");
batch->addChild(Sprite_red_stick);
Sprite* Sprite_yellow_ball = Sprite::createWithSpriteFrameName("yellow_ball.png");
batch->addChild(Sprite_yellow_ball);
addChild(batch, 0, TAGS::SPRITEBATCHNODE);
Sprite_all_side_connector->setPosition(ccp(winSize.width/2,winSize.height/2));
auto parent = Node::create();
addChild(parent);
parent->addChild(Sprite_all_side_connector);
but im getting exception , when i remove the SpriteBatchNode every thing working fine .
i want to use the SpriteBatchNode feature for OpenGL one draw call .
the exception is in :
parent->addChild(Sprite_all_side_connector);
You will need to create another instance of Sprite_all_side_connector and add that to the scene. Sprites can only be added once. Change your code to:
parent->addChild(Sprite::createWithSpriteFrameName("all_side_connector.png"));
That should clean up the assertion.

Use of particle effect

I am using particle effect my game using libgdx. But effect is showing for small time and after that it disappear.But I want to show my effect for long time or in my control.
My code is given below in my game play screen class...
ParticleEffectPool waterEffectPool;
Array<PooledEffect> effects = new Array<PooledEffect>();
ParticleEffect waterEffect;
...
...
waterEffect = new ParticleEffect();
waterEffect.load(Gdx.files.internal("data/runonwater"), Gdx.files.internal("data"));
waterEffectPool = new ParticleEffectPool(waterEffect, 1, 5);
//for(int i = 0; i <= waterEffectPool.max; i++){
PooledEffect effect = waterEffectPool.obtain();
effect.setPosition(150, 130);
effects.add(effect);
and in render method I use it to render
for(int i = effects.size - 1; i >= 0; i--){
PooledEffect effect = effects.get(i);
effect.draw(spriteBatch, deltaTime);
if(effect.isComplete()){
effect.free();
effects.removeIndex(i);
}
}
I have already answered the question in the comments section above but still writing it here so as it can be accepted (as suggested by P.T.)
If you are using particle editor then there is an option weather to set this effect continuous or not. Set continuous as true and problem will be solved .
#P.T. yups you r right. Will remember it from now on :)

erasing a layer where mouse is over it

I got the following question.
i added the following elements to the stage:
homeBg = new HomeBg();
homeMask = new HomeDrawBg();
addChild(homeBg);
addChild(homeMask);
I allready instantiated them in the beginning of the document. But my problem is the following. the homeBg layer is a image, the homeMask layer is the same image but it has a pencil scetch look. What i want is that wherever i move my mouse, the homemask layer should be erased so the bottom layer becomes visible(only where the mask is erased). So how can i tell the mask layer to erase itself if the mouse is over it?
Answer attempt 2
You can use the blendMode property of a display object to achieve this. Here's the code (tested):
// set the eraser width (diameter)
var eraserWidth:int = 20;
//get the HomeMask library item
var homeMask:HomeMask = new HomeDrawBg();
homeMask.blendMode = BlendMode.LAYER;
addChild(homeMask);
// create the eraser shape
var eraser:Shape = new Shape();
eraser.graphics.beginFill(0x000000);
eraser.graphics.drawCircle(0,0,eraserWidth/2);
eraser.blendMode = BlendMode.ERASE;
homeMask.addChild(eraser);
homeMask.addEventListener(MouseEvent.MOUSE_MOVE,mouseOverMask);
function mouseOverMask(evt:MouseEvent):void
{
eraser.x = homeMask.mouseX;
eraser.y = homeMask.mouseY;
}
There are a couple of important bits here.
Firstly, you must set the blendMode of the thing you want to erase to BlendMode.LAYER.
Secondly (and this is what has tricked me before) your eraser, with BlendMode.ERASE, must be a child of the object you're wanting to erase.
Have a look on the following:http://www.piterwilson.com/personal/2008/05/07/bitmapdata-erasing-in-as3-with-custom-brush-shape/