0

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

用户可以用他们的手指画线,并且一个对象将沿着该路径

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

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

提前致谢。

4

1 回答 1

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];
   }
}

就这样!

于 2012-08-20T06:17:45.890 回答