class Foo: Sprite{
public:
void myUpdate(float dt){
if(...){
removeFromParent(); //foo is destroyed
}
}
I have similar code which crashes sometimes: Inside myUpdate function, this is 0xDDDDD
class Foo: Sprite{
public:
void myUpdate(float dt){
if(...){
removeFromParent(); //foo is destroyed
}
}
I have similar code which crashes sometimes: Inside myUpdate function, this is 0xDDDDD
Bro ! You’ve remove the node that have been delete (0xDDDDD mean that pointer have been release) . You should do this :
class Foo: Sprite{
public:
void myUpdate(float dt){
if(…){
removeFromParent(); //foo is destroyed
unscheduleUpdate(); // if you use this->scheduleupdate();
//or
//unschedule(schedule_selector(Foo::myUpdate);
}
}
Hope you fix that bro !
The node is 0xDDDDD before calling removeFromParent() in myUpdate. I suspect it is caused by the constructor. The node is removed and deleted by the scheduled lambda, and then myUpdate is invoked, where the content of this should be 0xDDDDDD. It’s really annoying that the bug comes out rarely.
Foo::Foo(){
scheduleOnce([this](float){
removeFromParent();
}, 1.f, "removeFromParent");
}
bool Foo::init(){
schedule(CC_SCHEDULE_SELECTOR(Foo::myUpdate), 0.1f);
//....
}
You must be trying to call myUpdate from within the lambda or otherwise are trying to access this after the call to removeFromParent. It’s possible you’re calling removeFromParent twice, but your code would imply that you are not actually since the lambda or myUpdate function will call it, but not both.
What is the if condition inside myUpdate? The code you’ve posted seems to work fine no matter what delay and interval I use and how many times I run it. The issue probably lies outside of the code you’ve posted.
myUpdate is actually updateCustom. Foo is Jian. Jian is a bullet that explodes( dispatches a damage event) when it is collided with any sprites on the scene.
bool Jian::isCollided(Sprite* sp){
return getBoundingBox().intersectsRect(sp->getBoundingBox());
}
class Xu {
public:
Xu(Ref* ref) : _ref(ref) { _ref->retain(); }
~Xu() { _ref->release(); }
Ref* _ref;
};
Have you try my solution ? Try it . Your have error because you node is removed by myUpdate() once and now be remove again by same function.
I’ve tried it. But it doesn’t work. The problem is that scheduled function is called after the node has been deleted. Since I cannot find a minimal, complete, and verifiable example, I’m going to use other methods.
removeFromParent should unschedule all updates on that sprite (itself as target).A good pattern for removing or deleting objects from a game or scene is to mark them as deleted and sweep them up at the end of the frame. You could also have a vector<Node*> nodesToRemove
In order to do this you would probably want to have more control over when things get updated by scheduling an update in SpriteManager and then call each sprite’s update method manually.
SpriteManager::update(float dt) {
// optionally queue a list of objects to add, loop through each data config and create sprites
for(auto sp : this->getVas()) {
sp->updateCustom(dt);
}
// remove marked for deletion
for(auto toRemove : this->getVas_toRemove() ) {
toRemove->removeFromParent(); // optionally don't cleanup if using a pool
}
}
Might help?
It’s unbelieveable that I have done nearly the same thing as you mention, haha. last github commint
Given a list of rects (sprite) and a list of circles (bullet), what are ways to detect collisions between circle and rect? Is it wise to use physics engine only for collision detection?
Haha, nice!
A physics engine can work for collision detection only. If you already have experience using a physics engine that might be the smart direction to take.
On the other hand collision detection is often pretty simple. Circle to rect usually just involves comparing the center of the circle with the 4 corners of the rect using radius of the circle and the width or height of the rect.
There are slightly more advanced concepts you can use to improve collision detection.
Handmade Hero has a couple videos on these:
Basic collisions
Improve to prevent tiny gaps.
Collision detection loop using pairs of sprites or entities
Search on “collision” of their videos
Thanks. I have managed to get it, though Physics doesn’t work well with Timeline animation.