我正在尝试制作一个侧滚动游戏,我正在努力无限地产生地面

时间:2022-10-19 21:17:52

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

我已经做到这一点,当你点击屏幕的左侧或右侧时,它将通过移动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

这足以填满iphone 6的屏幕底部

1 个解决方案

#1


0  

I have code on github that creates an infinitely scrolling background. It is concerned with making a parallax background, but you can see the code in there for having it scroll infinitely. The trick is to create enough ground nodes to cover the width, plus a couple extras for each end. When the one at the left moves completely off the screen (by checking it's origin.x + size.width value against the origin of the view) you pick it up and move it to the far right. Conversely, if you are moving the other direction, and find that your last node is completely off the screen, you pick up the one furthest right and move it to the beginning.

我在github上有代码,可以创建无限滚动的背景。它关注的是制作一个视差背景,但你可以看到那里的代码让它无限滚动。诀窍是创建足够的地面节点来覆盖宽度,并为每一端增加几个额外的节点。当左边的那个完全离开屏幕时(通过检查它的origin.x + size.width值对着视图的原点),你拿起它并将它移到最右边。相反,如果您正在移动另一个方向,并且发现您的最后一个节点完全不在屏幕上,那么您将选择最右边的节点并将其移至开头。

When done correctly it looks smooth and is fairly simple.

如果操作正确,它看起来很平滑,非常简单。

#1


0  

I have code on github that creates an infinitely scrolling background. It is concerned with making a parallax background, but you can see the code in there for having it scroll infinitely. The trick is to create enough ground nodes to cover the width, plus a couple extras for each end. When the one at the left moves completely off the screen (by checking it's origin.x + size.width value against the origin of the view) you pick it up and move it to the far right. Conversely, if you are moving the other direction, and find that your last node is completely off the screen, you pick up the one furthest right and move it to the beginning.

我在github上有代码,可以创建无限滚动的背景。它关注的是制作一个视差背景,但你可以看到那里的代码让它无限滚动。诀窍是创建足够的地面节点来覆盖宽度,并为每一端增加几个额外的节点。当左边的那个完全离开屏幕时(通过检查它的origin.x + size.width值对着视图的原点),你拿起它并将它移到最右边。相反,如果您正在移动另一个方向,并且发现您的最后一个节点完全不在屏幕上,那么您将选择最右边的节点并将其移至开头。

When done correctly it looks smooth and is fairly simple.

如果操作正确,它看起来很平滑,非常简单。