Sprite Kit:在网格中创建居中的精灵

时间:2014-08-13 20:55:41

标签: ios xcode grid sprite-kit sprite

如果字段为空,我想在网格中创建一个新的精灵。我也希望这个新的精灵在空场中居中。让我们说这个领域是:

1 1 1
1 1 0
1 1 1

每当我点击其中一个填充了1的字段时,精灵就会消失(这就是我想要的,这样可以正常工作)。 每当我点击0时,都会添加一个新的精灵,并带有以下代码:

UITouch *touch = [touches anyObject];
CGPoint positionInScene = [touch locationInNode:self];
SKSpriteNode *touchedNode = (SKSpriteNode *)[self nodeAtPoint:positionInScene];
if(touchedNode.position.x == 0) { // x is 0 when I click an empty field, aka background
    [self addThing:positionInScene];
}

-(void)addThing:(CGPoint)newLocation{
    SKSpriteNode *thing = [SKSpriteNode spriteNodeWithImageNamed:@"thing"];
    thing.location = newLocation;

    [self addChild:thing];

    thing.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:thing.size.width/2];
    thing.physicsBody.dynamic = NO;
    thing.physicsBody.categoryBitMask = somethingCategory;
    thing.physicsBody.contactTestBitMask = somethingElseCategory;
    thing.physicsBody.collisionBitMask = 0;

}

但是,这个精灵以我点击的精确坐标为中心,而不是空场的中心点。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:2)

以下创建一个N x M网格,每个行和每列都有一个精灵。当用户触摸其中一个精灵时,它会旋转精灵然后将其从场景中移除。如果在触摸位置找不到精灵,则会在那里添加精灵。

#define kBoardMinX      25
#define kBoardMinY      25
#define kNumCols        3
#define kNumRows        3
#define kSpacing        40

-(id)initWithSize:(CGSize)size {    
    if (self = [super initWithSize:size]) {
        /* Setup your scene here */

        self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];

        [self addPiecesToScene];

    }
    return self;
}

// Convert row and col to the appropriate point (in scene coordinates) in the grid
- (CGPoint) pointFromRow:(NSInteger)row andCol:(NSInteger)col
{
    return CGPointMake(kBoardMinX+col*kSpacing, kBoardMinY+(kNumRows-row-1)*kSpacing);
}

// If a sprite is found at (row, col) rotate it, else add a sprite there
- (void) rotatePieceAtRow:(NSInteger)row andCol:(NSInteger)col
{
    SKSpriteNode *node = (SKSpriteNode *)[self nodeAtPoint:[self pointFromRow:row andCol:col]];
    if (node && (SKNode *)node != self) {
        SKAction *rotate = [SKAction rotateByAngle:M_PI*2 duration:2];
        SKAction *remove = [SKAction removeFromParent];
        SKAction *rotateThenRemove = [SKAction sequence:@[rotate,remove]];
        [node runAction:rotateThenRemove];
    }
    else {
        [self addPieceAtRow:row andCol:col];
    }
}
- (void) togglePieceAtTouchLocation:(CGPoint)location
{
    // Convert touch location to row and column
    NSInteger col = (NSInteger)roundf((location.x - kBoardMinX) / kSpacing);
    NSInteger row = kNumRows - (NSInteger)roundf((location.y - kBoardMinY) / kSpacing) - 1;
    // Check if the touch was within the grid
    if (col >= 0 && col < kNumCols && row >= 0 && row < kNumRows) {
        [self rotatePieceAtRow:row andCol:col];
    }
}
- (void) addPieceAtRow:(NSInteger)row andCol:(NSInteger)col
{
    CGSize size = CGSizeMake(32, 32);
    SKColor *color = [SKColor whiteColor];
    SKSpriteNode *node = [SKSpriteNode spriteNodeWithColor:color size:size];
    node.position = [self pointFromRow:row andCol:col];
    [self addChild:node];
}

- (void) addPiecesToScene
{
    for (int row=0;row<kNumRows;row++) {
        for (int col=0;col<kNumCols;col++) {
            [self addPieceAtRow:row andCol:col];
        }
    }
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */

    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:self];
        [self togglePieceAtTouchLocation:location];
    }
}