Swift 我的初始管道与以下管道不同
我正在做一个游戏,假设球穿过一些管道,当球员触碰管道时,游戏停止。有点像飞鸟。我唯一的问题是,最初的管道会阻塞整个屏幕,而其余的管道则完全按照我的意愿进行放置和随机分配。这怎么可能 这是棒球课:Swift 我的初始管道与以下管道不同,swift,sprite-kit,Swift,Sprite Kit,我正在做一个游戏,假设球穿过一些管道,当球员触碰管道时,游戏停止。有点像飞鸟。我唯一的问题是,最初的管道会阻塞整个屏幕,而其余的管道则完全按照我的意愿进行放置和随机分配。这怎么可能 这是棒球课: import SpriteKit struct ColliderType { static let Ball: UInt32 = 1 static let Pipes: UInt32 = 2 static let Score: UInt32 = 3 } class Ball: SKSpriteNod
import SpriteKit
struct ColliderType {
static let Ball: UInt32 = 1
static let Pipes: UInt32 = 2
static let Score: UInt32 = 3
}
class Ball: SKSpriteNode {
func initialize() {
self.name = "Ball"
self.zPosition = 1
self.anchorPoint = CGPoint(x: 0.5, y: 0.5)
self.physicsBody = SKPhysicsBody(circleOfRadius: self.size.height /
2)
self.setScale(0.7)
self.physicsBody?.affectedByGravity = false
self.physicsBody?.categoryBitMask = ColliderType.Ball
self.physicsBody?.collisionBitMask = ColliderType.Pipes
self.physicsBody?.contactTestBitMask = ColliderType.Pipes |
ColliderType.Score
}
}
这是随机类:
import Foundation
import CoreGraphics
public extension CGFloat {
public static func randomBetweenNumbers(firstNum: CGFloat, secondNum:
CGFloat) -> CGFloat {
return CGFloat(arc4random()) / CGFloat(UINT32_MAX) * abs(firstNum -
secondNum) + firstNum
}
}
这是游戏场景:
import SpriteKit
class GameplayScene: SKScene {
var ball = Ball()
var pipesHolder = SKNode()
var touched: Bool = false
var location = CGPoint.zero
override func didMove(to view: SKView) {
initialize()
}
override func update(_ currentTime: TimeInterval) {
moveBackgrounds()
if (touched) {
moveNodeToLocation()
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event:
UIEvent?) {
touched = true
for touch in touches {
location = touch.location(in:self)
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event:
UIEvent?) {
touched = false
}
override func touchesMoved(_ touches: Set<UITouch>, with event:
UIEvent?) {
for touch in touches {
location = touch.location(in: self)
}
}
func initialize() {
createBall()
createBackgrounds()
createPipes()
spawnObstacles()
}
func createBall() {
ball = Ball(imageNamed: "Ball")
ball.initialize()
ball.position = CGPoint(x: 0, y: 0)
self.addChild(ball)
}
func createBackgrounds() {
for i in 0...2 {
let bg = SKSpriteNode(imageNamed: "BG")
bg.anchorPoint = CGPoint(x: 0.5, y: 0.5)
bg.zPosition = 0
bg.name = "BG"
bg.position = CGPoint(x: 0, y: CGFloat(i) * bg.size.height)
self.addChild(bg)
}
}
func moveBackgrounds() {
enumerateChildNodes(withName: "BG", using: ({
(node, error) in
node.position.y -= 15
if node.position.y < -(self.frame.height) {
node.position.y += self.frame.height * 3
}
}))
}
func createPipes() {
pipesHolder = SKNode()
pipesHolder.name = "Holder"
let pipeLeft = SKSpriteNode(imageNamed: "Pipe")
let pipeRight = SKSpriteNode(imageNamed: "Pipe")
pipeLeft.name = "Pipe"
pipeLeft.anchorPoint = CGPoint(x: 0.5, y: 0.5)
pipeLeft.position = CGPoint(x: 350, y: 0)
pipeLeft.xScale = 1.5
pipeLeft.physicsBody = SKPhysicsBody(rectangleOf: pipeLeft.size)
pipeLeft.physicsBody?.categoryBitMask = ColliderType.Pipes
pipeLeft.physicsBody?.affectedByGravity = false
pipeLeft.physicsBody?.isDynamic = false
pipeRight.name = "Pipe"
pipeRight.anchorPoint = CGPoint(x: 0.5, y: 0.5)
pipeRight.position = CGPoint(x: -350, y: 0)
pipeRight.xScale = 1.5
pipeRight.physicsBody = SKPhysicsBody(rectangleOf: pipeRight.size)
pipeRight.physicsBody?.categoryBitMask = ColliderType.Pipes
pipeRight.physicsBody?.affectedByGravity = false
pipeRight.physicsBody?.isDynamic = false
pipesHolder.zPosition = 5
pipesHolder.position.y = self.frame.height + 100
pipesHolder.position.x = CGFloat.randomBetweenNumbers(firstNum:
-250, secondNum: 250)
pipesHolder.addChild(pipeLeft)
pipesHolder.addChild(pipeRight)
self.addChild(pipesHolder)
let destination = self.frame.height * 3
let move = SKAction.moveTo(y: -destination, duration:
TimeInterval(10))
let remove = SKAction.removeFromParent()
pipesHolder.run(SKAction.sequence([move, remove]), withKey: "Move")
}
func spawnObstacles() {
let spawn = SKAction.run({ () -> Void in
self.createPipes()
})
let delay = SKAction.wait(forDuration: TimeInterval(1.5))
let sequence = SKAction.sequence([spawn, delay])
self.run(SKAction.repeatForever(sequence), withKey: "Spawn")
}
func moveNodeToLocation() {
// Compute vector components in direction of the touch
var dx = location.x - ball.position.x
// How fast to move the node. Adjust this as needed
let speed:CGFloat = 0.1
// Scale vector
dx = dx * speed
ball.position = CGPoint(x:ball.position.x+dx, y: 0)
}
}
导入SpriteKit
类游戏场景:SKScene{
var ball=ball()
var pipesHolder=SKNode()
var:Bool=false
变量位置=CGPoint.zero
覆盖func didMove(到视图:SKView){
初始化()
}
覆盖函数更新(uCurrentTime:TimeInterval){
移动背景()
如果(触摸){
moveNodeToLocation()
}
}
覆盖func TouchesBegined(Touchs:Set,带有事件:
(事件?){
触摸=真实
接触{
位置=触摸。位置(in:self)
}
}
覆盖函数touchesend(touchs:Set,带有事件:
(事件?){
触摸=错误
}
覆盖功能触摸移动(touchs:Set,带有事件:
(事件?){
接触{
位置=触摸。位置(in:self)
}
}
func初始化(){
createBall()
createBackgrounds()
createPipes()
()
}
func createBall(){
ball=ball(图像名为:“ball”)
ball.initialize()
ball.position=CGPoint(x:0,y:0)
self.addChild(ball)
}
func createBackgrounds(){
因为我在0…2{
设bg=SKSpriteNode(图像名为:“bg”)
bg.anchorPoint=CGPoint(x:0.5,y:0.5)
bg.zPosition=0
bg.name=“bg”
bg.position=CGPoint(x:0,y:CGFloat(i)*bg.size.height)
self.addChild(bg)
}
}
func{
枚举子节点(名称为“BG”),使用:({
(节点,错误)在
节点位置y-=15
如果节点位置y<-(自身框架高度){
node.position.y+=self.frame.height*3
}
}))
}
func createPipes(){
pipesHolder=SKNode()
pipesHolder.name=“持有人”
让pipeLeft=SKSpriteNode(图像名为:“管道”)
让pipeRight=SKSpriteNode(图像名为:“管道”)
pipeLeft.name=“管道”
pipeLeft.ancorpoint=CGPoint(x:0.5,y:0.5)
pipeLeft.position=CGPoint(x:350,y:0)
pipeLeft.xScale=1.5
pipeLeft.physicsBody=SKPhysicsBody(矩形:pipeLeft.size)
pipeLeft.physicsBody?.categoryBitMask=ColliderType.Pipes
pipeLeft.physicsBody?.affectedByGravity=false
pipeLeft.physicsBody?.isDynamic=false
piperRight.name=“管道”
pipeRight.anchorPoint=CGPoint(x:0.5,y:0.5)
pipeRight.position=CGPoint(x:-350,y:0)
pipeRight.xScale=1.5
pipeRight.physicsBody=SKPhysicsBody(矩形:pipeRight.size)
pipeRight.physicsBody?.categoryBitMask=ColliderType.Pipes
pipeRight.physicsBody?重力影响=假
pipeRight.physicsBody?.isDynamic=false
pipesHolder.zPosition=5
管道支架位置y=自身框架高度+100
pipesHolder.position.x=CGFloat.randomBetweenNumber(firstNum:
-250,秒数:250)
pipesHolder.addChild(pipeLeft)
pipesHolder.addChild(pipeRight)
self.addChild(pipesHolder)
让目的地=self.frame.height*3
let move=SKAction.moveTo(y:-目的地,持续时间:
时间间隔(10))
让remove=SKAction.removeFromParent()
pipesHolder.run(SKAction.sequence([move,remove]),使用键:“move”)
}
func(){
让spawn=SKAction.run({()->Void in
self.createPipes()
})
let delay=SKAction.wait(持续时间:时间间隔(1.5))
let sequence=SKAction.sequence([spawn,delay])
self.run(SKAction.repeatForever(序列),带有键:“Spawn”)
}
func moveNodeToLocation(){
//计算触摸方向上的矢量分量
var dx=位置.x-球位置.x
//移动节点的速度。根据需要调整此速度
让速度:CGFloat=0.1
//尺度向量
dx=dx*速度
ball.position=CGPoint(x:ball.position.x+dx,y:0)
}
}
请使用内联代码更新您的问题。不要使用图像发布代码。请正确发布代码,以便我们可以将其复制到Xcode:)只需编辑它!伙计们,刚刚编辑过!:)