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

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.

Related

Find the co-ordinates of matching image using sikuli in java

I need to get the co-ordinates of matched image within the actualImage so that I can perform operations on it. However, I tried below two approaches,but both doesn't seem to work:
Approach 1:
Using below, I'm able to find a match but co-ordinates returned are just the width & height of image to be matched(which I already know). I want to get the position of the same within actual image.
BufferedImage actualImg = ImageIO.read(new File("C:/Images/SrcImg.PNG"));
ImageTarget actualTgt = new ImageTarget(actualImg);
BufferedImage searchImg = ImageIO.read(new File("C:/Images/TgtImg.PNG"));
ImageTarget searchTgt = new ImageTarget(searchImg);
ScreenRegion scrReg = new StaticImageScreenRegion(actualTgt.getImage());
ScreenRegion resReg = scrReg.find(searchTgt);
ScreenLocation center = resReg.getCenter();
System.out.println(":getElementFromImage: x_loc,y_loc =["+center.getX()+","+center.getY()+"]");
Approach 2:
In below code I tried with sikulix Finder. However, with this src.hasNext() returned true BUT src.next() threw nullpointer exception.Not sure what is the problem here:
Finder src = new Finder("C:/Images/SrcImg.PNG");
Pattern pat = new Pattern("C:/Images/TgtImg.PNG").similar(0.5);
src.find(pat);
Match m;
while( src.hasNext())
m = src.next();
src.destroy();
java.lang.NullPointerException
at org.sikuli.script.Finder.next(Finder.java:484)
at com.work.ImageFinder.main(ImageFinder.java:38)
I already spent good amount of time to make this work. Any help would be much appreciated.
Thanks!
It works fine after passing the Region to Finder like below:
Finder src = new Finder("C:/Images/SrcImg.PNG", new Region(0,0,<width>,<height>))
Pattern pat = new Pattern("C:/Images/TgtImg.PNG").similar(0.5);
src.find(pat);
Match m;
while( src.hasNext())
m = src.next();
src.destroy();
More details can be found below link:
Is it possible to use Sikuli to assert that images are the same in GUI-less mode?

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();

Masked images not displaying in AS3

I am trying to mask an image on another so that I only view the specific portion of the unmasked image through the masked one. My problem is that I cannot see anything on the screen.. no Image, no effect at all
cardMask = new Image(Root.assets.getTexture("card_mask"));
cardMask.y = Constants.STAGE_HEIGHT*0.40;
cardMask.x = Constants.STAGE_WIDTH *0.48;
trace("it's add mask");
cardLight = new Image(Root.assets.getTexture("card_light_mask"));
cardLight.y = Constants.STAGE_HEIGHT*0.46;
cardLight.x = Constants.STAGE_WIDTH *0.48;
cardLight.mask=cardMask;
maskedDisplayObject = new PixelMaskDisplayObject(-1,false);
maskedDisplayObject.addChild(cardLight);
maskedDisplayObject.x=cardLight.x;
maskedDisplayObject.y=cardLight.y;
maskedDisplayObject.mask=cardMask;
maskedDisplayObject.blendMode = BlendMode.SCREEN;
addChild(maskedDisplayObject);
First, for masking an object the mask object should also be added to the display list. Your code does not add cardMask to display list anywhere. Second, if your maskedDisplayObject should be visible at all times, the mask should be assigned not to it, but to some other object which displayed part you desire to control. And third, it is also possible that this.stage is null, therefore the entire tree (this -> maskedDisplayObject -> cardLight) is plain not rendered. You need to check all three of these conditions to get something displayed.
Also, if you desire cardLight as an object to move independently of maskedDisplayObject, you should add it to this instead, and check that it's displayed on top of maskedDisplayObject (call addChild(cardLight) after addChild(maskedDisplayObject)).
This all totals to this code:
trace("Stage is null:", (this.stage==null)); // if this outputs true, you're out of display
cardMask = new Image(Root.assets.getTexture("card_mask"));
cardMask.y = Constants.STAGE_HEIGHT*0.40;
cardMask.x = Constants.STAGE_WIDTH *0.48; // mask creation unaltered
trace("it's add mask");
cardLight = new Image(Root.assets.getTexture("card_light_mask"));
cardLight.y = Constants.STAGE_HEIGHT*0.46;
cardLight.x = Constants.STAGE_WIDTH *0.48;
cardLight.mask=cardMask; // this is right
maskedDisplayObject = new PixelMaskDisplayObject(-1,false);
// maskedDisplayObject.addChild(cardLight); this is moved to main part of display list
maskedDisplayObject.x=cardLight.x;
maskedDisplayObject.y=cardLight.y;
// maskedDisplayObject.mask=cardMask; NO masking of this, you're only masking cardLight
cardLight.blendMode = BlendMode.SCREEN; // display mode is also changed
addChild(maskedDisplayObject);
addChild(cardLight);

Cocos2d-x: Hide Sprite in 1s

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.

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/