SpriteKit how to have a label on screen change during runtime - sprite-kit

I have added a label to my scene with the following code:
SKLabelNode *myLabel = [SKLabelNode labelNodeWithFontNamed:#"Chalkduster"];
myLabel.name = #"scoreCounter";
myLabel.text = ;
myLabel.fontSize = 30;
myLabel.position = CGPointMake(50, 25);
[self addChild:myLabel];
How can i have the label display a number that will change when my objects reach a certain x point? Or is there a way to have the label change whenever a node is removed from its parent. Basically, whenever one of my objects reach 360 on the x axis, i want the label to go up one, to represent score. How can i do this?

since you have named this label,you can find this label by it's name,and change the text.
SKLabelNode *node = [SKLabelNode labelNodeWithFontNamed:#"scoreCounter"];
node.text = #"other string";

Related

How to create square SKLabelNode in Sprite kit

How am I able to create an SKLabelNode and set the width and height of it. I have thought about just making a sprite image with the word on and then position that sprite but I don't believe that this is the best way of doing this.
Image:
http://i.imgur.com/dAP6yeT.png
Thanks!
I think that the best way of doing what you are asking is doing what you said. Just make an image of the word. This makes it to where you can also use it as a button. It might subtract some fps, but only if you have about 50+ nodes in your scene. Which is not likely since most people put a label on the menus. But if you do want to resize the SKLabelNode just resize the tex like this. label.fontsize = 20
The following code will add a node with the text you supply with a box around it. The border will be a rectangle that corresponds to the size of the text. If you want it square, just adjust the borderPath points to only use the labelNode.frame.size.width/2 for both horizontal and vertical spacing. Adjust the +/- 10 to the amount of spacing around the text you want.
//create the wrapper node
float initialWidth = 50;
float initialHeight = 50;
SKSpriteNode *parentNode = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:CGSizeMake(initialWidth, initialHeight)];
//create the label node
NSString *FontName = [UIFont boldSystemFontOfSize:10].fontName;
SKLabelNode *labelNode = [SKLabelNode labelNodeWithFontNamed:FontName];
labelNode.text = #"some text";
labelNode.fontColor = [UIColor blackColor];
labelNode.position = CGPointMake(0, -labelNode.frame.size.height/4);
//create the border node
SKShapeNode *borderNode = [SKShapeNode new];
UIBezierPath* borderPath = [[UIBezierPath alloc] init];
[borderPath moveToPoint:CGPointMake(-labelNode.frame.size.width/2 -10, -labelNode.frame.size.height/2 -10)];
[borderPath addLineToPoint:CGPointMake(labelNode.frame.size.width/2 +10, -labelNode.frame.size.height/2 -10)];
[borderPath addLineToPoint:CGPointMake(labelNode.frame.size.width/2 +10, labelNode.frame.size.height/2 +10)];
[borderPath addLineToPoint:CGPointMake(-labelNode.frame.size.width/2 -10, labelNode.frame.size.height/2 +10)];
[borderPath addLineToPoint:CGPointMake(-labelNode.frame.size.width/2 -10, -labelNode.frame.size.height/2 -10)];
borderNode.path = borderPath.CGPath;
borderNode.lineWidth = 10.0;
[borderNode setStrokeColor:[UIColor blackColor]];
//add it all together and present it
[parentNode addChild:borderNode];
[parentNode addChild:labelNode];
parentNode.position = newShapeScenePoint;
parentNode.size = CGSizeMake(labelNode.frame.size.width +10, labelNode.frame.size.height +10);
[myScene addChild:parentNode];
You can move the entire thing with
parentNode.position = CGPointMake(someNewX, someNewY)
You can resize the entire thing (including the text) with
float bigger = 3.0; //or whatever size you want
parentNode.xScale = bigger;
parentNode.yScale = bigger;

Drawing Trail Lines for SKSpriteNode

I have a custom LineBall class as shown below:
#implementation LineBall
-(instancetype) init {
self = [super initWithImageNamed:LINE_BALL_IMAGE];
self.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:self.size.width/2];
self.physicsBody.categoryBitMask = BBPhysicsCategoryLineBall;
self.name = #"lineBall";
self.physicsBody.friction = 0.0f;
self.physicsBody.restitution = 1.0f;
self.physicsBody.linearDamping = 0.0f;
self.physicsBody.allowsRotation = NO;
self.physicsBody.dynamic = YES;
return self;
}
Later on I add this to the GameScene and it works as expected. The problem is that now I want to draw lines wherever the LineBall travels. How can I do that?
Here's one way to draw a trail behind a moving ball.
In the didSimulatePhysics method:
Store the ball's positions over time in a mutable array. Insert at index = 0.
Remove the oldest (last) array element if the number of elements exceeds the max trail size
Create a mutable CGPath by looping over the elements of the array using CGPathAddLineToPoint to connect each point
Remove the old trail from the scene if it exist
Create the ball's trail by creating an SKShapeNode from the path using shapeNodeWithPath
Add the SKShapeNode to the scene

