发牌、玩牌
Card dealing, playing
问题
我从来没有做过卡牌游戏,现在遇到了一些困难。
但是,我已经设法创建了套牌等。
-(NSMutableArray *)arrayWithDeck:(id)sender
{
[sender removeAllObjects];
NSArray *faces = [[NSArray alloc] initWithObjects:@"A",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"J",@"Q",@"K", nil];
NSArray *suits = [[NSArray alloc] initWithObjects:@"h",@"d",@"c",@"s", nil];
for (int i = 0; i < 52; i++) {
NSString *cardToAdd = [NSString stringWithFormat:@"%@%@", faces[i % 13], suits[i / 13]];
[sender addObject:cardToAdd];
}
return sender;
}
然后发牌给玩家(目前只发给一个玩家)
-(void) dealPlayersWithPlayers: (int) players withDealtCards: (int) dealt {
if (players == 2){
__block float add = 0;
for (int i = 0; i < dealt; i++) {
add = add + ((self.frame.size.width / 100) * 10);
NSString *imageName = [NSString stringWithFormat:@"%@", deck[i]];
NSString *bundle = [[NSBundle mainBundle] pathForResource:imageName ofType:@"png"];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:bundle];
SKTexture *texture = [SKTexture textureWithImage:image];
cardDisplay = [SKSpriteNode spriteNodeWithTexture:texture];
cardDisplay.size = CGSizeMake(104, 144);
cardDisplay.anchorPoint = CGPointMake(0.5, 0.5);
cardDisplay.position = CGPointMake(-self.frame.size.width/2.5 + add, -218);
cardDisplay.zPosition = 1;
cardDisplay.userInteractionEnabled = NO;
cardDisplay.name = [NSString stringWithFormat:@"card"];
[self addChild:cardDisplay];
}
}
}
然后当 touchesBegan 时,它会在卡片上动画到屏幕中央,即 pre-phase 对应 "lay cards" 按钮。但是,我真的很难找到一种方法来跟踪按下的卡片。即,卡片是 Js、Ah、8c 还是其他任何东西,所以它显然可以使用,但是 SKSpriteNode.name 已经用于检测 touchesBegan。
我遇到的另一个问题是打牌的时间。他们搞砸了 z-index。但是,解决这个问题的一个简单方法是继续递增 z-index 但这是最好的方法吗?我这里的例子说明了我在说什么。
我想说的是,您让它们渲染并向上移动,这是一个良好的开端。
不过,我的建议是查看 MVC(模型视图控制器)方法来解决该问题。将玩过的牌的信息和玩家拥有的牌保存在一个单独的对象中,与您的视图无关。这样当你触摸你的卡片时,你可以让你的控制器与你的模型一起识别它并决定接下来会发生什么。如果您仅在 SKSpriteNode 上中继并查看其名称而没有指向它的指针来与任何东西进行比较,那么管理游戏将非常困难。
就您的 z 索引排序而言,您的模型会知道哪张牌先添加到您的手中,然后您的控制器可以通知视图适当的位置和 z 索引。
至少我会考虑子类化 SKSpriteNode 并至少制作一个 CardSpriteNode 然后你不必查看触摸的精灵名称并且可以检查它是否是 CardSpriteNode。
if ([node isKindOfClass:[CardSpriteNode class]])
//handle touch logic if a card
MVC 是一个简单的概念,我会看一些关于它的文章。每个人对于可以看到什么的方法略有不同,但都同意将信息分开。
希望对您有所帮助。
问题
我从来没有做过卡牌游戏,现在遇到了一些困难。
但是,我已经设法创建了套牌等。
-(NSMutableArray *)arrayWithDeck:(id)sender
{
[sender removeAllObjects];
NSArray *faces = [[NSArray alloc] initWithObjects:@"A",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"J",@"Q",@"K", nil];
NSArray *suits = [[NSArray alloc] initWithObjects:@"h",@"d",@"c",@"s", nil];
for (int i = 0; i < 52; i++) {
NSString *cardToAdd = [NSString stringWithFormat:@"%@%@", faces[i % 13], suits[i / 13]];
[sender addObject:cardToAdd];
}
return sender;
}
然后发牌给玩家(目前只发给一个玩家)
-(void) dealPlayersWithPlayers: (int) players withDealtCards: (int) dealt {
if (players == 2){
__block float add = 0;
for (int i = 0; i < dealt; i++) {
add = add + ((self.frame.size.width / 100) * 10);
NSString *imageName = [NSString stringWithFormat:@"%@", deck[i]];
NSString *bundle = [[NSBundle mainBundle] pathForResource:imageName ofType:@"png"];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:bundle];
SKTexture *texture = [SKTexture textureWithImage:image];
cardDisplay = [SKSpriteNode spriteNodeWithTexture:texture];
cardDisplay.size = CGSizeMake(104, 144);
cardDisplay.anchorPoint = CGPointMake(0.5, 0.5);
cardDisplay.position = CGPointMake(-self.frame.size.width/2.5 + add, -218);
cardDisplay.zPosition = 1;
cardDisplay.userInteractionEnabled = NO;
cardDisplay.name = [NSString stringWithFormat:@"card"];
[self addChild:cardDisplay];
}
}
}
然后当 touchesBegan 时,它会在卡片上动画到屏幕中央,即 pre-phase 对应 "lay cards" 按钮。但是,我真的很难找到一种方法来跟踪按下的卡片。即,卡片是 Js、Ah、8c 还是其他任何东西,所以它显然可以使用,但是 SKSpriteNode.name 已经用于检测 touchesBegan。
我遇到的另一个问题是打牌的时间。他们搞砸了 z-index。但是,解决这个问题的一个简单方法是继续递增 z-index 但这是最好的方法吗?我这里的例子说明了我在说什么。
我想说的是,您让它们渲染并向上移动,这是一个良好的开端。
不过,我的建议是查看 MVC(模型视图控制器)方法来解决该问题。将玩过的牌的信息和玩家拥有的牌保存在一个单独的对象中,与您的视图无关。这样当你触摸你的卡片时,你可以让你的控制器与你的模型一起识别它并决定接下来会发生什么。如果您仅在 SKSpriteNode 上中继并查看其名称而没有指向它的指针来与任何东西进行比较,那么管理游戏将非常困难。
就您的 z 索引排序而言,您的模型会知道哪张牌先添加到您的手中,然后您的控制器可以通知视图适当的位置和 z 索引。
至少我会考虑子类化 SKSpriteNode 并至少制作一个 CardSpriteNode 然后你不必查看触摸的精灵名称并且可以检查它是否是 CardSpriteNode。
if ([node isKindOfClass:[CardSpriteNode class]])
//handle touch logic if a card
MVC 是一个简单的概念,我会看一些关于它的文章。每个人对于可以看到什么的方法略有不同,但都同意将信息分开。
希望对您有所帮助。