ios - 在 XCODE 7(swift 2)-CGPOINTMAKE 中构建我的第一款游戏?

标签 ios sprite-kit swift2 xcode7

我想构建一个类似斯诺克的游戏,一开始我已经遇到了一些问题。我想先建四面墙(这将是 screen-self.frame 的大小),我做的第一面是这样的:

let ground = SKNode()
ground.position = CGPointMake ( 0, 0)
ground.physicsBody = SKPhysicsBody( rectangleOfSize:CGSizeMake(self.frame.size.width * 3, 1))
ground.physicsBody!.dynamic = false
self.addChild(ground)

但是,我不知道如何操纵 CGPointMake 的值来制作左/右/上墙。我一开始以为(0,0)点是左下角,但好像不是这样的。有人可以帮我解决这个问题,或者只是解释一下这是如何工作的吗? (因为在 https://developer.apple.com/library/prerelease/ios/documentation/GraphicsImaging/Reference/CGGeometry/index.html#//apple_ref/c/func/CGPointMake 他们没有解释太多 =/)

最佳答案

这是一个基本示例,说明如何限制球离开屏幕:

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {

    let BallCategory    : UInt32 = 0x1 << 1
    let WallCategory    : UInt32 = 0x1 << 2

    let ball = SKShapeNode(circleOfRadius: 40)

    override func didMoveToView(view: SKView) {
        /* Setup your scene here */

      physicsWorld.contactDelegate = self



       setupWalls()

       setupBall()


    }

    func setupWalls(){


        physicsBody = SKPhysicsBody(edgeLoopFromRect: frame)
        physicsBody?.categoryBitMask = WallCategory
        physicsBody?.contactTestBitMask = BallCategory
        physicsBody?.collisionBitMask = BallCategory



    }

    func setupBall(){

        ball.physicsBody = SKPhysicsBody(circleOfRadius: 40)
        ball.fillColor = SKColor.whiteColor()
        ball.name = "ball"
        ball.physicsBody?.categoryBitMask = BallCategory
        ball.physicsBody?.contactTestBitMask = WallCategory
        ball.physicsBody?.collisionBitMask = WallCategory
        ball.physicsBody?.dynamic = true //In order to detect contact between two bodies, at least on body has to be dynamic
        ball.physicsBody?.affectedByGravity = false
        ball.physicsBody?.restitution = 0.5


        ball.position = CGPoint(x: CGRectGetMidX(frame), y: CGRectGetMidY(frame)) // placing to ball in the middle of the screen

        addChild(ball)
    }



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


       ball.physicsBody?.applyImpulse(CGVector(dx: 200, dy: 200))

    }

    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */


    }
}

关于ios - 在 XCODE 7(swift 2)-CGPOINTMAKE 中构建我的第一款游戏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34096009/

相关文章:

ios - swift:未检测到 Sprite 套件碰撞和位掩码

ios - 创建无限滚动的背景

ios - Swift 2 未使用的常量警告

ios - 无法播放AVAudioPlayer中文档中的文件

ios - UICollectionview cell/imageview 不会全屏点击

ios - UITableViewCell 中的 UIButton 从 UITableView 窃取触摸

ios - 类型 "any"在 alamofire 4 iOS swift 中没有下标成员

ios - 忽略从 iOS 上的导航栏按下后退按钮

ios - 尝试使用具有新属性的每个 SKSpriteNode

swift - UISwitch 不改变 Swift 中的值