Crash on updating position of Box2D body based on Cocos2d sprite - cocos2d-x

this is how I remove my body from the world: _world->DestroyBody(spriteBody);
In my game, I constantly create Cocos2d Sprite and Box2D Body and I also constantly delete them.
However, when I try to LOG("%d", _world->GetBodyCount()); the number is increasing and never decrease.
I think _world->DestroyBody(spriteBody); might not completely delete my Box2D body.
This is my delete method.
void GameScene::deleteSprite(Sprite *sprite){
b2Body *spriteBody = NULL;
for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext()) {
if (b->GetUserData() != NULL) {
Sprite *curSprite = (Sprite *)b->GetUserData();
if (sprite == curSprite) {
log("3");
spriteBody = b;
break;
}
}
}
if (spriteBody != NULL) {
//spriteBody->SetUserData(NULL);
//b2Fixture* f = spriteBody->GetFixtureList();
//spriteBody->DestroyFixture(f);
spriteBody->GetWorld()->DestroyBody(spriteBody);
}
sprite->removeFromParentAndCleanup(true);
}
Create method - I took this code from GMTDev and Raywenderlich. Box2D for collision detection only
void GameScene::addBoxBodyForSprite( Sprite *sprite, int iNumVerts, b2Vec2 verts[] ){
if( _world==NULL )
return;
Point pos = sprite->getPosition();
Size size = sprite->getContentSize();
b2BodyDef spriteBodyDef;
spriteBodyDef.type = b2_dynamicBody;
spriteBodyDef.position.Set(pos.x/PTM_RATIO, pos.y/PTM_RATIO);
spriteBodyDef.userData = sprite;
b2Body *spriteBody = _world->CreateBody(&spriteBodyDef);
b2PolygonShape spriteShape;
if( iNumVerts!=0 )
{
spriteShape.Set(verts, iNumVerts);
b2FixtureDef spriteShapeDef;
spriteShapeDef.shape = &spriteShape;
spriteShapeDef.density = 10.0;
spriteShapeDef.isSensor = true;
spriteBody->CreateFixture(&spriteShapeDef);
}
else
{
// No Vertice supplied so just make a box round the sprite
b2BodyDef spriteBodyDef;
spriteBodyDef.type = b2_dynamicBody;
spriteBodyDef.position.Set( pos.x/PTM_RATIO, pos.y/PTM_RATIO );
spriteBodyDef.userData = sprite;
b2Body *spriteBody = _world->CreateBody( &spriteBodyDef );
b2PolygonShape spriteShape;
spriteShape.SetAsBox( size.width/PTM_RATIO/2, size.height/PTM_RATIO/2 );
b2FixtureDef spriteShapeDef;
spriteShapeDef.shape = &spriteShape;
spriteShapeDef.density = 10.0;
spriteShapeDef.isSensor = true; // isSensor true when you want to know when objects will collide without triggering a box2d collision response
spriteBody->CreateFixture( &spriteShapeDef );
}
}
Thanks !!

The problem is solved from the help of #UmeshSharma and #LearnCocos2D
in the create method that I got from GMTDev has some mistakes where the function _world->CreateBody is used twice (when iNumVerts == 0)
Therefore, when I delete my body and sprite. There is actually another body remaining for that sprite. And then it crash on the update where the Box2D body cannot find its Cocos2d sprite.

Related

OpenFL - can someone explain __combinedVisible property of flash.display.Bitmap

