小编Poi*_*pps的帖子

恢复应用内购买Swift

我已在应用中添加了非耗材的应用内购买功能,以移除广告.购买工作正常,恢复我遇到麻烦.我只是不知道该怎么做.

这是我的购买代码:

    func buyNonConsumable(){
    println("About to fetch the products");
    // We check that we are allow to make the purchase.
    if (SKPaymentQueue.canMakePayments())
    {
        var productID:NSSet = NSSet(object: self.product_id!);
        var productsRequest:SKProductsRequest = SKProductsRequest(productIdentifiers: productID);
        productsRequest.delegate = self;
        productsRequest.start();
        println("Fething Products");
    }else{
        println("can't make purchases");
    }
}

// Helper Methods
func buyProduct(product: SKProduct){
    println("Sending the Payment Request to Apple");
    var payment = SKPayment(product: product)
    SKPaymentQueue.defaultQueue().addPayment(payment);
}

func removeTheAds(){

    userDefaults.setBool(true, forKey: "proUser")
    userDefaults.synchronize()
    print("You are a pro User")

    //Code for removing the ads …
Run Code Online (Sandbox Code Playgroud)

in-app-purchase ios swift

5
推荐指数
1
解决办法
3790
查看次数

用SKShapeNode画一条线

我想简单地使用SKShapeNode绘制一条线.我正在使用SpriteKit和Swift.

到目前为止,这是我的代码:

var line = SKShapeNode()
var ref = CGPathCreateMutable()

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {

    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)

    }
}

override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {

    for touch: AnyObject in touches {
        let locationInScene = touch.locationInNode(self)

        CGPathMoveToPoint(ref, nil, locationInScene.x, locationInScene.y)
        CGPathAddLineToPoint(ref, nil, locationInScene.x, locationInScene.y)
        line.path = ref
        line.lineWidth = 4
        line.fillColor = UIColor.redColor()
        line.strokeColor = UIColor.redColor()
        self.addChild(line)

    }
}
Run Code Online (Sandbox Code Playgroud)

每当我运行它并尝试绘制一条线时,应用程序崩溃并出现错误:reason:'尝试添加已经拥有父级的SKNode:SKShapeNode名称:'(null)'accumulationFrame:{{0,0}},{0 ,0}}'

为什么会这样?

ios sprite-kit skshapenode swift

4
推荐指数
1
解决办法
4706
查看次数

将NSNotificationCenter与SkScene一起使用到UIViewController

我正在使用SpriteKit与Swift进行游戏.

我正在使用界面构建器来定位我的所有按钮(菜单,重试等),所以我可以使用自动布局.然后,我在游戏功能中使用NSNotificationCenter在SKScene中显示这些按钮.使用NSNotificationCenter在SKScene中显示UIKit的东西没问题.

这是我的问题,我将尽力解释.

当你获得游戏时,会显示一个名为retry的UIButton.我在SKScene中设置了一个NSNotificationCenter观察器用于重试功能,我在IBAction中为viewController中的按钮调用postNotificationName.当我第一次加载游戏时,通知工作正常.我可以玩,重试,玩和重试.

然而,当我返回主菜单并再次加载游戏并按重试游戏崩溃.有时它只是(lldb).其他时候将其无法识别的选择器发送到实例.

这是我到目前为止的代码:这是ViewController

class GameSceneViewController: UIViewController{

 @IBOutlet weak var homeBtn: UIButton!
@IBOutlet weak var retryBtn: UIButton!

var userDefaults = NSUserDefaults.standardUserDefaults()

//MARK: Button Actions
@IBAction func returnToMenu(sender: UIButton) {

    self.dismissViewControllerAnimated(true, completion: nil)
}

@IBAction func retryGame(sender: UIButton) {

    NSNotificationCenter.defaultCenter().postNotificationName("retrygame", object: nil)

}
    override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    var scene: GameScene!
    let skView = view as SKView
    skView.showsFPS = false
    skView.showsNodeCount = true
    skView.showsPhysics = false

    skView.ignoresSiblingOrder = true

    scene = GameScene(size: skView.bounds.size)
    scene.scaleMode = .AspectFill

    skView.presentScene(scene)

} …
Run Code Online (Sandbox Code Playgroud)

nsnotificationcenter ios sprite-kit swift

2
推荐指数
1
解决办法
857
查看次数

识别阵列中的重复项

我有一组自定义对象,并且想知道如何识别哪些对象是重复的。我知道如何删除重复项,但这不是我要的功能。我正在使用Swift 2。

例:

var movies: [Movie] = ["Batman v Superman: Dawn of Justice", "Batman v Superman: Dawn of Justice", "Deadpool"," "Deadpool", "Hardcore Henry", "Jason Bourne", "Jurassic World"]
Run Code Online (Sandbox Code Playgroud)

因此,我想显示上面的电影列表的表格视图,但突出显示“蝙蝠侠”和“死侍”。

要进一步了解我要实现的目标,请查看屏幕截图。我有两个用户在上一个视图控制器中选择的电影列表。我想以表格视图显示选定的电影。我想特别说明两个人是否都选择了电影。

在此处输入图片说明

arrays swift swift2

1
推荐指数
1
解决办法
2907
查看次数

多个UIView动画

我有一个UIButton,我使用界面构建器放置在屏幕的中心.

我想动画UIButton来上下移动并永远重复动画.

到目前为止,我在didMoveToView中有这个:我正在使用swift.

 UIView.animateWithDuration(3, animations: { () -> Void in
        self.playBtn.transform = CGAffineTransformMakeTranslation(0, 10)
        self.playBtn.transform = CGAffineTransformMakeTranslation(0, -10)
        self.playBtn.transform = CGAffineTransformMakeTranslation(0, -10)
        self.playBtn.transform = CGAffineTransformMakeTranslation(0, 10)
        UIView.setAnimationRepeatCount(-1)
    })
Run Code Online (Sandbox Code Playgroud)

但是它只运行第一行并将按钮向下移动10.

任何帮助,将不胜感激

xcode uiviewcontroller uiview ios swift

0
推荐指数
1
解决办法
1074
查看次数

数组中的随机数不连续两次重复相同的数字?

我正在使用Swift和SpriteKit进行游戏,我将对象移动到基于数组的随机位置.

由CGPoints组成的数组:

let easyArray = [CGPointMake(0,0), CGPointMake(126.6,0),   CGPointMake(253.4,0), CGPointMake(0,197.5), CGPointMake(126.7,197.5), CGPointMake(253.4,197.5), CGPointMake(0,395), CGPointMake(126.7,395), CGPointMake(253.4,395)]
Run Code Online (Sandbox Code Playgroud)

我用这个函数生成一个随机数:

func randomNumber(maximum: UInt32) -> Int {

    var randomNumber = arc4random_uniform(maximum)
    while previousNumber == randomNumber {
    randomNumber = arc4random_uniform(maximum)
}
    previousNumber = randomNumber
    return Int(randomNumber)
}
Run Code Online (Sandbox Code Playgroud)

我用它来根据生成的随机数移动对象:

let greenEasy = randomNumberNew(9)
let moveSelector = SKAction.moveTo(easyArray[greenEasy], duration: 0)
selector.runAction(moveSelector)
Run Code Online (Sandbox Code Playgroud)

我在网上做了一些阅读,发现"While"条件应该使得相同的随机数连续两次生成.但它仍然会发生.

任何人都可以帮助我如何使它,所以我没有连续两次获得相同的数字?

arrays arc4random ios sprite-kit swift

0
推荐指数
1
解决办法
1406
查看次数