如何塑造不去GameOver? -SpriteKit

时间:2017-02-15 17:55:47

标签: swift sprite-kit

我正在制作一个有方形的游戏,每当玩家点击广场时,它就会进入GameOver场景。我想要做的就是当点击方形时,将分配不同的屏幕位置以便在方块上轻敲。

  

这是我的代码:

var sections = [
    { sectionId: 1 },
    { sectionId: 2 },
    { sectionId: 3 },
    { sectionId: 4 },
    { sectionId: 5 },
];

var questions = [
    { questionId: 1, sectionId: 1 },
    { questionId: 2, sectionId: 1 },
    { questionId: 3, sectionId: 2 },
    { questionId: 4, sectionId: 3 },
    { questionId: 5, sectionId: 1 },
    { questionId: 6, sectionId: 4 },
    { questionId: 7, sectionId: 1 },
    { questionId: 8, sectionId: 5 }
];

console.log("**********Example 2**********")
$.each(sections, function(index, section) {
    console.log("Section " + section.sectionId + ":");

    var relatedQuestions = questions.filter(function(question) {
        return question.sectionId === section.sectionId;
    });

    $.each(relatedQuestions, function(index, question) {
        console.log("\tQuestion " + question.questionId);
    });
});

1 个答案:

答案 0 :(得分:1)

我建议为不同的形状制作enum之类的东西,这样你就可以跟踪你正在使用的形状。

enum GameShape: Int {

    case circle = 0
    case square = 1

}

然后在GameShape

的顶部创建一个GameScene媒体资源
var currentShape: GameShape = .circle

然后你可以创建某种updateShape方法,你可以用touchesBegan方法调用,而不只是addBall

func updateShape(shapeSize: CGSize) {

    switch currentShape {
        case .circle:
            addCircle(shapeSize)
        case .square:
            addSquare(shapeSize)
        default:
            break
    }

    // However you want to setup the condition for changing shape
    if (condition) {
        currentShape = .square
    }

}

func addBall(_ size: CGSize) {
    // Add the ball
}

func addSquare(_ size: CGSize) {
   // Add the square
}

现在使用touchesBegan方法,而不是致电addBall(size,您可以致电updateShape

override func touchesBegan(_ touches: Set<UITouch>!, with event: UIEvent?) {

    // Setup your code for detecting position for shape origin
    updateShape(shapeSize)

}

编辑 - 您的代码很乱。你真的应该花点时间确保它在提交时格式正确,否则很难帮助你。缩进有助于查看闭包的开始和结束位置。据我所知,看起来您的addBall方法中嵌套了两个或更多功能。这个不好。我尽力为你清理它。您仍然需要编写代码以使形状成为正方形,但我已经引导您朝着正确的方向开始实现:

func addBall(_ size: CGSize) {
    // Add the ball
    let currentBall = SKShapeNode(circleOfRadius: CGFloat(size))

    let viewMidX = view!.bounds.midX
    let viewMidY = view!.bounds.midY

    currentBall.fillColor = pickColor()

    shape.path = UIBezierPath(roundedRect: CGRect(x: 64, y: 64, width: 160, height: 160), cornerRadius: 50).cgPath

    shape.fillColor = pickColor()

    currentBall.position = randomBallPosition()
    shape.position = randomBallPosition()

    self.addChild(shape)

    if scoreCount != 0{
        if scoreCount == 1{
            self.addChild(score)
            self.addChild(timeLeftLabel)
            self.childNode(withName: "welcome")?.removeFromParent()
        }
        self.childNode(withName: "ball")?.run(getSmaller)
        self.childNode(withName: "ball")?.removeFromParent()

    }
    currentBall.name = "ball"
    shape.name = "ball"

    self.addChild(currentBall)
}

func addSquare(_ size: CGSize) {
    // Add the square
}

func randomBallPosition() -> CGPoint {
    let xPosition = CGFloat(arc4random_uniform(UInt32((view?.bounds.maxX)! + 1)))
    let yPosition = CGFloat(arc4random_uniform(UInt32((view?.bounds.maxY)! + 1)))


    return CGPoint(x: xPosition, y: yPosition)

}

现在上面的代码假设你有一些名为shape的属性,因为我可以告诉你从未明确地实例化过,但是你开始操作它。