Frage

-(void)startNewScene{

[self removeAllChildren];//Clear


NSImage *image=[NSImage imageNamed:@"down"];
//image.size=CGSizeMake(self.barWidth*2, self.frame.size.height);

NSColor *color=[NSColor colorWithPatternImage:[NSImage imageNamed:@"down"]];



SKSpriteNode *sprite= [SKSpriteNode spriteNodeWithColor:color size:CGSizeMake(200, 300)];


    sprite.position=CGPointMake(10, CGRectGetMidY(self.frame));



    SKAction *move=[SKAction moveToX:self.frame.size.width duration:1];
    SKAction *goBack= [SKAction moveToX:-self.barWidth duration:0];
    SKAction *action=[SKAction sequence:@[move,goBack]];

    [sprite runAction:[SKAction repeatActionForever:action]];

    [self addChild:sprite];

}

Hi everybody, i want to make a node with a pattern color which is an image but this code doesn't work at all.

I've tried with 3 different images in 3 different formats so i think that's not the problem. I know that the variable image is useless but i've used it to understand if the nsimage was the problem. When i look for the value of image whit a breakpoint it has been created, but its width and height are both 0.

I really can't understand why this doesn't work at all (the node doesn't show up).

War es hilfreich?

Lösung

Sprite Kit nodes with a color property don't support colors using a pattern image. It has to be a solid color, for example one you'd create with colorWithRed:green:blue:alpha:.

You should create your pattern image in an image program and add it to the bundle. If you make the pattern image grayscale you can colorize it at runtime to create various color combinations. For example a checkerboard white/black pattern image and the sprite node's color set to red will give you a black & red pattern. Adjust the sprite node's colorBlendFactor to control the "brightness" of the color component.

Andere Tipps

Here is a link describes how to draw a background with a pattern image

I've made some changes in my Swift's SKScene version:

override func didMoveToView(view: SKView) {


    let bkg = UIImage(named: "Background")
    let backgroundCGImage = bkg!.CGImage //change the string to your image name
    let coverageSize = self.size //the size of the entire image you want tiled
    let textureSize = CGRectMake(0, 0, bkg!.size.width, bkg!.size.height); //the size of the tile.

    UIGraphicsBeginImageContextWithOptions(coverageSize, false, UIScreen.mainScreen().scale);

    let context = UIGraphicsGetCurrentContext()

    CGContextDrawTiledImage(context, textureSize, backgroundCGImage)
    let tiledBackground = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    let backgroundTexture = SKTexture(image: tiledBackground)
    let backgroundTiles = SKSpriteNode(texture: backgroundTexture)
    backgroundTiles.yScale = -1 //upon closer inspection, I noticed my source tile was flipped vertically, so this just flipped it back.
    backgroundTiles.position = CGPointMake(self.size.width/2, self.size.height/2)
    self.addChild(backgroundTiles)

}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top