SpriteKit 1 Dimensional Movement

I'm using apple's Sprite Kit and I need to move a SKSprite Node in horizontal movement only. I want the rest of the physics to apply but only in the horizontal component.
Context: This is for an object supposedly on a slider that can bounce back and forth. I have everything done but if it is hit the end the wrong way it simply floats off vertically, how can I simply make it ignore all forces in the vertical direction.
By putting the node's position back at the desired Y coordinate every frame after physics has been simulated:
-(void) didSimulatePhysics
{
CGPoint pos = horizontalMoveNode.position;
pos.y = fixedVerticalPosY;
horizontalMoveNode.position = pos;
}
Add this method to your scene class and apply it to whichever node(s) you want to lock in at a given Y coordinate.
You could use constraints for this purpose. I made a short sample with a node that only moves in a fixed X range and never leaves a specified Y position:
SKSpriteNode* node = [SKSpriteNode node];
node.color = [SKColor greenColor];
node.size = CGSizeMake(20, 20);
SKRange* rangeX = [[SKRange alloc] initWithLowerLimit: 100 upperLimit: 400];
SKRange* rangeY = [SKRange rangeWithConstantValue: 100];
SKConstraint* positionConstraint = [SKConstraint positionX: rangeX Y: rangeY];
NSArray* constraintArray = [NSArray arrayWithObject: positionConstraint];
node.constraints = constraintArray;
[self addChild: node];
This method is from SKAction class to move objects only on Horizontal or X-axis:-
[mySpriteNode runAction:[SKAction moveToX:260 duration:0.5]];
I hope this work's for you.

iOS - How to draw a circle with a number/character in it?

I would like to create a UIView which would show a colored circle with a number or character in it. I have seen examples to create a circle, but how would I accomplish this?
I would want to place multiple of these views on the screen and connect them with lines.
Any suggestions?
// Swift, iOS8
var letterLabel = UILabel(frame: CGRect(x: 50, y: 0, width: 100, height: 100))
letterLabel.layer.cornerRadius = 50
letterLabel.textAlignment = NSTextAlignment.Center
letterLabel.layer.borderWidth = 10
letterLabel.layer.backgroundColor = UIColor.blueColor().CGColor
letterLabel.font = UIFont(name: "Hoefler Text", size:75.0)
letterLabel.text = "A"
If you already know how to draw a circle, then you've got half of it solved :) Let's say the circle is drawn in a UIView called circleView.
Now you want to create a UILabel to draw the letter in.
UILabel* circleLabel = [[UILabel alloc] init];
[circleLabel setText:#"letter goes here"];
And now you want to put the label on top of the circleView, so you add it as a subview:
[circleView addSubview:circleLabel];
At this point, depending on the size of circleView and the font size of circleLabel, the label may or may not be in the center of the circle. Use the setFrame function on the circleLabel to adjust its position within the circleView.

How to give shadow effects to label items in cocos2D?

I have moving sprites in my game and have added labels on it (CCLabelTTF). The labels have characters like A, B, C etc. I want to give a shadow effect to the contents of labels so that they are properly visible.
in .h file i have
CCLabelTTF *label;
and in .m i have set its position and color. "target" is the sprite on which there are labels.
target=[CCSprite spriteWithFile:[NSString stringWithFormat:#"balloon%d.png",enType] rect:CGRectMake(0, 0, 100, 119)];
label = [[CCLabelTTF alloc] initWithString:alphabetValue dimensions:CGSizeMake([target contentSize].width, [target contentSize].height)
alignment:UITextAlignmentCenter fontName:#"verdana" fontSize:30.0f];
//LABEL POSITION HERE
label.position = ccp(55,30);
label.color = ccc3(60,60,60);
[target addChild:label z: 10];
Now i want to give a shadow effect...how can i do so?
Add the same label twice, one time slightly bigger. The shadow label should be created first to make it appear behind the actual label, or use the z property.
CGSize size = CGSizeMake([target contentSize].width, [target contentSize].height);
labelShadow = [[CCLabelTTF alloc] initWithString:alphabetValue
dimensions:size
alignment:UITextAlignmentCenter
fontName:#"verdana" fontSize:30.0f];
labelShadow.position = ccp(55+2,30+2); // slightly offset
labelShadow.color = ccc3(10,10,10);
[target addChild:labelShadow z:10];
label = [[CCLabelTTF alloc] initWithString:alphabetValue
dimensions:size
alignment:UITextAlignmentCenter
fontName:#"verdana" fontSize:30.0f];
label.position = ccp(55,30);
label.color = ccc3(60,60,60);
[target addChild:label z:10];
You can also experiment with slightly scaling the labelShadow up, or increasing its fontSize.
Note: it's not going to create a soft (blurred) shadow. For that you could use the texture filter methods available here.