So I have a class that inherits Sprite (I call it Brick). In my main layer, I declare and initialize 46 of those objects and push them into a std::vector.
The class that inherits the sprite looks like this:
#include "Brick.h"
#include "LevelOne.h"
Brick::~Brick()
{
CC_SAFE_RELEASE(appearAnimate);
CC_SAFE_RELEASE(disintegrateAnimate);
}
Brick * Brick::create(int x, int y)
{
Brick * brick = new Brick();
if (brick && brick->initWithFile("brick1.png"))
{
brick->autorelease();
brick->initBrick(x,y);
return brick;
}
CC_SAFE_DELETE(brick);
return NULL;
}
void Brick::initBrick(int x, int y)
{
char str[100] = { 0 };
Vector<SpriteFrame*> appearFrames(5);
for (int i = 5; i <= 1; i--) //Iterate for the number of images you have
{
sprintf(str, "brick%i.png", i);
auto frame = SpriteFrame::create(str, Rect(0, 0, 32, 32)); //The size of the images in an action should be the same
appearFrames.pushBack(frame);
}
auto appearAnimation = Animation::createWithSpriteFrames(appearFrames, 0.20f);
appearAnimate = Animate::create(appearAnimation);
appearAnimate->retain(); //Retain to use it later
Vector<SpriteFrame*> disintegrateFrames(5);
for (int i = 1; i <= 5; i++)
{
sprintf(str, "brick%i.png", i);
auto frame = SpriteFrame::create(str, Rect(0, 0, 32, 32));
disintegrateFrames.pushBack(frame);
}
auto disintegrateAnimation = Animation::createWithSpriteFrames(disintegrateFrames, 0.12f);
disintegrateAnimate = Animate::create(disintegrateAnimation);
disintegrateAnimate->retain();
brickbox = Sprite::create("brick1.png");
this->setAnchorPoint(Point(0, 0));
this->setPosition(Point(brickbox->getBoundingBox().size.width * x, brickbox->getBoundingBox().size.height * y));
brickbody = PhysicsBody::createBox(Size(32,32), PhysicsMaterial(0, 0, 0));
brickbody->setCollisionBitmask(4);
brickbody->setContactTestBitmask(true);
brickbody->setDynamic(false);
this->setPhysicsBody(brickbody);
}
void Brick::appear()
{
this->runAction(appearAnimate);
}
void Brick::deletion()
{
this->setOpacity(0);
this->brickbody->removeFromWorld();
}
void Brick::disintegrate()
{
this->runAction(disintegrateAnimate);
}
void Brick::creation()
{
this->setOpacity(255);
this->setPhysicsBody(brickbody);
}
void Brick::drill()
{
CCDelayTime *delay1 = CCDelayTime::create(1.5f);
CCDelayTime *delay2 = CCDelayTime::create(3.0f);
CallFunc *disintegrate = CallFunc::create([this]()
{
this->disintegrate();
});
CallFunc *deletion = CallFunc::create([this]()
{
this->deletion();
});
CallFunc *appear = CallFunc::create([this]()
{
this->appear();
});
CallFunc *brickcreate = CallFunc::create([this]()
{
this->creation();
});
runAction(Sequence::create(disintegrate, delay1, deletion, delay2, Spawn::create(appear, brickcreate), NULL));
}
When I call the method drill like this: bricks.at(brickpos)->drill();
where brickpos is the position of the object in the vector and the bricks is the vector itself, I get the error:
Sometimes the compiler either points me that he couldn’t setphysicsbody or to getOwner() method.