Know when CCScene is shown after scene on top of stack is popped - cocos2d-x

Using `cocos2d-x, say I am on Scene 1 and I add a new scene (Scene 2), but I later pop that scene:
CCDirector *pDirector = CCDirector::sharedDirector();
pDirector->popScene();
How do I know when Scene 1 has appeared again? Is there any sceneDidAppear method or something similar?

CCScene is derived from CCNode, therefore you can override onEnter() and onExit() method to know when a scene is pushed or poped.

Adding to #Vikas Patidar answear : you can override onEnterTransitionDidFinish() - this will fire after the transition animation is complete. Docs : link

Related

Box2d MovieClip to original position

I want to try a simple task where if i move a object inside the world and then press a button it should go back to its original position , but its not working , below is the code i am using - the file is here - http://www.fastswf.com/yAnIvBs (when i remove the event listener)
with event listener - http://www.fastswf.com/rpYsIt8
////////========================
stop();
var startXPos:Number = level1WorldObj.box1.x;
var startYPos:Number = level1WorldObj.box1.y;
function areaS(e:Event) {
level1WorldObj.box1.y= startYPos;
level1WorldObj.box1.x= startXPos;
level1WorldObj.box1.removeEventListener(Event.ENTER_FRAME, areaS);
}
but1.addEventListener(MouseEvent.CLICK,nClick3);
function nClick3(event:MouseEvent):void{
level1WorldObj.box1.addEventListener(Event.ENTER_FRAME, areaS);
level1WorldObj.box1.y= startYPos;
level1WorldObj.box1.x= startXPos;
}
/////////////////======================
Now i want to be able to do it many time so i kept the variables that detect the initial x, y as global ...
Here you can see how it behaves in debugdraw mode , strangely only the clip moves not the actual body - http://www.fastswf.com/-Ijkta4
Can some one please guide me here ...
Thanks in advance ...
Jin
The graphics that you see (box1) aren't related to the physical object behind the scenes - you're currently only moving the graphics not the object itself.
You need to use either SetPosition() or SetTransform() on the b2Body of the object
Edit 07/7
As you're using the Box2D World Construction Kit, I took a look at the source code (available here: https://github.com/jesses/wck). The main class seems to be BodyShape (https://raw.githubusercontent.com/jesses/wck/master/wck/BodyShape.as).
Looking through it, you should be able to access the b2Body directly. If it's null (which is probably the source of the TypeError that you're getting, then you haven't called createBody(), which is what actually takes all of your properties as creates the physical object behind the scenes.
Once you have a b2Body, if you want to position it based on the graphics, there's a function syncTransform() to do just that.
You should turn on debugDraw on your World class to make it easier to see what's going on in the background. NOTE: this needs to be done before calling create()
I was able to find solution to this problem , i found the starting point by using this -
trace(level1WorldObj.box1.b2body.GetPosition().x);
trace(level1WorldObj.box1.b2body.GetPosition().y);
then once i had the position manually i took down the coordinates and used the below code ....
level1WorldObj.box1.b2body.SetTransform(new V2(-2, 2),0 );
Thanks #divillysausages for all the help ...
Regards

condition to swap two sprite in c++ for cocos2d?

I am developing a game in cocos2d-x. In that game I am having two sprites, When i click on a sprite one after another it should swap.I don't know the code for that please help.I created the sprite using following code.
CCSprite *splash=CCSprite::create("misc_textur111.jpg");
splash->setPosition(ccp(500,300));
this->addChild(splash,1);
CCSprite *splash1=CCSprite::create("misc_textur222.jpg");
splash1->setPosition(ccp(300,600));
this->addChild(splash,1)
Now I need to swap the position of both the images.please help.
You can do it like:
CCPoint position(splash->getPosition());
splash->setPosition(splash1->getPosition());
splash1->setPosition(position);
I'd suggest, if you can, upgrading to a newer release of Cocos2d-x. Version 2.23 or even better, version 3.v. Dragging sprites around is trivial with the new EventDispatcher You can create a Listener for each Sprite.
Example:
//Create a "one by one" touch event listener (processes one touch at a time)
auto listener1 = EventListenerTouchOneByOne::create();
// When "swallow touches" is true, then returning 'true' from the onTouchBegan method will "swallow" the touch event, preventing other listeners from using it.
listener1->setSwallowTouches(true);
//Trigger when moving touch
listener1->onTouchMoved = [](Touch* touch, Event* event){
auto target = static_cast<Sprite*>(event->getCurrentTarget());
//Move the position of current button sprite
target->setPosition(target->getPosition() + touch->getDelta());
};
Check out the Cocos2d-x Wiki: http://cocos2d-x.org/wiki

changing skins in flash as3

hello im making game and im trying to add skins for layers in this game so i made a vovieclip
and inside it there are skins http://prntscr.com/2as4mp and inside that when it be clicked there are http://prntscr.com/2as4pm i want it that when i ress this button the skin will change in the menu i tried this
select_2.addEventListener(MouseEvent.CLICK, open1243)
function open1243(event:MouseEvent){
bk = 1
gotoAndStop(2)
}
stop ();
well that dint work um where there is goandsto 2 that goes to menu thats it nothing works pls help
In your event listener, you're not specifying that you want select_2 to gotoAndStop, rather, it will apply that method call to whatever this is in the scope of that method. I'm guessing, at that point, it's the stage.
select_2.addEventListener(MouseEvent.CLICK, open1243);
function open1243(event:MouseEvent){
bk = 1
select_2.gotoAndStop(2);
}
You should probably make sure your event listener could be used for any object similar to select_2, in which case, I would suggest you change that line of code to:
MovieClip(event.target).gotoAndStop(2)

cocos2dx/coco2d layer transition

I hope to pop down a score panel when players win and pass the gate.
Normally it will pop down score board.
I think the best way is to use layer and pull down it.
But I only get the transition of scene, just wonder is there any way for layer transition?
Did not see an equivalent of CCTransitionScene :CCScene for CCLayer but layers can runActions using which we can bring out most of the animations/transitions.
Here is what I do in such situations but I guess you are thinking of the same thing. Nevertheless,
1.Create a layer and add it as a child at a position outside of your screen frame.
2.Then use CCMoveTo to move it to the desired location when you want to pull it down.
I have done something similar in the past.
Display your layer offscreen
i.e setposition(0, CCDirector::sharedDirector()->getWinSize().height*1.5f);
create an action to move it onscreen (I like to use CCEaseSineOut)
you can also use a callfunc to call a function when it has finished its animation
scoreLayer->runAction( CCSequence::create( CCEaseSineOut::create(CCMoveTo::create(1.0f, ccp(0, 0-_screenHeight*1.5f))), CCCallFunc::create(this, callfunc_selector(MainLayer::scorefinishedMove)), NULL));
Note: that function might need some fixes to ending brackets etc. And you may want to seperate out some of those actions rather than putting the initialization right in the runAction function
For layer transition you can do this:
CCScene* newScene = CCTransitionCrossFade::create(.5f,Layer2::scene());
CCDirector::sharedDirector()->pushScene(newScene);
In Layer2.cpp
CCScene* Layer2::scene()
{
CCScene* scene = CCScene::create();
CCLayer* layer = new Layer2();
scene->addChild(layer,1);
return scene;
}

Action Script - how to go to another scene from movieClip?

I'm trying to go to another scene by clicking a button witch is placed in a mc and I got an error.
This is my code below
Play_btn.addEventListener(MouseEvent.CLICK,gotoScene2);
function gotoScene2(evt:MouseEvent){
movClip.gotoAndPlay(1,"Scene 2");
}
(this code is placed inside a movieClip witch is instantiated like "movClip", at frame 260 - may be this help to understand better).
And this is the erorr i get:
Symbol 'preloader26',Layer'Layer9',Frame 260,Line 4 1120:Access of undefined property movClip
I hope anyone can help me...thanks very much!
Try to get the parent of the object , like this:
MovieClip(this.parent).gotoAndPlay(1,"Scene 2");