2013-09-16 226 views
0

我製作了一個戰艦遊戲,玩家和計算機輪流在10X10格子上發射炸彈。Cocos2d遊戲兩個場景之間的替換場景

我對iPhone使用cocos2d 2.0。

我有兩個場景,PlayerSceneAIScene

在Playerscene.m,我用

[[CCDirector sharedDirector] replaceScene:[CCTransitionSlideInR transitionWithDuration:1.0 scene:[AIScene sceneWithPositions:otherpos andHits:otherhits andOtherPositions: rects andOtherHits: prev]]]; 

玩家選擇了位置後,推進到AIScene。

這個效果很好。

然而,在AIScene,我採用的是

[[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:[CombatScene sceneWithParameters:OtherPositions andHits:OtherHits andOtherPositions: Positions andOtherHits: Hits]]]; 

回去,這是行不通的。遊戲保持在AIScene。

但是,我能夠觸摸屏幕,遊戲將閃光我PlayerScene與我剛剛放置的炸彈,並返回到AIScene

出了什麼問題?

最新信息:我在AIscene中添加了一個按鈕來觸發replaceScene事件,它的工作原理。但是,如果我將它添加到onEnter()方法的末尾,它不起作用。

+0

你確定你需要場景替換嗎?在這種情況下似乎很奇怪。 – Morion

回答

1

採取這一代碼出來的OnEnter方法,創建一個定時器或類似的東西,例如把這個進入的OnEnter方法:在AIScene

[self performSelector:@selector(replaceSceneAfterDelay:)withObject:nil afterDelay:3.0]; 

,仍然,創建方法:

-(void)replaceSceneAfterDelay 
{ 
    [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:[CombatScene sceneWithParameters:OtherPositions andHits:OtherHits andOtherPositions: Positions andOtherHits: Hits]]]; 
} 

請注意3秒。太方便了,試試你自己的價值。

以下文字摘自「學習iPhone 5和iPad 2 Cocos2d遊戲開發」一書。由Steffen Itterheim撰寫。 鏈接:http://www.amazon.com/Learn-cocos2d-Game-Development-iOS/dp/1430238135

「場景和記憶:

請記住,當你與另一個替換一個場景,新場景被載入 到內存中的舊場景的內存被釋放之前。」

2

您不能在onEnter方法(也不是init方法)中替換場景。換句話說,你不能從當前仍在替換另一場景的場景中調用replaceScene。

您可以安排一次選擇器,然後從計劃選擇器調用replaceScene。只有在場景被替換之後,這種替換纔會發生。