Im using a Bitmap for a button in OpenFL. What im seeing when I set myBitmap.visible = true is that the bitmap does not become visible.
It seems I need to call myBitmap.__CombinedVisible = true as well for it to be drawn. I can't find any documentation for this property on how to use it properly.
I'm also noticing the first time I switch between 2 bitmaps that the bitmap made visible does not appear right away, but the one made invisible disappears right away. Any time after that it behaves properly and the switch happens instantly.
Could this have something to do with __CombinedVisible?
You can see the bitmap switching code below inside of my button.
private function update() : Void {
if( state == ButtonState.OVER ){
this.over.visible = this.over.__combinedVisible = true;
this.up.visible = this.down.visible = false;
}else if( state == ButtonState.UP ){
this.up.visible = this.up.__combinedVisible = true;
this.down.visible = this.over.visible = false;
} else if( state == ButtonState.DOWN ){
this.down.visible = this.down.__combinedVisible = true;
this.up.visible = this.over.visible = false;
}else if( state == ButtonState.CLICK ) {
this.up.visible = this.up.__combinedVisible = true;
this.over.visible = this.down.visible = false;
this.enabled = false;
dispatchEvent(new Event("CLICK"));
}
}
So after a bunch of testing I'v narrowed it down to this: If I set visible to false prior to assigning the BitmapData this __combinedVisible member seems to need to be used. If I do it right after setting the BitmapData it still needs this.
If I let the bitmap draw for 1 frame then set visible to false. visible = true works after this happens and I can now see the bitmap.
But if it doesn't draw once then visible = true does not show the bitmap.
Can I not create an empty bitmap this way and assign the BitmapData later? It works on the up state as I never set visible = false prior to it being drawn the first time.
Okay so after tons of testing and trying to get this to work it just won't.
There is a bug here where if visible is set to false before drawing the bitmap to the canvas it won't draw it until __combinedVisible is set to true. This always causes a flash when drawing the bitmap for the first time as well, so its not feasible to use.
I went and used alpha instead of visible. This works properly and as expected.
If someone can get this working with visible I will accept that answer.
private function update() : Void {
if( state == ButtonState.OVER ){
this.over.alpha = 1;
this.up.alpha = this.down.alpha = 0;
}else if( state == ButtonState.UP ){
this.up.alpha = 1;
this.down.alpha = this.over.alpha = 0;
} else if( state == ButtonState.DOWN ){
this.down.alpha = 1;
this.up.alpha = this.over.alpha = 0;
}else if( state == ButtonState.CLICK ) {
this.up.alpha = 1;
this.over.alpha = this.down.alpha = 0;
this.enabled = false;
dispatchEvent(new Event("CLICK"));
}
}

Chipmunk collisions not happening when moving sprite using actions

Really a basic question, but I am moving a sprite A with a physics body over another kind of sprites B having another physics body. I expect the collision callback oncontact to be called for these bodies. They have their respective category bitmasks set using setCategoryBitmask() and also respectively have each other's categories set using setContactTestBitmask().
The collision works as long as I do not move sprite A. I assume the problem is that I move sprite A using cocos2d actions, and I need to do something else. But using cocos2d actions for scripting things like these look so much simpler to me than anything else I can think of.
Move sprite A using physics calls. (looks like a lot of work, and it looks like it's hard to achieve exact scripting perfection)
Do my own collision detection in update() instead. (looks like a bunch of work too, especially if the sprites are rotated etc)
Is there any other shortcut? Or did I miss something else?
I ended up doing "Do my own collision detection in update() instead". It wasn't too problematic.
Something like this in update()...
const Rect RECT_A = spriteA->getBoundingBox();
for (auto spriteB : someparent->getChildren()) {
const Rect RECT_B = spriteB->getBoundingBox();
if (RECT_A.intersectsRect(RECT_B)) {
// Hit
}
}
Contaction detection should be fine while collion doesn't work.
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::createWithPhysics();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
scene->getPhysicsWorld()->setGravity(Vec2::ZERO);
// return the scene
return scene;
}
bool HelloWorld::init()
{
if ( !Layer::init() )
{
return false;
}
auto createSprite = [this](const Vec2& pos) {
auto sprite = Sprite::create();
auto bodyA = PhysicsBody::createCircle(20);
sprite->setPhysicsBody(bodyA);
sprite->setPosition(pos);
bodyA->setCollisionBitmask(0xff);
bodyA->setCategoryBitmask(0xff);
bodyA->setContactTestBitmask(0xff);
return sprite;
};
auto spriteA = createSprite(Vec2(300, 300));
auto moveBy = MoveBy::create(1.f, Vec2(200, 200));
spriteA->runAction(moveBy);
auto spriteB = createSprite(Vec2(350, 350));
addChild(spriteA);
addChild(spriteB);
auto contactListener = EventListenerPhysicsContact::create();
contactListener->onContactBegin = [=](PhysicsContact& contact) {
auto a = contact.getShapeA()->getBody()->getNode();
auto b = contact.getShapeB()->getBody()->getNode();
assert((a == spriteA && b == spriteB) || (a == spriteB && b == spriteA));
log("It is working");
return false;
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this);
return true;
}

