用手指画线,一个物体会沿着那条路走?

时间:2012-08-20 06:08:55

标签: iphone objective-c cocos2d-iphone xcode4.3 gamekit

我是ios游戏开发的新手。现在我想做一个类似“Control Air Flight”“空中交通管制员”的游戏,

  

用户可以使用他们的手指绘制线条,而对象则可以   遵循那条道路

所以,任何人都可以指导我最适合这样开发。可以选择 Cocos2d 吗?或者我必须使用其他任何东西。

如果有人知道已有的教程或任何参考链接,请建议我。

先谢谢。

1 个答案:

答案 0 :(得分:1)

简单地让对象跟随你的手指,实现触摸(只是其中一种方法):

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint toPoint = [touch locationInView:self.view];
    [yourObjectOutlet setCenter:toPoint];
}

在这里,您的对象的中心将遵循您的路径,但您可以通过相应地编辑对象框架的“toPoint”来调整其锚点。

修改

如果要绘制路径,请使对象遵循该路径,如下所示:

//define an NSMutableArray in your header file (do not forget to alloc and init it in viewDidLoad), then:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
   //you begin a new path, clear the array
   [yourPathArray removeAllObjects];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint toPoint = [touch locationInView:self.view];
    //now, save each point in order to make the path
    [yourPathArray addObject:[NSValue valueWithCGPoint:toPoint]];
}

现在你想开始行动:

- (IBAction)startMoving{
   [self goToPointWithIndex:[NSNumber numberWithInt:0]];
}
- (void)goToPointWithIndex:(NSNumber)indexer{
   int toIndex = [indexer intValue];  

   //extract the value from array
   CGPoint toPoint = [(NSValue *)[yourPathArray objectAtIndex:toIndex] CGPointValue];
   //you will repeat this method so make sure you do not get out of array's bounds
   if(indexer < yourPathArray.count){
       [yourObject setCenter:toPoint];
       toIndex++;
       //repeat the method with a new index
       //this method will stop repeating as soon as this "if" gets FALSE
       [self performSelector:@selector(goToPointWithIndex:) with object:[NSNumber numberWithInt:toIndex] afterDelay:0.2];
   }
}

这就是全部!