CCSprite EXC_BAD thing PROBLEM, PLEEEASE HELP

Dear Cocos2d-x forum
I am pretty new to iOS development but am good in c++ ONLY
I wanted to create a sprite from a class but every time i try to run the simulator, it crashes!

Here is my syntax code that i want to get fixed!

void Game::initializeGame()
{

if (Utils::s().width > 1024) {
    
}
else { CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("GEMS.plist");
}

gems = CCArray::create();
gems->retain();

for (int i = 1; i <= 3; i++) {
    Gem *gem = (Gem *) Gem::create();
    gem->setPosition(ccp(300 * i , 667)); (EXC_BAD code=2 adress=0x0)
    gems->addObject(gem);
    this->addChild(gem, 1);
}

And here is the header file from the Gem.h

---------------------------------------------------------------//
// Mole.h
// moleit-x
//
//
// Copyright © 2012 MyCompanyName. All rights reserved.
//

#ifndef moleit_x_Mole_h
#define moleit_x_Mole_h

#include “cocos2d.h”

using namespace cocos2d;

class Gem : public cocos2d::CCSprite
{
private:

public:
float upTime;
bool isIn, didMiss;
// Here’s a difference. Method ‘init’ in cocos2d-x returns bool, instead of returning ‘id’ in cocos2d-iphone
virtual bool init();
void stop();
void reset();
void start();
void wasTapped();
void startAndFall();
bool getIsUp();
void stopEarly();
void startLoopAnimation();

CCSprite* gem;
// implement the “static node()” method manually
CREATE_FUNC(Gem);
};

#endif // HELLOWORLD_SCENE_H

And finally the definition of init

#define TAG_REPEAT_ANIM 6

bool Gem::init()
{
if ( !Gem::init() )
{
return false;
}

this->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("greenNOTHDgemstone.png"));

return true;

}


JUST ASK ME IF U HAVE SOME QUESTIONS
u guys can save my life!

Hello!

From what I understand, you want to subclass CCSprite with your class, am I right?

If you want to do that, you can try this (C++ Class Player extends CCSprite):

#pragma once

#include "cocos2d.h"

class Player : public cocos2d::CCSprite
{
public:
    // functions
    bool initPlayer();
    void onExit();
    
    // overload CCSprite create to meet your needs
    static Player* create(std::string pSpriteFrame);
};
#include "Player.h"

USING_NS_CC;

// overload CCSprite create to meet your needs
Player* Player::create(std::string pSpriteFrame)
{
    Player *sprite = new Player();
    if (sprite && sprite->initWithSpriteFrameName(pSpriteFrame.c_str()))
    {
        sprite->autorelease();
        return sprite;
    }
    CC_SAFE_DELETE(sprite);
    return NULL;
}

bool Player::initPlayer()
{
   // do whatevery init work you need to do: init variables, load animations, etc
}

void Player::onExit()
{
    // release everything you allocated. Basically this is ~Player()
}

Note that I used initWithSpriteFrameName to create the Sprite because I am using spritesheets. You should change this accordingly depending on how you are creating the sprite!

And finally, create the Player in some CCScene:

    // Create the player
    Player *player = Player::create("idle.png");
    player->initPlayer();
    player->setPosition(ccp(visibleSize.width*0.5, visibleSize.height*0.5));
    addChild(player);

Hope this helps :slight_smile:

Let me know if you have questions!

OMG, IT WORKED… LOVE U!!!(/)

U have just saved my life

Glad I could help :slight_smile: