How would I save "coins" in my game in Swift SpriteKit? - xcode

I have this game where the user collects coins and I want them to be able to save them and use the coins for in app purchaces. How would I do this? Could I use NSUserDefaults for this or is it something else? Thanks! I used this for saving the highScore:
EDITED
var coinDefaults = NSUserDefaults()
var coinScore = defaults.integerForKey("coinScore")
if(coins > 0)
{
coinDefaults.setInteger(coins, forKey: "coinScore")
}
var showCoins = coinDefaults.integerForKey("coinScore")
coinLabel.text = String(showCoins)
}

Reseting the coinScore
NSUserDefaults.standardUserDefaults().removeObjectForKey("coinScore")
loading the saved score
var coinScore = NSUserDefaults.standardUserDefaults().integerForKey("coinScore")
var coins = 5
updating the coinScore
if coins > coinScore {
coinScore = coins
NSUserDefaults().setInteger(coinScore, forKey: "coinScore")
}
println( NSUserDefaults().integerForKey("coinScore").description )
coins += 20
if coins > coinScore {
coinScore = coins
NSUserDefaults().setInteger(coinScore, forKey: "coinScore")
}
println( NSUserDefaults().integerForKey("coinScore").description )

yes, you have different options to save data : nsuserdefaults, write to a file and store the file, coredata, or use a webserver with a database.
If someone knows how to hack your app, he will use what is inside your app (the 3 first methods), this is why many big games use internet (webserver) to check against what was bought by the user.
But, it is not so often that someone hack your game, if he does though, he would never pay anyway. So stay with nsuserdefaults, it is simple and good enough in my opinion.

Related

Pass my Coins variable to another Scene

I want to pass my varible from the GameScene.swift to the CoinScene.swift. I know i can use struct but i don't know how to use it. Everytime i try it, it doesn't work. In the GameScene.swift i have the variable coins:
var coins = Int()
in the CoinScene.swift i use iAP and if the User buy 100 Coins it has to update.
There i use the function:
func addCoins100() {
coins + 100
}
but this wouldn't work because theres no connection between this two "coins".
Where and how should i use the struct or is there an other way to do this?
I set this code in the top of GameScene.swift:
struct coins {
static var coinVariable = "coins"
}
And then this in the CoinsScene.swift:
let coins = coins.coinVarible
This is not working. Do anyone have an Idea to fix that?
Im a beginner and not very good but hungry to learn. Thank you very much.
You can use also:
in GameScene.swift:
static var coins = Int()
in CoinsScene.swift
func addCoins100() {
GameScene.coins + 100
}
Be careful with this approach
You are setting coinVariable to the string coins, not the variable coins. Here's what I would do:
struct coins{
var coinCount = 0
}
Then when you want to set coins in CoinScene.swift use
func addCoins100() {
coins.coinCount += 100
}
Whenever you want to access the coins just use coins.coinCount

Better way to show iAd interstitial ads

Is there a better way to show interstitial ads than just picking a random number between 0-4 and if its 3 show the ad?
This method has its downsides because sometimes ads wont show for awhile and other times they will show 3 times in a row which is very annoying. Any help would be appreciated!
Why generate a random number at all? There is no requirement for you to implement your interstitials in this manner. How often do you want to show your ads? You can just increment an index and show your interstitial once it reaches a certain amount. For example:
// Create an Int to increment
var index = 0
// Call this func after an event
func showInterstitial() {
index++
if (index == 5) {
// Show ad
// Reset index
index = 0
}
}

How do I get my HighScore to display instantly in Swift Spritekit?

I figured out how to save my highscore but I am having one problem. The highscore saves but it doesn't update the highscore after I beat the score. I have to play again and lose then it updates the highscore in the game over menu. How would I fix this? Here is the code.
func didBeginContact(contact:SKPhysicsContact){
var defaults=NSUserDefaults()
var highscore=defaults.integerForKey("highscore")
if(score>highscore)
{
defaults.setInteger(score, forKey: "highscore")
}
var highscoreshow = defaults.integerForKey("highscore")
endOfGameHighScoreLabel.text = String(highscore)
}
}
You need to use the highscoreshow variable you've declared instead of using the highscore variable. Because in your highscore variable there is still the highscore from one round before. And as you've already done, you need to get the new highscore which you saved in your NSUserDefaults. So you need to change that:
endOfGameHighScoreLabel.text = String(highscore)
To that:
endOfGameHighScoreLabel.text = String(highscoreshow)

Polymer core-animation-group imperative example?

I'm fairly new to Polymer and struggling to get some animations to work imperatively. My page displays a grid of cards. When one is clicked, I want the rest to move off screen.
I can get the cards to move one at a time in code, but since they all need to move in parallel, I think I need a core-animation-group to run them. But...
I can't figure out the syntax for creating a core-animation-group in code, and there doesn't seem to be a "play()" method...?
I'd be very, very grateful for a quick example.
TIA
The documentation on core-animation-group is a bit lackluster, but it is possible to create and play a core-animation-group using only JavaScript. Create your core-animation-group with new CoreAnimationGroup(). Then add child animations with animGroup.appendChild(coreAnim). Finally, play the group with animGroup.play().
You say you want to animate an indeterminate number of cards in parallel. Try something like this:
var cards = document.querySelector("#cards-wrapper").children;
var anim = new CoreAnimationGroup();
anim.type = "par"; // Display child animations in parallel.
anim.duration = 200; // Milliseconds
var cardAnimKeyframes = [
// CSS properties {propName: "value"}
{opacity: 1},
{opacity: 0},
];
for (var i = 0; i < cards.length; i++) {
var childAnim = new CoreAnimation();
childAnim.target = cards[i];
childAnim.keyframes = cardAnimKeyframes;
anim.appendChild(childAnim); // This is critical.
}
// Then, when you are ready...
anim.play();
Hope this helps :D

How do I apply the same animation to multiple items in AS3?

I have about 100 different MCs that I need to apply the following animation to.
total_bananas = 5;
frameCount = 0;
for (i=1;i<=total_bananas;i++)
{
thisMC = _root["mc"+i];
thisMC.startY = thisMC._y;
thisMC.rand = Math.random();
}
this.onEnterFrame = function ()
{
frameCount++;
for (i=1;i<=total_bananas;i++)
{
thisMC = _root["mc"+i];
thisMC._y = thisMC.startY + Math.sin(thisMC.rand*100+frameCount/10)*5;
}
}
how would I go about applying this animation to each of them individually? I don't need to populate 5 of the same MCs. This script is simply the perfect animation visually, but not lean and bespoke like it should be. I just need to make a lot of objects (all unique) bob up and down like they are tied to balloons. Also I was thinking this might be depreciated and there might be a waayy better way to do this.

Resources