cocos2dX setBlendFunc - cocos2d-iphone

My image looks like there is too much black. I use the function setBlendFunc.
But When the sprite does not run Animate, it will work. If run, it does not work. How can this be fixed?
CCSprite *effectSprite=CCSprite::create("init_black.png");
effectSprite->setBlendFunc((ccBlendFunc) {GL_ONE, GL_ONE});
SoliderSprite *enemySolider=(SoliderSprite *)(enemy->objectAtIndex(0));
CCArray *position=enemySolider->soliderPosition;
position->retain();
cout<<((CCString *)position->objectAtIndex(0))->intValue()<<endl;
effectSprite->setPosition(ccp(((CCString *)position->objectAtIndex(0))->intValue(),((CCString *)position->objectAtIndex(1))->intValue()));
this->addChild(effectSprite);
string effectString="effect";
if(this->direction)
{
msg.property[1].append("L");
}
else
{
msg.property[1].append("R");
}
CCAnimate *effectAction=animate->createWithKind(msg.property[1],effectString.c_str(),2);
effectSprite->runAction(effectAction);
position->release();

It seems that is a bug and new version does not have that problem.
Try this code with version 2.1.5 of cocos2d-x:
CCSprite someSprite = CCSprite.spriteWithFile("someImage");
ccBlendFunc someBlend = new ccBlendFunc();
someBlend.src = OGLES.GL_ONE;
someBlend.dst = OGLES.GL_ONE;
someSprite.BlendFunc = someBlend;

I think the render mode is not what you want to show the final effect.
How about using:
SRC = GL_SRC_ALPHA,
SRC = GL_ONE_MINUS_SRC_ALPHA

Related

cocos2d - Sprite doesn't show properly

I am using cocos2dx v3.11.1 with c++ to create simple game for iOS platform. But I run into strange issue with sprite movement.
Definition of main objects looks like that:
Sprite *logoBackSprite;
SpriteFrameCache *cache;
SpriteBatchNode *batchNode;
I create sprite from the plist like that:
cache = SpriteFrameCache::getInstance();
cache->addSpriteFramesWithFile("game_sprites1.plist");
batchNode = SpriteBatchNode::create("game_sprites1.png");
addChild(batchNode);
logoBackSprite = Sprite::createWithSpriteFrameName(fileName);
logoBackSprite->setPosition(Vec2(centerPoint.x-4, centerPoint.y + 200-43));
addChild(logoBackSprite, 0);
After that I call function which must perform simple moving animation for sprite. Code of this function looks like that:
void MainMenuScene::playMenuAnimations() {
float startDelay = 0.0f;
auto windowSize = Director::getInstance()->getVisibleSize();
Point centerPoint = Vec2(windowSize.width/2, windowSize.height/2);
logoBackSprite->stopAllActions();
logoBackSprite->setOpacity(0);
logoBackSprite->runAction(Sequence::create(DelayTime::create(startDelay + 0.5 + 0.75),
FadeTo::create(0.5f, 255),
nullptr));
}
But when I run application - nothing happens. Could you point me what I am making wrong? Thank you.
EDIT After few hours of experiments, I have noticed, that runAction method of sprite objects doesn't work. For example, I have created vector of sprite frames and I want to run animation. When I run application I can see this sprite, but animation still does not work. Maybe, I missed smth important?
EDIT2
Today I have noticed that any action works prefect on previous scene.
Issue solved. Problem was in bad configuration of scene where I want to run animations.
I should override onEnter() and onExit() methods. In onEnter() method of scene I call
Layer::onEnter();
Also in scene class I create init() method like that:
bool MainMenuScene::init() {
if ( !Layer::init() )
{
return false;
}
return true;
}
And animation runs perfectly!

How to take screenshot of the scene in cocos2dx 3.3?

I Am developing a game and i want to take scene screen shot..How it possible in cocos2dx 3.3?Please tell me
My Code is Given Below
CCSize screenSize = CCDirector::sharedDirector()->getWinSize();
CCRenderTexture * tex = CCRenderTexture::create(screenSize.width, screenSize.height);
tex->setPosition(ccp(screenSize.width/2, screenSize.height/2));
tex->begin();
this->getParent()->visit();
tex->end();
tex->saveToFile("Image_Save.png", kCCImageFormatPNG);
use this
utils::captureScreen( CC_CALLBACK_2(GameScene::afterCaptured, this), "screenshot.png");
//callback function
void GameScene::afterCaptured(bool succeed, const std::string &outputFile)
{
if (succeed) {
log(" screen shot%s", outputFile.c_str());
}
}
Just Replace
tex->saveToFile("Image_Save.png", Image::Format::PNG);
Instead of
tex->saveToFile("Image_Save.png", kCCImageFormatPNG);
It might be surely help you

cocos2d v3 Sprite animation issue

Sprite animation issues.
Using this example which seems to work for others:
How to create animation in cocos2d 3.0?
So I do this:
NSMutableArray *ballAnimFrames = [NSMutableArray array];
for(int i = 1; i <= 11; ++i)
{
[ballAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache]
spriteFrameByName: [NSString stringWithFormat:#"discoball1200x2008c16-%d.png", i]]];
}
CCAnimation *ballAnim = [CCAnimation
animationWithSpriteFrames:ballAnimFrames delay:0.1f];
discoBallSprite = [CCSprite spriteWithImageNamed:#"discoball1200x2008c16-1.png"];
discoBallSprite.position = ccp(upper1Body.position.x,upper1Body.position.y-200);
CCActionAnimate *animationAction = [CCActionAnimate actionWithAnimation:ballAnim];
CCActionRepeatForever *repeatingAnimation = [CCActionRepeatForever actionWithAction:animationAction];
discoBallSprite.scaleX = 0.25;
discoBallSprite.scaleY = 0.25;
[discoBallSprite runAction:repeatingAnimation];
[self addChild:discoBallSprite z:10];
I get the following error:
'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
Obviously the error message is saying my image file is nil but it seems perfectly fine.
I tried renaming files to remove the extra "-" character but no change (I also renamed files outside of Xcode and re-imported with new name).
All my images are in Resources folder.
I tried moving images out of Resources folder to next level up but still the error.
The naming of them seems fine.
Any ideas what else to check?
Make a sprite sheet, add it to your int
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"YourAnim.plist"];
Now in the animation code
[ballAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [YourAnim.png", i]]];
When you make a sprite sheet it makes two things, 1) a png with the sprites and 2) a plist with the coordinates
if you so decide to use the program I told you about make sure you set it to cocos2d
See The code Link
Now Change for Cocos2dV3 is below.
In the link code there is CCAnimation that is replace by CCActionAnimate in V3 and also CCRepeatForever is replace by CCActionRepeatForever in V3.
Also check your .plist images may be 10 althougt in for condition it get 11th image in it. so may be that is your problem to insert image in array,
replace and try.
for(int i = 1; i < 11; ++i)
{
// Code
}
Change it work fine.

CCTableView with cocos2d 3.0

I'm trying to get CCTableView working with cocos2d 3.0 but I really don't know where to start. Does anyone have a good tutorial or anything for 3.0? I see there are a few for older versions of cocos2d but nothing for 3.0. Any help is appreciated! Thanks!
Here is a short sample. I do recommend just trying out writing some code on your own and then posting what problems you are having with it. It definitely makes for easier answering of questions. You can also create your own subclass of CCTableViewCell if you want.
The Header file, SampleTableView.h
#import "cocos2d.h"
#import "cocos2d-ui.h"
#interface SampleTableView : CCNode <CCTableViewDataSource>
#end
The source file: SampleTableView.m
float const kNumberOfRows = 30.0f;
#implementation SampleTableView
- (instancetype)init
{
self = [super init];
if (self) {
CCTableView* table = [CCTableView node];
table.dataSource = self; // make our class the data source
table.block = ^(CCTableView* table) {
NSLog(#"Cell %d was pressed", (int) table.selectedRow);
};
[self addChild:table];
}
return self;
}
- (CCTableViewCell*) tableView:(CCTableView*)tableView nodeForRowAtIndex:(NSUInteger) index {
CCTableViewCell* cell = [CCTableViewCell node];
cell.contentSizeType = CCSizeTypeMake(CCSizeUnitNormalized, CCSizeUnitUIPoints);
cell.contentSize = CGSizeMake(1.0f, 32.0f);
float colorFactor = (index / kNumberOfRows);
// Just a sample node that changes color with each index value
CCNodeColor* colorNode = [CCNodeColor nodeWithColor:[CCColor colorWithRed:colorFactor green:(1.0f - colorFactor) blue:(0.2f + 0.5 * colorFactor) ] width:100.0f height:18.0f];
[cell addChild:colorNode];
return cell;
}
- (NSUInteger) tableViewNumberOfRows:(CCTableView*) tableView {
return kNumberOfRows; // just a demo
}
And how to add it in your hello world scene or elsewhere:
SampleTableView* table = [SampleTableView node];
table.contentSizeType = CCSizeTypeNormalized;
table.contentSize = CGSizeMake(1.0, 1.0);
Here is a sample screenshot of what it looks like:
You should check out the cocos2d-tests-ios.xcodeproj in the cocos2d source.
It has a sample for pretty much every thing implemented in cocos2d, Including a CCTableView sample.
https://github.com/cocos2d/cocos2d-iphone
git clone cocos2d source.
Open the cocos2d-tests-ios.xcodeproj project and build/run the cocos2d-tests-ios target.

cocos2d-x convert UIImage to CCSprite

has anyone an idea how to convert a UIImage to cocos2d-x CCSprite.
My latest attempt was was:
1. Store the UIImage as png on the phone
2. Load the png as a CCSprite
[UIImagePNGRepresentation(photo) writeToFile:imagePath atomically:true];
CCSprite *sprite = CCSprite::spriteWithFile(imagePath);
But this crashed in CCObject retain function
void CCObject::retain(void)
{
CCAssert(m_uReference > 0, "reference count should greater than 0");
++m_uReference;
}
And I do not understand how Walzer Wangs suggestion works
http://cocos2d-x.org/boards/6/topics/3922
CCImage::initWithImageData(void* pData, int nDataLen, ...)
CCTexture2D::initWithImage(CCImage* uiImage);
CCSprite::initWithTexture(CCTexture2D* pTexture);
CCSprite* getCCSpriteFromUIImage(UIImage *photo) {
CCImage *imf =new CCImage();
NSData *imgData = UIImagePNGRepresentation(photo);
NSUInteger len = [imgData length];
Byte *byteData = (Byte*)malloc(len);
memcpy(byteData, [imgData bytes], len);
imf->initWithImageData(byteData,imgData.length);
imf->autorelease();
CCTexture2D* pTexture = new CCTexture2D();
pTexture->initWithImage(imf);
pTexture->autorelease();
CCSprite *sprit = new CCSprite();
sprit->createWithTexture(pTexture);
sprit->autorelease();
DebugLog("size :%f :%f ",sprit->getContentSize().width , sprit->getContentSize().height);
return sprit;
}
I got the solution for my own Problem. You can't create a CCSprite before the CCDirector is initialized. There are missing some config settings and cocos releases the image right after it is instantiated.
Save uiimage to documents directory first
now get it using getWritablePath
Texture2D* newTexture2D = new Texture2D();
Image* JpgImage = new Image();
JpgImage->initWithImageFile("your image path.jpg");
newTexture2D->initWithData(JpgImage->getData(),JpgImage->getDataLen(),Texture2D::PixelFormat::RGB888,JpgImage->getWidth(),JpgImage->getHeight(),Size(JpgImage->getWidth(),JpgImage->getHeight()));