Make Touch-Area for Sklabelnode Bigger for Small Characters

SKLabelNode text with two different fonts and colour. How is this possible?

You cannot set two fonts to the same SKLabelNode instance. Instead you can write subclasses to create a custom node which contains multiple SKLabelNodes with different font sizes. For example, Your scoreLabel can be an instance of the following class.

class ScoreLabel : SKNode
{
var label : SKLabelNode!
var scoreLabel : SKLabelNode!

var score : Int = 0 {
didSet
{
scoreLabel.text = "\(score)"
}
}

override init() {
super.init()
label = SKLabelNode(text: "Score : ")
label.position = CGPointMake(0, 0)
label.fontSize = 20
addChild(label)

scoreLabel = SKLabelNode(text: "\(0)")
scoreLabel.position = CGPointMake(label.frame.size.width , 0)
scoreLabel.fontSize = 25
addChild(scoreLabel)
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

}

Using ScoreLabel class

let scoreLabel = ScoreLabel()
scoreLabel.position = CGPointMake(100, 300)
scoreLabel.score = 10
self.addChild(scoreLabel)

The two labels in ScoreLabel acts as a single SKNode from the outside.
SKActions can be executed on the ScoreLabel and it will affect both the child label nodes. For instance

    scoreLabel.runAction(SKAction.scaleTo(2.0, duration: 2.0))

This will scale both labels together as a single unit.

How to make text appear on button? -SpriteKit

You always need to add zPosition to your sprites and labels. Otherwise sometimes they will appear and other times they may appear behind the background

//creating the start game programmatically. 
let dw_startButton = SKSpriteNode()
dw_startButton.zPosition = 1
dw_startButton.name = "dw_startbutton"
dw_startButton.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
self.addChild(dw_startButton)

//Instructions Button
let dw_selector = SKSpriteNode()
dw_selector.name = "dw_selector"
dw_selector.zPosition = 1
dw_selector.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
self.addChild(dw_selector)

//Starting text
let startText = SKLabelNode(text: "Play!")
startText.fontColor = UIColor.white
startText.position = CGPoint(x: 50, y: 50)
startText.fontSize = 45
startText.fontName = "Helvetica-Bold"
startText.verticalAlignmentMode = SKLabelVerticalAlignmentMode(rawValue: 1)!
startText.name = "dw_startbutton"
startText.zPosition = 2
dw_startButton.addChild(startText)

//instructions text
let startTexts = SKLabelNode(text: "Insturctions")
startTexts.fontColor = UIColor.white
startTexts.position = CGPoint(x: 0, y: 0)
startTexts.fontSize = 20
startTexts.fontName = "Helvetica-Bold"
startTexts.verticalAlignmentMode = SKLabelVerticalAlignmentMode(rawValue: 1)!
startTexts.name = "dw_selector"
startTexts.zPosition = 2
dw_selector.addChild(startTexts)

//new button
let newButton = SKSpriteNode(imageNamed: "button_back")
newButton.zPosition = 1
newButton.name = "button1"
newButton.position = CGPoint(x: 100, y: 100)
self.addChild(newButton)

let buttonText = SKLabelNode(text: "Play")
buttonText.fontColor = .white
buttonText.fontSize = 20
buttonText.fontName = "Helvetica-Bold"
buttonText.verticalAlignmentMode = .center
buttonText.horizontalAlignmentMode = .center
buttonText.name = "dw_selector"
buttonText.zPosition = 2
newButton.addChild(buttonText)

Setting up buttons in SKScene

you could use a SKSpriteNode as your button, and then when the user touches, check if that was the node touched. Use the SKSpriteNode's name property to identify the node:

//fire button
- (SKSpriteNode *)fireButtonNode
{
SKSpriteNode *fireNode = [SKSpriteNode spriteNodeWithImageNamed:@"fireButton.png"];
fireNode.position = CGPointMake(fireButtonX,fireButtonY);
fireNode.name = @"fireButtonNode";//how the node is identified later
fireNode.zPosition = 1.0;
return fireNode;
}

Add node to your scene:

[self addChild: [self fireButtonNode]];

Handle touches:

//handle touch events
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];

//if fire button touched, bring the rain
if ([node.name isEqualToString:@"fireButtonNode"]) {
//do whatever...
}
}

Deallocate SKScene after transition to another SKScene in SpriteKit

A similar problem was faced by the person who asked this question.

When asked whether he was able to solve it, they said:

Yes, I did, there wasn't anything I could do about it from the scene
or Sprite Kit for that matter, I simply needed to remove the scene and
the view containing it completely from the parent view, cut all its
bonds to the other parts of the system, in order for the memory to be
deallocated as well.

You should use separate views for each of your scene and transition between these views. You can follow these steps to make it look natural:

1 - At the point you want to transition from one scene to the other, take a snapshot of the scene using the following code:

UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, scale);
[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Then, add this image as a subview of the SKView of the current scene, and remove the scene.

2 - Initialise the new scene on another view, and transition between the two views using UIView animation.

3 - Remove the first view from it's superview.



Related Topics



Leave a reply



Submit