Using createjs, preoloading and caching, why is my canvas still so slow?

I am trying to create a small RPG for class. I create one Bitmap image, cache it, and then every time I need that same Bitmap, I clone it, and finally add them to the stage. However, despite my efforts, my canvas is still extremely slow since I am drawing all of these bushes.
I would put them in a container, however, I need to know the X and Y position that way I know if the player is trying to step over their boundaries.
Here is my function:
parseRoom: function(mapObject, room, obj, image){
var letter = 'X';
//var object = obj;
var object = null;
var img = new createjs.Bitmap(image);
for(var m=0; m < mapObject.length; m++){
for(var j=0; j< mapObject[m].length; j++){
letter = mapObject[j][m];
switch (letter){
case 'X':
//do nothing
break;
case 'O':
//object = this.createObject();
//object.image = img.clone();
img.cache();
object = img.clone();
room.AddObstacle(object, m, j);
break;
}
}
}
}
and this is my function when I actually add them to the stage:
addObstacle: function(imgObj, x, y){
imgObj.x = x ||imgObj.x || 0;
imgObj.y = y ||imgObj.y || 0;
imgObj.setVisible = function(visible){
this.visible = visible;
};
imgObj.update = function(){
if(this.visible)this.visible= true;
else this.visible = false;
};
objects.push(imgObj);
stage.addChild(imgObj);
},
as you can see, I extend the Bitmap class and also add and update and a setVisible method to it. That way I can turn them all off or on depending on the screen. Any ideas that could help me and make my game smoother? Thank you!
I was constantly updating every object, including the bushes, and there was no need to update the bushes every tick. I simply removed logic from checking the bushes and now it runs really fast.

AS3 Object Bin and Reset Button

