27

我正在使用名为 Sprite Kit 的新 API 开发 iOS7 游戏。我想水平旋转 SKSpriteNode 图像/纹理。我已经尝试过首先镜像图像,然后创建一个 SKTexture,最后将它应用到 SKSpriteNode 但它不起作用。有没有办法做到这一点?或者我应该有不同的图像?

4

4 回答 4

37

如果您只是想沿轴翻转精灵,您可以执行以下操作:

sprite.xScale = -1.0;
于 2013-10-03T15:18:49.520 回答
26

您可以使用此代码在 x 轴之间翻转:

spriteNode.xScale = spriteNode.xScale * -1;

但要小心你可能会失去一些物理体的属性,我强烈建议你以这种方式使用 xScale:

spriteNodeBody = [SKNode node];
spriteNodeBody.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:spriteNode.size];
spriteNodeBody.physicsBody.categoryBitMask = CNPhysicsCategoryPlayer;
spriteNodeBody.physicsBody.collisionBitMask = CNPhysicsCategoryBall;

[spriteNodeBody addChild:spriteNode];
[self addChild:spriteNodeBody];

现在您可以安全地使用:

    spriteNode.xScale = spriteNode.xScale * -1;
于 2014-07-26T07:45:53.733 回答
0

我将它用于我的右手或左手精灵:

在Objective-C中框架的水平翻转的帮助下

BOOL leftHanded = YES;

SKSpriteNode *sprite;

if (leftHanded) {  //my textures point east, so I flip vertically
    SKTexture *texture = [SKTexture textureWithImageNamed:@"figure-step0"];
    CIFilter *filter = [CIFilter filterWithName:@"CIAffineTransform"];
    [filter setValue:[CIImage imageWithCGImage:[texture CGImage]] forKey:kCIInputImageKey];

#ifdef IOS_BLOCK  //set these up with defines
    CGAffineTransform flipTransform = CGAffineTransformMakeScale(1.0f, -1.0f); // vert
    [filter setValue:[NSValue valueWithBytes:&flipTransform
                                objCType:@encode(CGAffineTransform)]
                                forKey:@"inputTransform"];
#else //OSX_BLOCK
    NSAffineTransform* flipTransform = [NSAffineTransform transform];
    [flipTransform scaleXBy:1.0f yBy: -1.0f]; // vert
    [filter setValue:flipTransform forKey:@"inputTransform"];
#endif
    sprite = [SKSpriteNode spriteNodeWithTexture:
                     [texture textureByApplyingCIFilter:filter]];
} else {
    sprite = [SKSpriteNode spriteNodeWithImageNamed:@"figure-step0"];
}

你也可以对动画帧做同样的事情。适用于 iOS 或 OS X。

于 2015-08-26T23:38:25.497 回答
0

这是我用 Swift 2.x编写的解决方案,通常我更喜欢在landscape游戏中使用模式,所以我编写了这个扩展:

extension SKTexture {
    class func flipImage(name:String,flipHoriz:Bool,flipVert:Bool)->SKTexture {
        if !flipHoriz && !flipVert {
            return SKTexture.init(imageNamed: name)
        }
        let image = UIImage(named:name)

        UIGraphicsBeginImageContext(image!.size)
        let context = UIGraphicsGetCurrentContext()

        if !flipHoriz && flipVert {
            // Do nothing, X is flipped normally in a Core Graphics Context 
            // but in landscape is inverted so this is Y
        } else
        if flipHoriz && !flipVert{
            // fix X axis but is inverted so fix Y axis
            CGContextTranslateCTM(context, 0, image!.size.height)
            CGContextScaleCTM(context, 1.0, -1.0)
            // flip Y but is inverted so flip X here
            CGContextTranslateCTM(context, image!.size.width, 0)
            CGContextScaleCTM(context, -1.0, 1.0)
        } else
        if flipHoriz && flipVert {
            // flip Y but is inverted so flip X here
            CGContextTranslateCTM(context, image!.size.width, 0)
            CGContextScaleCTM(context, -1.0, 1.0)
        }

        CGContextDrawImage(context, CGRectMake(0.0, 0.0, image!.size.width, image!.size.height), image!.CGImage)

        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext();
        return SKTexture(image: newImage)
    }
}

用法:

let spriteTxt = SKTexture.flipImage(imageName, flipHoriz: true, flipVert: false)

PS:如果您想要相同的功能但要portrait模式,请参考此答案

于 2016-08-06T10:23:02.827 回答