gpt4 book ai didi

ios - 我正在尝试制作一款横向卷轴游戏,但我正在努力无限地生成地面

转载 作者:行者123 更新时间:2023-11-30 13:35:31 25 4
gpt4 key购买 nike

I have made it so that when you tap on the left or right side of the screen it will move accordingly by moving the worldNode

import SpriteKit
import UIKit
import GameplayKit

var ground = Ground(imageNamed: "grass.png")
var sceneBody = SKPhysicsBody()

enum BodyType:UInt32 {
case Player = 1
case Wall = 2
case Ground = 4
case AnotherObject = 8
case AnotherObject2 = 16
case AnotherObject3 = 32
case AnotherObject4 = 64

}



class GameScene: SKScene, SKPhysicsContactDelegate{

var player = Player(imageNamed: "p1_walk02.png")
let theCamera: SKCameraNode = SKCameraNode()
var textAtlas = SKTextureAtlas()
var textArry = [SKTexture]()
var worldNode = SKNode()






override func didMoveToView(view: SKView) {
self.physicsWorld.gravity = CGVectorMake(0, -9.8)
physicsWorld.contactDelegate = self



self.anchorPoint = CGPointMake(0, 0)
self.backgroundColor = SKColor.whiteColor()

//self.camera?.position = player.position

addChild(worldNode)

let spawn = SKAction.runBlock({
() in

self.makeGU1()

})

let delay = SKAction.waitForDuration(2)

let seq = SKAction.sequence([spawn, delay])

worldNode.runAction(SKAction.repeatAction(seq, count: 1))

self.addChild(player)

player.xScale = 0.5
player.yScale = 0.5
player.position = CGPoint(x: self.frame.width*0.3, y: ground.position.y+56)

worldNode.position = self.position
worldNode.xScale = self.xScale
worldNode.yScale = self.yScale


}

func scheduledTimerWithTimeInterval(){
//Scheduling timer to Call the function **Countdown** with the interval of 1 seconds
let myTimer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: Selector("updateCounting"), userInfo: nil, repeats: true)
}
func updateCounting() {

}


override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
print(location)

//-7.49999237060547
if player.position.x > 230.499969482422{

}
if location.x > self.frame.width/2 {
// Left side of the screen
textAtlas = SKTextureAtlas(named: "Walk")

for i in 1...textAtlas.textureNames.count{
let Name = "p1_walk0\(i).png"
textArry.append(SKTexture(imageNamed: Name))
}
let Animate = SKAction.animateWithTextures(textArry, timePerFrame: 0.1)
player.runAction(SKAction.repeatActionForever(Animate), withKey: "Left run")
print(location)
worldNode.runAction(SKAction.repeatActionForever(SKAction.moveBy(CGVectorMake(1000, 0), duration: 4).reversedAction()), withKey: "moveLeft")
self.runAction(SKAction.repeatActionForever(SKAction.moveBy(CGVectorMake(1000, 0), duration: 4).reversedAction()), withKey: "moveLeft")


} else {
textAtlas = SKTextureAtlas(named: "Walk")

for i in 1...textAtlas.textureNames.count{
let Name = "p1_walk0\(i).png"
textArry.append(SKTexture(imageNamed: Name))

}
let Animate = SKAction.animateWithTextures(textArry, timePerFrame: 0.1)
player.runAction(SKAction.repeatActionForever(Animate), withKey: "Left run")
print(location)
worldNode.runAction(SKAction.repeatActionForever(SKAction.moveBy(CGVectorMake(1000, 0), duration: 4)), withKey: "moveLeft")
self.runAction(SKAction.repeatActionForever(SKAction.moveBy(CGVectorMake(1000, 0), duration: 4)), withKey: "moveLeft")

}
}
}




override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
player.removeActionForKey("Left run")
var tex = [SKTexture]()
tex.append(SKTexture(imageNamed:"p1_walk02.png"))
player.runAction(SKAction.animateWithTextures(tex, timePerFrame: 0.2))
worldNode.removeActionForKey("moveLeft")
self.removeActionForKey("moveLeft")

}




