开发者

Couting Moves made for Puzzle [COCOS2D]

Im back again sorry! :)

Just need a quick hand if possible, i want to be able to count the number of moves that are made in this slider puzzle that im building. What ive done so far is:

.h

int moves;
CCLabel *moveLabel;

.m

moveLabel = [[CCLabel labelWithString: [NSString stringWithFormat:@"Moves: %d", moves] dimensions: CGSizeMake(130, 27) alignment: UITextAlignmentRight fontName:@"Marker Felt" fontSize: 25.0] retain]; 
    moveLabel.position = ccp(55,430); 
    [self schedule: @selector(tick3:) interval:1.0];
[self addChild: moveLabel];

The method that deals with the swapping of the tiles is this, so i added moves++; to it at the bottom:

-(void) changeWithTileA: (Tile *) a TileB: (Tile *) b sel : (SEL) sel{
    CCAction *actionA = [CCSequence actions:[CCMoveTo actionWithDuration:kMoveTileTime position:[b pixPosition]], [CCCallFuncND actionWithTarget:self selector:sel data: a], nil];

    CCAction *actionB = [CCSequence actions:[CCMoveTo actionWithDuration:kMoveTileTime position:[a pixPosition]], [CCCallFuncND actionWithTarget:self 开发者_高级运维selector:sel data: b], nil];
    [a.sprite runAction:actionA];
    [b.sprite runAction:actionB];

    [a trade:b];

    moves++;
}

Im not sure if this is the correct way to do it, or how to reference that method here:

[self schedule: @selector(   ) interval:1.0];

If anyone could help that would be great :)

Cheers


You need something that updates the label. You can have changeWithTileA do this directly, or you could have changeWithTileA call NSNotificationCenter and have a separate method that listens for the notification and updates the label's text. The latter is the better way if you ever intend to have something else happen whenever the move count changes.

The code to do the update is:

[moveLabel setString:[NSString stringWithFormat:@"Moves: %d", moves]];

However, note that the reference says that setString is a slow operation and recommends using CCLabelAtlas instead of CCLabel.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