0

I'm just training cocos2d-x.

Now,I tryed to create sprite on display using CCSprite class.

I wrote the code as follows.

Characters Class

class characters : public cocos2d::CCSprite{
    public:

    void setRect(CCPoint point);
    CCRect getRect();

    void setTag(int x,int y);
    int getTag();

    void setDirection(int num);
    int* getDirection();

    bool isTouchPoint(CCPoint point);
    void animation(characters *chara,int num);



 private:
    CCRect m_rect;
    int m_tag;
};



bool HelloWorld::init(){


 for(int i=0;i<9;i++){
    characters *chara = new characters;
    chara->autorelease();
    chara->create("char04x2.png");

    x = Indexes[i]%HORIZONTAL_AXIS; //Indexes is cell number on display
    y = Indexes[i]/HORIZONTAL_AXIS;

    CCPoint point = ccp(MARGIN_WIDTH + PIPE_WIDTH * x,MARGIN_HEIGHT + PIPE_HEIGHT * y);
    chara->setPosition(point);
    chara->setRect(point);
    chara->setTag(x,y);


    this->addChild(chara,1);

 }
}

But,this code is wrong.

It stops in CC_NODE_DRAW_SETUP() of void CCSprite::draw(void) method.

Error message is

Cocos2d: Assert failed: No shader program set for this node Assertion failed: (getShaderProgram()), function draw, file /Users/nyoronyoro-kun/Desktop/cocos2d-x/cocos2dx/sprite_nodes/CCSprite.cpp, line 554.

When I replaced create method with initWithFile method,error don't appear.

Why this error appeared?

user3321541
  • 221
  • 1
  • 16

2 Answers2

1

In lines

characters *chara = new characters;
chara->autorelease();

you create an empty sprite (with no texture). Then at line

chara->create("char04x2.png");

you create another sprite with an associated texture but you don't assign it to nothing! This line of code do nothing for you!

Result: in draw function your sprite (created in the first two lines of code) doesn't have an associated texture and it has never been initialized!

'create' function is a static function, that is a "class function":

static CCSprite* create(const char *pszFileName);

Use it in this way:

chara = CCSprite::create("char04x2.png");

and remove the first two lines.

I hope this can help you.

Salvatore Avanzo
  • 2,656
  • 1
  • 21
  • 30
  • Thank you for your comment. When I repaired as you pointed out,the error disappear. But new error appear. the error message is malloc: *** error for object 0x9e44564: incorrect checksum for freed object - object was probably modified after being freed. *** set a breakpoint in malloc_error_break to debug How should I solve? Sorry to keep pestering you. – user3321541 May 04 '14 at 13:50
  • @user3321541: Have you removed the first two lines of code? especially your explicit autorelease? – Salvatore Avanzo May 04 '14 at 14:00
  • @Salvatore Avanzo Yes.I have removed the first two lines. – user3321541 May 04 '14 at 14:05
  • @user3321541 : I'm sorry but more code is needed. Maybe this [link](http://stackoverflow.com/questions/19840671/malloc-error-for-object-0x208a7614-incorrect-checksum-for-freed-object-o) can help you. – Salvatore Avanzo May 04 '14 at 14:11
  • @Salvatore Avanzo:Thank you for showing the link. I intend to refer to the link. – user3321541 May 04 '14 at 14:57
0

In init() method of your derived class (charecters) you missed calling super Class method init. call CCSprite::init() in your init method should solve your problem

Alok Rao
  • 162
  • 1
  • 12