func makeGU1(){

This is where I set up the scene

        var ground1 = SKSpriteNode(imageNamed: "grass.png")
var ground2 = SKSpriteNode(imageNamed: "grass.png")
var ground0 = SKSpriteNode(imageNamed: "grass.png")
var ground3 = SKSpriteNode(imageNamed: "grass.png")
var ground4 = SKSpriteNode(imageNamed: "grass.png")
var ground5 = SKSpriteNode(imageNamed: "grass.png")
var ground6 = SKSpriteNode(imageNamed: "grass.png")
var ground7 = SKSpriteNode(imageNamed: "grass.png")
var ground8 = SKSpriteNode(imageNamed: "grass.png")
var ground9 = SKSpriteNode(imageNamed: "grass.png")
var ground10 = SKSpriteNode(imageNamed: "grass.png")
var ground11 = SKSpriteNode(imageNamed: "grass.png")
var ground12 = SKSpriteNode(imageNamed: "grass.png")
var ground13 = SKSpriteNode(imageNamed: "grass.png")




ground0.xScale = 0.7
ground0.yScale = 0.7

ground0.position = CGPoint(x: 20, y: 10)

worldNode.addChild(ground0)

ground0.physicsBody = SKPhysicsBody(rectangleOfSize: ground0.size)
ground0.physicsBody?.affectedByGravity = false
ground0.physicsBody?.dynamic = false
ground0.physicsBody?.categoryBitMask = BodyType.Ground.rawValue
ground0.physicsBody?.collisionBitMask = BodyType.Player.rawValue
ground0.physicsBody?.contactTestBitMask = BodyType.Player.rawValue
ground0.copy()

ground1 = ground0.copy() as! SKSpriteNode
ground1.position = CGPoint(x: ground0.position.x+ground1.size.width, y: 10)
ground1.copy()

worldNode.addChild(ground1)









ground2.xScale = 0.7
ground2.yScale = 0.7

ground2.physicsBody = SKPhysicsBody(rectangleOfSize: ground2.size)
ground2.physicsBody?.affectedByGravity = false
ground2.physicsBody?.dynamic = false
ground2.physicsBody?.categoryBitMask = BodyType.Ground.rawValue
ground2.physicsBody?.collisionBitMask = BodyType.Player.rawValue
ground2.physicsBody?.contactTestBitMask = BodyType.Player.rawValue
ground2.position = CGPoint(x: ground1.position.x+ground2.size.width, y: 10)

worldNode.addChild(ground2)



ground3.xScale = 0.7
ground3.yScale = 0.7

ground3.physicsBody = SKPhysicsBody(rectangleOfSize: ground3.size)
ground3.physicsBody?.affectedByGravity = false
ground3.physicsBody?.dynamic = false
ground3.physicsBody?.categoryBitMask = BodyType.Ground.rawValue
ground3.physicsBody?.collisionBitMask = BodyType.Player.rawValue
ground3.physicsBody?.contactTestBitMask = BodyType.Player.rawValue
ground3.position = CGPoint(x: ground2.position.x+ground3.size.width, y: 10)

worldNode.addChild(ground3)



ground4.xScale = 0.7
ground4.yScale = 0.7

ground4.physicsBody = SKPhysicsBody(rectangleOfSize: ground4.size)
ground4.physicsBody?.affectedByGravity = false
ground4.physicsBody?.dynamic = false
ground4.physicsBody?.categoryBitMask = BodyType.Ground.rawValue
ground4.physicsBody?.collisionBitMask = BodyType.Player.rawValue
ground4.physicsBody?.contactTestBitMask = BodyType.Player.rawValue
ground4.physicsBody?.affectedByGravity = false
ground4.physicsBody?.dynamic = false
ground4.position = CGPoint(x: ground3.position.x+ground4.size.width, y: 10)

worldNode.addChild(ground4)



ground5.xScale = 0.7
ground5.yScale = 0.7

ground5.physicsBody = SKPhysicsBody(rectangleOfSize: ground5.size)

ground5.physicsBody?.categoryBitMask = BodyType.Ground.rawValue
ground5.physicsBody?.collisionBitMask = BodyType.Player.rawValue
ground5.physicsBody?.contactTestBitMask = BodyType.Player.rawValue
ground5.physicsBody?.affectedByGravity = false
ground5.physicsBody?.dynamic = false
ground5.position = CGPoint(x: ground4.position.x+ground5.size.width, y: 10)

worldNode.addChild(ground5)



ground6.xScale = 0.7
ground6.yScale = 0.7
ground6.physicsBody = SKPhysicsBody(rectangleOfSize: ground6.size)
ground6.physicsBody?.affectedByGravity = false
ground6.physicsBody?.dynamic = false

ground6.physicsBody?.categoryBitMask = BodyType.Ground.rawValue
ground6.physicsBody?.collisionBitMask = BodyType.Player.rawValue
ground6.physicsBody?.contactTestBitMask = BodyType.Player.rawValue

ground6.position = CGPoint(x: ground5.position.x+ground6.size.width, y: 10)

worldNode.addChild(ground6)



ground7.xScale = 0.7
ground7.yScale = 0.7
ground7.physicsBody = SKPhysicsBody(rectangleOfSize: ground6.size)
ground6.physicsBody?.affectedByGravity = false
ground7.physicsBody?.dynamic = false

ground7.physicsBody?.categoryBitMask = BodyType.Ground.rawValue
ground7.physicsBody?.collisionBitMask = BodyType.Player.rawValue
ground7.physicsBody?.contactTestBitMask = BodyType.Player.rawValue

ground7.position = CGPoint(x: ground6.position.x+ground7.size.width, y: 10)
worldNode.addChild(ground7)



ground8.xScale = 0.7
ground8.yScale = 0.7
ground8.physicsBody = SKPhysicsBody(rectangleOfSize: ground8.size)
ground8.physicsBody?.affectedByGravity = false
ground8.physicsBody?.dynamic = false

ground8.physicsBody?.categoryBitMask = BodyType.Ground.rawValue
ground8.physicsBody?.collisionBitMask = BodyType.Player.rawValue
ground8.physicsBody?.contactTestBitMask = BodyType.Player.rawValue

ground8.position = CGPoint(x: ground7.position.x+ground8.size.width, y: 10)

worldNode.addChild(ground8)



ground9.xScale = 0.7
ground9.yScale = 0.7
ground9.physicsBody = SKPhysicsBody(rectangleOfSize: ground9.size)
ground9.physicsBody?.affectedByGravity = false
ground9.physicsBody?.dynamic = false

ground9.physicsBody?.categoryBitMask = BodyType.Ground.rawValue
ground9.physicsBody?.collisionBitMask = BodyType.Player.rawValue
ground9.physicsBody?.contactTestBitMask = BodyType.Player.rawValue

ground9.position = CGPoint(x: ground8.position.x+ground9.size.width, y: 10)
worldNode.addChild(ground9)




ground10.xScale = 0.7
ground10.yScale = 0.7
ground10.physicsBody = SKPhysicsBody(rectangleOfSize: ground10.size)
ground10.physicsBody?.affectedByGravity = false
ground10.physicsBody?.dynamic = false

ground10.physicsBody?.categoryBitMask = BodyType.Ground.rawValue
ground10.physicsBody?.collisionBitMask = BodyType.Player.rawValue
ground10.physicsBody?.contactTestBitMask = BodyType.Player.rawValue

ground10.position = CGPoint(x: ground9.position.x+ground10.size.width, y: 10)
worldNode.addChild(ground10)



ground11.xScale = 0.7
ground11.yScale = 0.7
ground11.physicsBody = SKPhysicsBody(rectangleOfSize: ground11.size)
ground11.physicsBody?.categoryBitMask = BodyType.Ground.rawValue
ground11.physicsBody?.collisionBitMask = BodyType.Player.rawValue
ground11.physicsBody?.contactTestBitMask = BodyType.Player.rawValue
ground11.physicsBody?.affectedByGravity = false
ground11.physicsBody?.dynamic = false
ground11.position = CGPoint(x: ground10.position.x+ground11.size.width, y: 10)
worldNode.addChild(ground11)



ground12.xScale = 0.7
ground12.yScale = 0.7
ground12.physicsBody = SKPhysicsBody(rectangleOfSize: ground12.size)
ground12.physicsBody?.categoryBitMask = BodyType.Ground.rawValue
ground12.physicsBody?.collisionBitMask = BodyType.Player.rawValue
ground12.physicsBody?.contactTestBitMask = BodyType.Player.rawValue
ground12.physicsBody?.affectedByGravity = false
ground12.physicsBody?.dynamic = false
ground12.position = CGPoint(x: ground11.position.x+ground12.size.width, y: 10)
worldNode.addChild(ground12)


//if ground13.parent == nil {
ground13.xScale = 0.7
ground13.yScale = 0.7
ground13.physicsBody = SKPhysicsBody(rectangleOfSize: ground13.size)
ground13.physicsBody?.categoryBitMask = BodyType.Ground.rawValue
ground13.physicsBody?.collisionBitMask = BodyType.Player.rawValue
ground13.physicsBody?.contactTestBitMask = BodyType.Player.rawValue
ground13.physicsBody?.affectedByGravity = false
ground13.physicsBody?.dynamic = false
ground13.position = CGPoint(x: ground12.position.x+ground13.size.width, y: 10)
worldNode.addChild(ground13)
//}


}//end of Make Block
}

This makes just enough to fill the ground of the screen of a iphone 6

最佳答案

我有code on github创建无限滚动的背景。它与制作视差背景有关,但您可以在其中看到让它无限滚动的代码。诀窍是创建足够的地面节点来覆盖宽度,并为每一端添加一些额外的节点。当左边的完全移出屏幕时(通过检查它的 origin.x + size.width 值与 View 的原点),您将其拾起并将其移动到最右侧。相反,如果您向另一个方向移动,并发现最后一个节点完全超出屏幕,则可以拾取最右边的节点并将其移动到开头。

如果做得正确,它看起来很流畅而且相当简单。

关于ios - 我正在尝试制作一款横向卷轴游戏,但我正在努力无限地生成地面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36120511/

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