Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8623121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:12:37+00:00 2026-06-12T07:12:37+00:00

I am making a blackjack game in cocos2d-x here is the following code of

  • 0

I am making a blackjack game in cocos2d-x here is the following code of the same where I am getting crash but can not understant why this is happening whats going wrong ? I tried several things thinking that crash might be because of string memory leak or something

CCSprite* _big = CCSprite::createWithSpriteFrameName(ptr);

Here is the card cpp file which inherits CCSprite

Card.cpp

#include "Card.h"

Card::Card() {
  }

Card::~Card() {
  }

void Card::init(int value, int suit) {
    mValue = value;
    mSuit = suit;
    mGameValue = 0;
    UnHide();
  }

void Card::UnHide() {

    mHidden = false;
    //if(this)
    //this->removeAllChildrenWithCleanup(true);
    //this->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("card"));
    this->initWithSpriteFrameName("card");

    char strName[64]={0};

    //memset ( strName, 0, sizeof ( strName ));

    switch(mSuit){
    case 0: sprintf(strName,"%s","club_big");break;
    case 1: sprintf(strName,"%s","diamond_big");break;
    case 2: sprintf(strName,"%s","spade_big");break;
    case 3: sprintf(strName,"%s","heart_big");break;
    default :
            break;
    }

    const char *ptr = strdup(strName);  

    CCSprite* _big = CCSprite::createWithSpriteFrameName(ptr);
    delete  ptr;

   // CCSprite* big = CCSprite::create(strName);
    _big->setPosition(ccp(55, 44));
    this->addChild(_big);

    switch(mSuit){
        case 0: sprintf(strName,"%s","club");break;
        case 1: sprintf(strName,"%s","diamond");break;
        case 2: sprintf(strName,"%s","spade");break;
        case 3: sprintf(strName,"%s","heart");break;
        default :
            break;
    }

    const char *_ptr = strdup(strName); 

    CCSprite* _small = CCSprite::createWithSpriteFrameName(_ptr);
    delete _ptr;

   // CCSprite* small = CCSprite::create(str);
    _small->setPosition(ccp(16, 82));
    this->addChild(_small);

    if(mSuit==0 || mSuit==2){
        switch(mValue){
            case 1: sprintf(strName,"%s","blacka");mGameValue = 1;break;
            case 2: sprintf(strName,"%s","black2");mGameValue = mValue;break;
            case 3: sprintf(strName,"%s","black3");mGameValue = mValue;break;
            case 4: sprintf(strName,"%s","black4");mGameValue = mValue;break;
            case 5: sprintf(strName,"%s","black5");mGameValue = mValue;break;
            case 6: sprintf(strName,"%s","black6");mGameValue = mValue;break;
            case 7: sprintf(strName,"%s","black7");mGameValue = mValue;break;
            case 8: sprintf(strName,"%s","black8");mGameValue = mValue;break;
            case 9: sprintf(strName,"%s","black9");mGameValue = mValue;break;
            case 10: sprintf(strName,"%s","black10");mGameValue = mValue;break;
            case 11: sprintf(strName,"%s","blackj");mGameValue = 10;break;
            case 12: sprintf(strName,"%s","blackq");mGameValue = 10;break;
            case 13: sprintf(strName,"%s","blackk");mGameValue = 10;break;
            default :
            break;
        }
    }
    else {
        switch(mValue){
        case 1: sprintf(strName,"%s","reda");mGameValue = 1;break;
        case 2: sprintf(strName,"%s","red2");mGameValue = mValue;break;
        case 3: sprintf(strName,"%s","red3");mGameValue = mValue;break;
        case 4: sprintf(strName,"%s","red4");mGameValue = mValue;break;
        case 5: sprintf(strName,"%s","red5");mGameValue = mValue;break;
        case 6: sprintf(strName,"%s","red6");mGameValue = mValue;break;
        case 7: sprintf(strName,"%s","red7");mGameValue = mValue;break;
        case 8: sprintf(strName,"%s","red8");mGameValue = mValue;break;
        case 9: sprintf(strName,"%s","red9");mGameValue = mValue;break;
        case 10: sprintf(strName,"%s","red10");mGameValue = mValue;break;
        case 11: sprintf(strName,"%s","redj");mGameValue = 10;break;
        case 12: sprintf(strName,"%s","redq");mGameValue = 10;break;
        case 13: sprintf(strName,"%s","redk");mGameValue = 10;break;
        default :
            break;
        }
    }

    const char *_ptrr = strdup(strName);    

    CCSprite* _num = CCSprite::createWithSpriteFrameName(_ptrr);
    delete _ptrr;
   // CCSprite* _num = CCSprite::create(strName);
    _num->setPosition(ccp(18, 108));
    this->addChild(_num);
}

void Card::Hide() {

    mHidden = true;
    this->removeAllChildrenWithCleanup(true);
    this->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("deck"));
    //this->initWithSpriteFrameName("deck");
}

Intialization of cards is as per this

for (int suit = 0; suit < 4; suit++) {
            for (int value = 0; value < 13; value++) {
                Card* card = new Card;
                //card->init
                card->init(value+1, suit);
              mCard[suit*13 + value] = card;
            }
          }

The crash I get is on the line of initWithSpriteFrameName but it doesnt occur frequently

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T07:12:38+00:00Added an answer on June 12, 2026 at 7:12 am

    h file:

    class Card : public cocos2d::Node
    {
    public:
    int mValue;
    int mSuit;
    bool mHidden;
    int mGameValue;
    CCSprite* front;
    CCSprite* back;
    
    static Card* create();
    bool init(int value, int suit);
    void UnHide();
    void Hide();
    };
    

    cpp file

    Card* Card::create(int value, int suit){
        Card* card = new Card();
        if(card && card->init(value, suit)){
            card->autorelease();
            return card;
        }
        else{
            CC_SAFE_DELETE(card);
            return NULL;
        }
    }
    
    bool Card::init(int value, int suit) {
        if(CCNode::init()){
            mValue = value;
            mSuit = suit;
            mGameValue = 0;
    
            //create your front and back sprite here as what you did in Unhide
            CCSprite* back = blablabla;
            CCSprite* front = blablabla;
    
            //add them as a child of current node
            this->addChild(back);
            this->addChild(front);
    
    
            this->UnHide();
            return true;
        }
        return false;
    }
    
    void Card::UnHide() {
        back->setVisible(false);
        front->setVisible(true);
    }
    
    void Card::Hide() {
        back->setVisible(true);
        front->setVisible(false); 
    }
    

    and for your deck, from my understanding,

    CCArray::createWithCapacity(52);
    

    will be a better choice.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Making a server in java, First off, Here's the code. AnsiConsole.out.println(This is a Test.);
making an infinite loop in javascript, and jQuery... this is my current code: $(#bg2).css({opacity:
Making a new shooter game here in the vein of Galaga (my fav shooter
I am new to programming and I'm making a very simple blackjack game with
I'm starting out with xna, I'm pretty newbie with this, but I'm making my
Im making a game, when my mainCharacter in this game dies, or when he
I'm making a blackjack game where a card needs to be shown a second
Making a simple Java code for Blackjack and need some advice. OK I am
Im making a game in Java with a few other people but we are
I'm making a Blackjack game for a project in college (semester-long) and I've hit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.