Int into label SpriteKit SKView, hl - swift

I just want to know if is it possible to make a label or anything else who will be a integer and to display it to my gameview.
var pvElf = SKLabelNode(fontNamed:"Chalkduster")
pvElf.text = "100";
pvElf.fontSize = 45;
pvElf.position = CGPoint(x:370, y:600)
pvElf.zPosition = 2
addChild(pvElf)
I want pvElf to be an int but I don't find an SKIntNode or anything else. Because I want to decrease this number when I do an action. But if this is a label I can't.
Thank you very much !

Simply update the label's text with the Int (converted to a String).
let someInt = 4
pvElf.text = "\(someInt)"

Related

Adding Ui labels values

I have been attempting to Add the values of some UI labels in Xcode, but with '+' there is an error and I have not found any other way yet to have it work.
Storyboard
ViewController.Swift
It‘s not clear in your question but I think you’re trying to change the text displayed by the UILabel. If this is the case, you can do this as following
myLabel.text = “myText”
or
myLabel.text += “Test”
You have to be more specific. I don’t really get what you want to add so I will assume.
If you want to add the Strings the UILabels contain, you should do as Bruce said above.
If you want to add the two strings into a variable you should do this.
Var outcome = label1.text + label2.text
The outcome would be the string of label one plus the string of label two connected.
Now if the labels contain numbers and you want to add those numbers you should do this. (Assuming they do contain Integers)
let num1 = label1.text.toInt()
let num2 = label2.text.toInt()
let outcome = num1 + num2
In your case it would be....:
Let sumOfIntegers = MathCriAValue.text.toInt() + MathCriBValue.text.toInt() + MathCriCValue.text.toInt() + MathCriDValue.text.toInt()

Most efficient way to take max width of multiple views and set that as width of all said views?

What is the most efficient way (least amount of lines of code) to take the widths of a list of views, find the greatest value, and set that value as the width of those views? This example uses if/else for 2 views (to help illustrate what I'm trying to accomplish) but what if there were 3 or 30 views?
let label1Width = label1.bounds.width
let label2Width = label2.bounds.width
if label1Width > label2Width {
label2.frame.size.width = label1Width
}
if label2Width > label1Width {
label1.frame.size.width = label2Width
}
You can use max(by:) to find the element of an array with a maximum value where you define the comparison function yourself. In this case, you would use .frame.size.width for comparison.
Once you find the maximum width, you can just iterate through all your labels and assign the maximum width to each label.
Below code has been tested in a playground and is working as expected.
let labels = [UILabel(),UILabel(),UILabel(),UILabel(),UILabel(),UILabel(),UILabel(),UILabel(),UILabel(),UILabel()]
labels[3].frame.size.width = 100
labels[5].frame.size.width = 200
let maxWidth = labels.max(by: {$0.0.frame.size.width < $0.1.frame.size.width})!.frame.size.width
labels.forEach{
$0.frame.size.width = maxWidth!
}
Print the width values for testing:
labels.forEach{
print($0.frame.size.width) //prints 200 for each element of the array
}
Sounds like you're looking for a max function, eg:
let maxWidth = max(label1.bounds.width, label2.bounds.width)

Apps Script: setting one formula for a whole column

I'm working on an Apps Script project for Sheets and I don't know if it's because I just never really worked with Sheets or Excel, but I don't know how to set a formula for a whole column through code.
var cell = sheet.getRange([i], 2);
var cell2 = sheet.getRange([i], 1);
var inhoud = cell2.getValue();
cell.setFormula("=(" + inhoud/86400000 + "DATE(1970,1,1)");
I want every B of a row to do something with the A of that same row. In the sheet self it's easy to just "drag the function down", to make it apply to every row, but I don't know how to get that to work in code as I can't use A2, for example, or A2:A30. Part of the problem may be that it's in a for loop:
var subsie = [];
for (i = 0; i < subscriptions.length; i++) {
var subscription = subscriptions[i];
creationdate = subscription.creationTime;
if (subscription.plan.planName == 'ANNUAL' && subscription.renewalSettings.renewalType == 'AUTO_RENEW') {
subsie.push([creationdate, ' ', subscription.plan.planName]);
Logger.log(subsie);
var cell = sheet.getRange([i], 2);
var cell2 = sheet.getRange([i], 1);
var inhoud = cell2.getValue();
cell.setFormula("=(A1:A100/86400000) + DATE(1970,1,1)");
} }
sheet.getRange(sheet.getLastRow()+1, 1, subsie.length, subsie[0].length).setValues(subsie);
The actual goal is to convert the epoch values of A into dates, which I tried in a lot of different ways but turned out to be more difficult than I expected. This was the only formula that seemed to work for my output, which was like this: 1433235478178. How can I make this code work? Thanks in advance!
Solved it :)
creationdate = (subscription.creationTime/86400000)+25567;

Swift: how to change a integer variable name within a method and reassign its value

I am a complete beginner at Swift/programming and am making a noughts and crosses app as part of an online course. I wanted to do this on my own before I saw the solution so my logic may be a bit weird.
I have a function which is called each time a button is pressed (there are 9, one for each square). The function acts to:
i) update the number of turns (which allows me to see who's go it is)
ii) change the picture to X or O
iii) deactivate the button after each turn
iv) calculate is anyone has won by changing the value of that squares potential lines. The numbers I have chosen to do this are 3 and 4 (for noughts and crosses), hence the c = 3 or 4 below. This means that if any winning line adds up to 9 or 12, the game stops as someone has won.
I have 9 variables (Int) that hold the score for each square - they are a1o, a2o, a3o, b1o... i.e all end in "o". In the function I want to add the string of "a1" in front of the "o", meaning each button is only relevant to itself with only one line of code above the function (within the button parentheses).
The function looks like this at the moment; calling it by: button(a1, buttonValue: "a1")
func button(buttonName: UIButton, buttonValue: NSString) -> String {
let a = buttonName
var b = buttonValue
b = (b as String) + "o"
print(b)
// the above prints "a1o", the name of the variable, but it is a string...
index += 1
print("Go number \(index)")
if index % 2 == 0 {
// show a nought
a.setImage(UIImage(named: "nought.png"), forState: UIControlState.Normal)
c = 3
// the above line doesn't work as its a string, but I am attempting to set the squares value to 3 (i.e. a1o = 3)
winner()
a.userInteractionEnabled = false
} else {
// show a cross
a.setImage(UIImage(named: "cross.png"), forState: UIControlState.Normal)
c = 4
// the above line doesn't work as its a string
winner()
a.userInteractionEnabled = false
}
return "done"
}
What I can't figure out is how to change the common stem of the variable *a1*o by not making it a string, or if I do, how I convert it back to a variable.
I am struggling with definitions and as a result looking for an answer has been hard. The variable a1o is an integer, but how do I refer to a1o itself?
Thank you in advance,
Sam
It is not possible to access variables dynamically by name like you want to do. However, there are better ways to accomplish the same goal. How about a two-dimensional array?
var scores: [[Int]] = [[0,0,0], [0,0,0], [0,0,0]]
scores[0][1] = 4 //this is the new version of "a2o = 4"
Unlike with variable names, the indices to the array ("0" and "1" in this case) can be dynamically chosen. Or, if you really want to keep your buttons' values exactly the same, you could use a dictionary:
var scores: [String: Int] = [:]
scores[buttonValue as String] = 4

Random positioning in Swift

How would I create an arc4random thing? Like in previous versions I've seen
int blank = arc4random(5)
But how would I do it in the most recent version of Xcode?
let maxNumber = 10
let randomIndex = Int(arc4random_uniform(UInt32(maxNumber)))