gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 19:41:47 26 4
gpt4 key购买 nike

我想构建一个类似斯诺克的游戏,一开始我已经遇到了一些问题。我想先建四面墙(这将是 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com