简体   繁体   English

快速图像堆叠/复制-iOS

[英]swift images stacking / duplicating - iOS

i've recently started coding and my first attempt is an iOS game, part of the game I'm working on has the following elements. 我最近开始编码,而我的第一个尝试是iOS游戏,我正在开发的游戏的一部分包含以下元素。

Scrolling foreground and background A cat that jumps when clicked A missile raining down from above. 滚动前景和背景单击时会跳的猫导弹从天而降。

I've recently managed to spawn the missile every few seconds however in doing so I think i've messed up something. 我最近设法每隔几秒钟产生一次导弹,但是这样做我觉得我搞砸了。

Now i have two errors, the first is the cat, foreground and background images duplicating and stacking themselves all over the screen and the other is a build error under override func update(currentTime . I've commented out the cat's Z rotation as otherwise the game wont build. 现在我有两个错误,第一个是猫,前景和背景图像在整个屏幕上复制和堆叠,另一个是override func update(currentTime下的生成错误。我已经注释掉了猫的Z轴旋转,否则游戏不会建立。

If anyone can help me work out how to fix I'd be really grateful. 如果有人可以帮助我解决问题,我将非常感激。

Sorry if it's overkill, but here is the code.... 抱歉,如果使用率过高,但是代码如下。

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {


var cat = SKSpriteNode()
var crow = SKSpriteNode()
var crowTexture1 = SKTexture()
var skyColor = SKColor()
var moveAndRemoveCrow = SKAction()
var spawn = SKAction()
var lastMissileAdded : NSTimeInterval = 0.0

let missileVelocity : CGFloat = 5.0



override func didMoveToView(view: SKView) {

    self.addMissile()

}


func addMissile() {
    // Initializing missile node
    var missile = SKSpriteNode(imageNamed: "red-missile")
    missile.setScale(0.15)

    // Adding SpriteKit physics body for collision detection
    missile.physicsBody = SKPhysicsBody(rectangleOfSize: missile.size)
    //      missile.physicsBody?.categoryBitMask = UInt32(obstacleCategory)
    missile.physicsBody?.dynamic = true
    //      missile.physicsBody?.contactTestBitMask = UInt32(shipCategory)
    missile.physicsBody?.collisionBitMask = 0
    missile.physicsBody?.usesPreciseCollisionDetection = true
    missile.name = "missile"

    // Selecting random y position for missile
    var random : CGFloat = CGFloat(arc4random_uniform(300))
    missile.position = CGPoint(x: self.frame.size.width / 1.8, y: self.frame.size.height / 1.2 )
    self.addChild(missile)
}

func moveObstacle() {
    self.enumerateChildNodesWithName("missile", usingBlock: { (node, stop) -> Void in
        if let obstacle = node as? SKSpriteNode {
            obstacle.position = CGPoint(x: obstacle.position.x - self.missileVelocity, y: obstacle.position.y)
            if obstacle.position.x < 0 {
                obstacle.removeFromParent()
            }
        }
    })


    skyColor = SKColor(red:113.0/255.0, green:197.0/255.0, blue:207.0/255.0, alpha:1.0)
    self.backgroundColor = skyColor

    var catTexture1 = SKTexture(imageNamed: "Cat1")
    catTexture1.filteringMode = SKTextureFilteringMode.Nearest
    var catTexture2 = SKTexture(imageNamed: "Cat2")
    catTexture2.filteringMode = SKTextureFilteringMode.Nearest

    var anim = SKAction.animateWithTextures([catTexture1, catTexture2], timePerFrame: 0.2)
    var run = SKAction.repeatActionForever(anim)

    cat = SKSpriteNode(texture: catTexture1)
    cat.position = CGPoint(x: self.frame.size.width / 2.2, y: self.frame.size.height / 7.0 )
    cat.runAction(run)

    cat.physicsBody = SKPhysicsBody(circleOfRadius: cat.size.height / 2.0)
    cat.physicsBody!.dynamic = true
    cat.physicsBody!.allowsRotation = false

    self.addChild(cat)


    var groundTexture = SKTexture(imageNamed: "Ground")
    groundTexture.filteringMode = SKTextureFilteringMode.Nearest

    var moveGroundSprite = SKAction.moveByX(-groundTexture.size().width, y: 0, duration: NSTimeInterval(0.01 * groundTexture.size().width))
    var resetGroundSprite = SKAction.moveByX(groundTexture.size().width, y: 0, duration: 0.0)
    var moveGroundSpritesForever = SKAction.repeatActionForever(SKAction.sequence([moveGroundSprite,resetGroundSprite]))

    for var i:CGFloat = 0; i < 2 + self.frame.size.width / ( groundTexture.size().width); ++i {
        var sprite = SKSpriteNode(texture: groundTexture)
        sprite.position = CGPointMake(i * sprite.size.width, sprite.size.height / 2)
        sprite.runAction(moveGroundSpritesForever)
        self.addChild(sprite)

    }

    var dummy = SKNode()
    dummy.position = CGPointMake(0, groundTexture.size().height / 2)
    dummy.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width, groundTexture.size().height))
    dummy.physicsBody!.dynamic = false
    self.addChild(dummy)

    var skylineTexture = SKTexture(imageNamed: "Skyline")
    skylineTexture.filteringMode = SKTextureFilteringMode.Nearest

    var moveSkylineSprite = SKAction.moveByX(-skylineTexture.size().width, y: 0, duration: NSTimeInterval(0.1 * skylineTexture.size().width))
    var resetSkylineSprite = SKAction.moveByX(skylineTexture.size().width, y: 0, duration: 0.0)
    var moveSkylineSpritesForever = SKAction.repeatActionForever(SKAction.sequence([moveSkylineSprite,resetSkylineSprite]))

    for var i:CGFloat = 0; i < 2 + self.frame.size.width / ( skylineTexture.size().width); ++i {
        var sprite = SKSpriteNode(texture: skylineTexture)
        sprite.zPosition = -20;
        sprite.position = CGPointMake(i * sprite.size.width, sprite.size.height / 2 + groundTexture.size().height)
        sprite.runAction(moveSkylineSpritesForever)
        self.addChild(sprite)
    }

}

override func  touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    /* Called when a touch begins */

    cat.physicsBody!.velocity = CGVectorMake(0, 0)
    cat.physicsBody!.applyImpulse(CGVectorMake(0, 15))

}

func clamp(min: CGFloat, max: CGFloat, value: CGFloat) -> CGFloat {
    if( value > max ) {
        return max;
    } else if( value < min ) {
        return min;
    } else {
        return value;
    }

}
override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */
    //       cat.zRotation = self.clamp( -1, max: 0.5, value: cat.physicsBody!.velocity.dy * ( cat.physicsBody!.velocity.dy < 0 ? 0.003 : 0.001 ) );
    if currentTime - self.lastMissileAdded > 1 {
        self.lastMissileAdded = currentTime + 1
        self.addMissile()
    }

    self.moveObstacle()
}


}

I've figured it out, 我知道了

I had functions in 'didMoveToView'. 我在“ didMoveToView”中有函数。 After moving them outside the block the code now runs smoothly. 将它们移出块后,代码现在可以平稳运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM