Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 用手指画一条线,一个物体就会沿着这条路径走?_Iphone_Objective C_Cocos2d Iphone_Xcode4.3_Gamekit - Fatal编程技术网

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

Iphone 用手指画一条线,一个物体就会沿着这条路径走?,iphone,objective-c,cocos2d-iphone,xcode4.3,gamekit,Iphone,Objective C,Cocos2d Iphone,Xcode4.3,Gamekit,我是ios游戏开发新手。现在我想制作一个类似于“控制空中飞行”和“空中交通管制员”的游戏 在这里,用户可以用手指画一条线,而对象将 沿着这条路走 所以,任何人都可以指导我,哪一个最适合这样发展。Cocos2d最适合吗?或者其他我必须用的东西 另外,如果有人知道已经有一个教程或任何参考链接,请建议我 提前感谢。要简单地让对象跟随您的手指,请实现触摸(以及其中一种方法): 在这里,对象的中心将跟随您的路径,但您可以通过根据对象的帧编辑“toPoint”来调整其定位点 编辑 如果要绘制路径,请使对象跟

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

在这里,用户可以用手指画一条线,而对象将 沿着这条路走

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

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


提前感谢。

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

在这里,对象的中心将跟随您的路径,但您可以通过根据对象的帧编辑“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];
   }
}
-(iAction)开始移动{
[self goToPointWithIndex:[NSNumber Number Withint:0]];
}
-(void)goToPointWithIndex:(NSNumber)索引器{
int-toIndex=[indexer-intValue];
//从数组中提取值
CGPoint-toPoint=[(NSValue*)[yourPathArray-objectAtIndex:toIndex]CGPointValue];
//您将重复此方法,以确保不会超出数组的边界
if(索引器

就这些

根据你的回答,当我移动手指时,物体也会移动(类似于物体充当指针)。我想沿着我画的特定路径跟随物体。请纠正我的错误。你是说你想先画路径,然后点击一个按钮,该按钮将启动该路径下的对象?它正在工作。只需对代码进行微小更改,我就完成了..在指向NSNumber的指针中,检查intValue的拼写。。非常感谢你对我的帮助……是的,谢谢,你说得对,我只是写了没有Xcode拼写检查的代码。我很高兴这有助于你完成任务,检查了我接受的ans。这对我很有用。
- (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];
   }
}