【问题标题】:draw line using finger and an object will follow that path?用手指画线,一个物体会沿着这条路走吗?
【发布时间】:2012-08-20 06:08:55
【问题描述】:

我是 ios 游戏开发新手。现在我想做一个类似 "Control Air Flight""Air Traffic Controller" 的游戏,

用户可以用他们的手指画线,一个对象会 走那条路

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

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

提前致谢。

【问题讨论】:

  • 嗨,你的任务做得怎么样
  • 检查了我接受的答案。这对我很有用。

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


【解决方案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];
   }
}

就是这样!

【讨论】:

  • 根据您的回答,当我移动手指时,对象也会移动(它像对象一样用作指针)。我想在我绘制的特定路径上跟随对象。请纠正我的错误。
  • 所以你的意思是你想先绘制路径,然后点击一个按钮,该按钮将沿着该路径启动对象?
  • 它的工作。只是对你的代码进行了微小的更改,它为我完成了..assin 指向 NSNumber 的指针,检查 intValue 的拼写..非常感谢你的帮助..
  • 是的,谢谢,你是对的,我只是在没有 Xcode 检查拼写的情况下编写了该代码。我很高兴这有帮助
猜你喜欢
  • 1970-01-01
  • 2013-09-04
  • 2013-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多