我们什么时候应该使用 pushScene 和 replaceScene?什么时候、pushScene、replaceScene

2023-09-06 10:27:12 作者:重庆娃儿√太嘴

我 cocos2d,我正在使用 pushScene 和 replaceScene 移动到下一个场景.但是,我很困惑我们应该使用哪个?

I cocos2d, I am using pushScene and replaceScene to move to next scene. But, I am confused which we should use?

当我在某些地方使用 replaceScene 时,应用程序崩溃并出现类似

When I am using replaceScene in some places app is crashing and giving errors like

-[UITextView length]: unrecognized selector sent to instance 0x842a750  
 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UITextView length]: unrecognized selector sent to instance 0x842a750'  

但是,在我的程序中,我没有将长度传递给 UITextView.我的程序是这样的在scene1 我有UITextView,我用scene2 替换这个场景.那么

But, in my program I am not passing length to UITextView. My program is in this way In scene1 I have UITextView, and I am replacing this scene with scene2. Then

-(id)buttonPressed:(id)sender
{
    [description removeFromSuperview];  // It is the textView(description)
    CCScene *Scene = [CCScene node];
    CCLayer *Layer = [scene2 node];

    [Scene addChild:Layer];

    [[CCDirector sharedDirector] setAnimationInterval:1.0/60];
    [[CCDirector sharedDirector] replaceScene: Scene];
}  

但是,当我在存在 replaceScene 的情况下使用 pushScene 时,它​​运行良好.请澄清我应该在哪些情况下使用哪一个?

But, when I am using pushScene in presence of replaceScene it is working good. Please clarify me which one should use in which cases ?

谢谢

推荐答案

几乎在所有情况下都需要使用 replaceScene.pushScene 方法将前一个场景保存在内存中,这很可能是它没有崩溃的原因.关于该场景的某些内容不太正确,当它在 replaceScene 之后释放时,就会发生崩溃.与cocos2d的替换场景系统无关.

You will want to use replaceScene in almost all cases. The pushScene method keeps the previous scene in memory, which is most likely why its not crashing. Something about that scene isn't quite right and when it deallocates after a replaceScene, the crash occurs. It has nothing to do with cocos2d's replace scene system.

scene1 中的 UITextView 可能发布得太频繁了.如果它是作为自动释放对象创建的,请不要向它发送释放消息.

Your UITextView in scene1 is probably released too often. If its created as an autorelease object, don't send it the release message.