我寫此代碼爲GAME OVER了現場我有一個遊戲:兒童SkShapeNode定位混亂
#import "GameOverScene.h"
#import "SharedInfo.h"
@implementation GameOverScene
-(void)didMoveToView:(SKView *)view {
/* Setup your scene here */
[self setupView];
}
-(void)showGameEndingWithGameInformation:(NSDictionary *)gameEndingInformation{
}
-(void)setupView{
SKLabelNode *GOTitle = [SKLabelNode labelNodeWithFontNamed:@"Generica Bold"];
GOTitle.fontSize = 40.f;
NSString* text = [NSString stringWithFormat:@"GAME OVER"];
[GOTitle setText:text];
GOTitle.position = CGPointMake(CGRectGetMidX(self.frame), self.frame.size.height- GOTitle.frame.size.height*1.5);
[GOTitle setFontColor:[[SharedInfo sharedManager]colorFromHexString:@"#2EB187"]];
[self addChild: GOTitle];
SKLabelNode *replayButton = [SKLabelNode labelNodeWithFontNamed:@"Quicksand-Bold"];
replayButton.fontSize = 25.f;
NSString* replayText = [NSString stringWithFormat:@"Play Again"];
[replayButton setText:replayText];
replayButton.name = kGOSceneReplayButton;
replayButton.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame)- self.frame.size.height/5);
[replayButton setFontColor:[SKColor whiteColor]];
SKShapeNode *bgNode = [SKShapeNode shapeNodeWithRectOfSize:replayButton.frame.size];
[bgNode setFillColor:[UIColor redColor]];
[replayButton addChild:bgNode];
[self addChild:replayButton];
NSLog(@"replay dimensions: %@",NSStringFromCGRect(replayButton.frame));
SKLabelNode *returnButton = [SKLabelNode labelNodeWithFontNamed:@"Quicksand-Bold"];
returnButton.fontSize = 25.f;
NSString* returnText = [NSString stringWithFormat:@"Return To Main Menu"];
[returnButton setText:returnText];
returnButton.name = kGOSceneReturnToMainButton;
returnButton.position = CGPointMake(CGRectGetMidX(self.frame), replayButton.position.y -self.frame.size.height/7 );
[returnButton setFontColor:[SKColor whiteColor]];
[self addChild:returnButton];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *sprite = [self nodeAtPoint:location];
NSLog(@"sprite name: %@",sprite.name);
if ([sprite.name isEqualToString:kGOSceneReturnToMainButton]||[sprite.name isEqualToString:kGOSceneReturnToMainButton]) {
//return to main menu or retry
[self.gameEndingSceneDelegate goToScene:sprite.name withOptions:nil]; //Sort out the options later on.
}
}
@end
當我雖然運行它,我得到這個:
有兩個問題我很困惑。首先,爲什麼我在場景中有8個節點,我真的應該有4個節點?我認爲節點翻倍,但這只是一個猜測。
更令人困惑的問題是紅色的SKShapeNode定位。我讀過縮放父節點可能會給子SKShapeNode帶來問題,但我沒有縮放任何東西。另外,爲什麼它將我的紅色矩形放置在一個隨機位置(它不是父級的中間,或者與底部相對應)。
非常感謝所有的幫助提前。
UPDATE 1:所以下面的建議,我檢查了我的方法是否被調用兩次,從而創建重複。沒有運氣,因爲它只被調用一次。神祕感依然強勁!
至於定位有心計,我改變了代碼稍微設置紅色矩形的位置,它的父節點匹配:
SKLabelNode *replayButton = [SKLabelNode labelNodeWithFontNamed:@"Quicksand-Bold"];
replayButton.fontSize = 25.f;
NSString* replayText = [NSString stringWithFormat:@"Play Again"];
[replayButton setText:replayText];
replayButton.name = kGOSceneReplayButton;
replayButton.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame)- self.frame.size.height/5);
[replayButton setFontColor:[SKColor whiteColor]];
SKShapeNode *bgNode = [SKShapeNode shapeNodeWithRectOfSize:replayButton.frame.size];
[bgNode setFillColor:[UIColor redColor]];
[self addChild:replayButton];
bgNode.position = replayButton.position;
[replayButton addChild:bgNode];
但在更新後,我得到這個:
萬一有幫助,這是我做的呈現什麼場景:
SKView * skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = YES;
scene = [GameOverScene sceneWithSize:self.view.frame.size];
[(GameOverScene*)scene setGameEndingSceneDelegate:self];
[(GameOverScene*)scene setScaleMode: SKSceneScaleModeAspectFill];
[(GameOverScene*)scene showGameEndingWithGameInformation:self.gameEndingInfo];
// Present the scene.
[skView presentScene:scene transition:sceneTransition];
而且,這是我的NSLog輸出:
重播尺寸:{{221,91},{127,25}
我有一種感覺,因爲我把我的場景的setScaleMode,它會變得很奇怪,但沒有別的東西是不尋常的,所以不知道該怎麼做。我想也許只是爲我的標籤創建一個圖像,並將SKLabelNode更改爲SKSpriteNode並設置圖像,所以我跳過添加紅色矩形作爲標籤節點的背景。我想添加矩形的原因實際上是爲了提供更大的命中目標,因此如果有人知道更簡單,更直接的方式,我會非常感激。
UPDATE 3:
我還嘗試設置矩形的位置以匹配父標籤節點的:
bgNode.position = [replayButton convertPoint:CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame)- self.frame.size.height/5) fromNode:self];
矩形在同一地點作爲最後的更新結束(所有一直到屏幕的右側)
@ originaluser2我試了兩個,setupView只被調用一次,並且更新後的代碼發生了怪異的事情!我已經使用代碼更新更新了原始帖子。這很奇怪! – Septronic
@ originaluser2當添加SKLabelNode時,在iOS 9模擬器中,節點數量增加2。所以,這不是OP的錯誤。 – Whirlwind
@Whirlwind事情是我在設備上運行它。 – Septronic