我刚刚在我的精灵工具包塔防游戏中完成了核心功能,现在我正在努力改进游戏。首先,我正在考虑做一个选择菜单来选择要放置的塔。这可以在诸如Kingdom Rush等游戏中完成,您可以在4种不同的塔之间进行选择。但是,我有点失去了如何做到这一点。
http://i.ytimg.com/vi/ihc4G76jA_Q/0.jpg //塔选择的形象
任何建议都将不胜感激!
感谢
答案 0 :(得分:2)
正如所承诺的那样,下载了一些代码让你走了,我现在还没有我的mac可用,所以可能会有一些错别字,但概念就在那里:
首先通过定义地图上哪些区域能够容纳塔,这可以通过几种方式完成,但是因为它是SpriteKit,我将使用一个名为towerPlot
的精灵来表示可以占用的区域塔。
定义一个方法,将地图添加到某个位置,例如:
-(void)addTowerPlotAt:(CGPoint)location{
SKSpriteNode* plot = [SKSpriteNode spriteNodeWithImageNamed: "@plotImage";
plot.location = location;
plot.name = @"towerPlot";
plot.zPosition = 1; //if needed
[self addChild: plot];
}
你的viewDidLoad方法添加如下内容:
[self addTowerPlotAt:CGPointMake(someX, someY)];
[self addTowerPlotAt:CGPointMake(otherX, otherY)];
为了将你的情节添加到正确的位置。
接下来是检查触摸是否位于图上;
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
//if the node is a plot, bring up the menu
if ([node.name isEqualToString:@"towerPlot"]) {
//add the menu frame
SKSpriteNode* towerMenu = [SKSpriteNode spriteNodeWithImageNamed: "@TowerMenu";
towerMenu.position = node.position;
SKSpriteNode* towerType1= [SKSpriteNode spriteNodeWithImageNamed: "@Tower1";
towerType1.position = CGPointMake(someX, someY); /* remember this is relative to the menu, the next tower type has an example */
towerType1.name = @"towerType1";
[towerMenu addChild:towerType1];
SKSpriteNode* towerType2= [SKSpriteNode spriteNodeWithImageNamed: "@Tower2";
towerType2.position = CGPointMake((towerType2.size.width/2)-(towerMenu.size.width/2), 0); /* would add it to to the middle left of the menu */
towerType1.name = @"towerType2";
[towerMenu addChild:towerType2];
//add and position as many tower types as you need
[self addChild: towerMenu];
}
if ([node.name isEqualToString:@"towerType1"]) {
//code to build the tower here
}
}
您还需要其他一些小工具,例如在选择塔后删除菜单,并确保如果奇怪地分层,则点击会返回节点名称,但这是我认为的主旨。