【问题标题】:How to re-color a sprite using SpriteKit swift如何使用 SpriteKit 快速重新着色精灵
【发布时间】:2014-08-11 20:35:51
【问题描述】:

我正在创建一个游戏,您必须用彩弹枪射击从天上掉下来的不同敌人,“彩弹”是一种随机颜色,其中彩弹来自继承自 SKShapeNode 的类。当彩弹击中敌人时,我想创建一个飞溅动画,如果每个颜色相同,这样做会很简单。如何运行帧颜色继承原始项目符号颜色的动画?或者我可以使用 SKEmitterNode 类以某种方式进行类似的操作吗?

这里是 Bullet 类和 addBullet 类

class Bullet:SKShapeNode {
    var color:SKColor?
    var dx:CGFloat?
    var dy:CGFloat?
    var move:SKAction {
    get {
        if dx != nil && dy != nil {
            return SKAction.moveByX(dx!, y: dy!, duration: 0.4)
        } else {
            return SKAction.moveByX(0, y: 0, duration: 0)
        }
    }
}

required override init() {
    super.init()
    let circle = CGRectMake(0, 0, 40, 40)
    self.antialiased = true
    self.setScale(0.5)
    self.path = UIBezierPath(ovalInRect: circle).CGPath
    self.lineWidth = 1
    self.color = Random.randomColor()
    if let c = self.color {
        self.fillColor = c
        self.strokeColor = c
    } else {
        self.fillColor = SKColor.whiteColor()
        self.strokeColor = SKColor.whiteColor()
    }
    self.physicsBody = SKPhysicsBody(circleOfRadius: circle.width/2)
    self.physicsBody.affectedByGravity = false
    self.physicsBody.categoryBitMask = Collision.bulletCategory
    self.physicsBody.contactTestBitMask = Collision.enemyCategory
}


required init(coder aDecoder: NSCoder!) {
    super.init(coder: aDecoder)
}

}

func generateBullet(loc:CGPoint) {
    let dx = loc.x - self.player.position.x
    let dy = loc.y - self.player.position.y
    let norm = CGVectorMake(dx, dy).normalize()
    let b = Bullet()
    b.dx = norm.dx * 600
    b.dy = norm.dy * 600
    let height = self.player.size.height/4
    b.position = CGPointMake(self.player.position.x + norm.dx * height, self.player.position.y + norm.dy * height)
    self.addChild(b)
    b.runAction(b.move, completion: {
        b.removeFromParent();
    })
}

【问题讨论】:

    标签: swift sprite-kit xcode6


    【解决方案1】:

    尝试将此添加到您的 SKScene 子类中...

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        /* Called when a touch begins */
    
        for touch: AnyObject in touches {
            let location = touch.locationInNode(self)
    
            // Add paintball splat at tap point
            self.splat(location,color:self.pickColor())
        }
    }
    
    func skRandf() -> Float {
        return Float(Float(random()) / Float(RAND_MAX))
    }
    
    func skRand(low:Float, high:Float) -> Float {
        return skRandf() * (high - low) + low;
    }
    
    func splat(location:CGPoint, color:SKColor) -> Void
    {
        let count = arc4random_uniform(7)+6
        let node = SKNode.node()
        let core = SKShapeNode(path: self.core())
    
        core.position = location;
        core.zRotation = CGFloat(skRand(0.0, high: Float(M_PI*2)))
        core.xScale = CGFloat(skRand(0.1, high: 0.25))
        core.yScale = CGFloat(skRand(0.1, high: 0.25))
        core.fillColor = color
    
        node.addChild(core)
    
        for index in 1...count {
            let shape = SKShapeNode(path:self.paintDrop())
    
            shape.xScale = CGFloat(skRand(0.075, high: 0.15))
            shape.yScale = CGFloat(skRand(0.05, high: 0.25))
    
            shape.zRotation = CGFloat(skRand(0.0, high: Float(M_PI*2)))
    
            shape.position = location
            shape.fillColor = color
            shape.strokeColor = color
    
            node.addChild(shape)
        }
        self.addChild(node)
    }
    
    func core() -> CGPathRef
    {
        var bezierPath = UIBezierPath()
        bezierPath.moveToPoint(CGPointMake(-19.5, -8.5))
        bezierPath.addCurveToPoint(CGPointMake(-1.5, -16.5), controlPoint1: CGPointMake(-16.25, -16.25), controlPoint2: CGPointMake(-9.5, -14))
        bezierPath.addCurveToPoint(CGPointMake(18.5, -8.5), controlPoint1: CGPointMake(6.5, -19), controlPoint2: CGPointMake(13.75, -16.25))
        bezierPath.addCurveToPoint(CGPointMake(15.5, 12.5), controlPoint1: CGPointMake(23.25, -0.75), controlPoint2: CGPointMake(19.25, 5))
        bezierPath.addCurveToPoint(CGPointMake(1.5, 20.5), controlPoint1: CGPointMake(11.75, 20), controlPoint2: CGPointMake(6.5, 20.5))
        bezierPath.addCurveToPoint(CGPointMake(-13.5, 12.5), controlPoint1: CGPointMake(-3.5, 20.5), controlPoint2: CGPointMake(-9.25, 17.5))
        bezierPath.addCurveToPoint(CGPointMake(-19.5, -8.5), controlPoint1: CGPointMake(-17.75, 7.5), controlPoint2: CGPointMake(-22.75, -0.75))
        bezierPath.closePath()
        return bezierPath.CGPath;
    }
    
    
    func paintDrop() -> CGPathRef {
        var bezierPath = UIBezierPath()
        bezierPath.moveToPoint(CGPointMake(-8.5, 81.5))
        bezierPath.addCurveToPoint(CGPointMake(-15.5, 114.5), controlPoint1: CGPointMake(-9.5, 89.5), controlPoint2: CGPointMake(-15.5, 104.5))
        bezierPath.addCurveToPoint(CGPointMake(1.5, 130.5), controlPoint1: CGPointMake(-15.5, 124.5), controlPoint2: CGPointMake(-8.5, 130.5))
        bezierPath.addCurveToPoint(CGPointMake(17.5, 114.5), controlPoint1: CGPointMake(11.5, 130.5), controlPoint2: CGPointMake(17.5, 123.5))
        bezierPath.addCurveToPoint(CGPointMake(9.5, 81.5), controlPoint1: CGPointMake(17.5, 105.5), controlPoint2: CGPointMake(10.5, 88.5))
        bezierPath.addCurveToPoint(CGPointMake(9.5, 0.5), controlPoint1: CGPointMake(8.5, 74.5), controlPoint2: CGPointMake(9.5, 0.5))
        bezierPath.addLineToPoint(CGPointMake(-8.5, 0.5))
        bezierPath.addCurveToPoint(CGPointMake(-8.5, 81.5), controlPoint1: CGPointMake(-8.5, 0.5), controlPoint2: CGPointMake(-8.32, 80.09))
        bezierPath.closePath()
    
        return bezierPath.CGPath
    }
    
    func pickColor() -> SKColor
    {
        var color:SKColor
        switch (arc4random_uniform(6)) {
        case 0:
            color = SKColor.greenColor()
        case 1:
            color = SKColor.blueColor()
        case 2:
            color = SKColor.yellowColor()
        case 3:
            color = SKColor.redColor()
        case 4:
            color = SKColor.purpleColor()
        default:
            color = SKColor.magentaColor()
        }
        return color
    }
    

    【讨论】:

      猜你喜欢
      • 2019-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多