I am trying to create a game for kids where they can drag letters on to a stage to make words.
I want to add a 'trash can' where users can drag letters they no longer need to dispose of them. I have created the movie clip but am totally unsure how to make it function using AS3.
I would also like to add a reset button so that the stage reverts to it's original state. Again, I have drawn it up and added the little as3 that I am aware of (to make it a button) but if anyone could assist with how to actually make this happen, I would be grateful.
The files are here: SWF | FLA and the code for the game is as follows:
import flash.display.MovieClip;
for (var i=1; i<27; i++)
{
this["object" + i].addEventListener(MouseEvent.MOUSE_DOWN, onStart);
this["object" + i].addEventListener(MouseEvent.MOUSE_UP, onStop);
}
var sx = 0,sy = 0;
function onStart(e)
{
sx = e.currentTarget.x;
sy = e.currentTarget.y;
e.currentTarget.startDrag();
}
function onStop(e)
{
if ( e.target.dropTarget != null &&
e.target.dropTarget.parent == dest &&
e.currentTarget.name != "copy" )
{
var objectClass:Class =
getDefinitionByName(getQualifiedClassName(e.currentTarget)) as Class;
var copy:MovieClip = new objectClass();
copy.name = "copy";
this.addChild(copy);
copy.x = e.currentTarget.x;
copy.y = e.currentTarget.y;
e.currentTarget.x = sx;
e.currentTarget.y = sy;
copy.addEventListener(MouseEvent.MOUSE_DOWN, onStart);
copy.addEventListener(MouseEvent.MOUSE_UP, onStop);
}
e.currentTarget.stopDrag();
}
resetButton.addEventListener(MouseEvent.CLICK, reset);
resetButton.buttonMode = true;
function reset(event:MouseEvent):void
{
//Not sure what AS3 to add here to reset to original state
}
I have already gave you the solution here Flash AS3 Clone, Drag and Drop
Here, I am providing a detail solution on how to drag objects inside a bin and remove them.
For dropping copied objects inside a bin, after dragging is stopped, check collision with bin object. for more info see,
copiedObject.hitTestObject(binObject)
For e.g.
First create trash-can MovieClip on the stage and give it an instance name 'trashCan' and add following lines to your onStop()(below e.currentTarget.stopDrag();)function like so:
UPDATE:
var copiedObjsArr:Array = [];
function onStop(e)
{
if ( e.target.dropTarget != null &&
e.target.dropTarget.parent == dest &&
e.currentTarget.name != "copy" )
{
//Code here remains same
//.......
//Keep collecting copied letters for further access in `reset()` function
copiedObjsArr.push(copy);
}
else if(e.currentTarget.name == "copy") //this is 'else if' (newly added)
{
var tarObject:MovieClip = e.currentTarget;
// These detects collision of dragged object with the trashCan
if(tarObject.hitTestObject(trashCan)) {
//These removes dragged object from the display list (not from the memory)
removeChild(tarObject);
tarObject = null; //to garbage
}
}
e.currentTarget.stopDrag();
}
And your reset() becomes like so:
function reset(event:MouseEvent):void
{
if(copiedObjsArr.length > 0)
{
//Traverse through all copied letters
for(var i:int = 0; i<copiedObjsArr.length; i++)
{
var objToRemove:MovieClip = copiedObjsArr[i];
removeChild(objToRemove);
objToRemove = null;
}
//Finally empty the array
copiedObjsArr = [];
}
}

adding Bitmaps to various frames in movieClip in as3

I have a movieClip and I want attach to this movieClip bitmaps. I want attach each bitmap to different frame of movieClip. I have tried something like this, but it is not working. It is for memory game I am creating.
for(var i : int = 0; i < cardList.length; i++){
var helpVar : int = cardList[i].pictureOfCard;
cardList[i].gotoAndStop(cardList[i].pictureOfCard+2);
var bitmap : Bitmap = new Bitmap(bitMapArray[helpVar].bitmapData.clone());
cardList[i].addChild(bitmap);
cardList[i].gotoAndStop(1);
}
You could simply load all Bitmaps and only show the one, that should be visible right now. e.g.
function ShowFrame(nr:int):void{
for(i:int = 0; i<bitMapArray.length; i++){
bitMapArray[i].visible = false;
}
bitMapArray[nr].visible = true
}
My AS3 skills are rusty, so this might need some syntax correction, but it works in theory.
var i :int = 0;
processNext();
function processNext():void
{
cardList[i].addEventListener(Event.FRAME_CONSTRUCTED, onFrameConstructed );
cardList[i].gotoAndStop(cardList[i].pictureOfCard+2);
}
function onFrameConstructed( e:Event ):void
{
cardList[i].removeEventListener(Event.FRAME_CONSTRUCTED, onFrameConstructed );
var helpVar : int = cardList[i].pictureOfCard;
var bitmap : Bitmap = new Bitmap(bitMapArray[helpVar].bitmapData.clone());
cardList[i].addChild(bitmap);
if( i < cardList.length - 1 )
{
i++;
processNext();
{
else
trace("All done");
}
After some time I discover that what I was trying to achieve is probably not possible in as3. The way I solved my problem is that everytime i gotoAndStop to frame I need have there bitmap I addChild(bitmap) and everytime I gotoAndStop other frame where there should not be bitmap I removeChild(bitmap) from movieClip. (So it is very similar aproach to makeing bitmap visible and invisible)