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



我是ios游戏开发的新手。现在我想制作一个类似的游戏,如"控制空中飞行"《空中交通管制员》

用户可以用手指画线,对象将按照的路径

所以,任何人都可以指导我哪一个最适合这样发展。Cocos2d是否最适合它?或者我必须用的任何其他东西。

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

提前感谢。

要简单地让对象跟随你的手指,请实现触摸(以及它的一种方法):

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

仅此而已!

相关内容

最新更新