3

我正在使用COCOS2D为 iPhone 开发游戏。

在那,当用户将手指从一个点拖动到另一个点时,我需要画一条线。据我所知,我需要Touches Moved method从我可以得到分数的地方做到这一点。

但我不知道该怎么做。有人可以帮我吗?

4

2 回答 2

6
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event  
{
        UITouch  *theTouch = [touches anyObject];
    CGPoint touchLocation = [theTouch locationInView:[theTouch view] ];
    cgfloat x = touchLocation.x;
    cgfloat y= touchLocation.y;
printf("move x=%f,y=%f",x,y);   
}

试试上面的代码。touches moved它会在iphone中获取坐标点。

要画线,请使用以下内容:

-void draw
{
here is the code for line draw.
}

在 update 方法中更新这个函数。

于 2010-12-27T13:03:16.780 回答
6

起亚奥拉。无聊迫使我就这个话题提供答案。

图层部分(即@interface GetMyTouches : CCLayer):

-(void) ccTouchesMoved:(NSSet *)inappropriateTouches withEvent:(UIEvent *)event
{
    UITouch *touchMyMinge = [inappropriateTouches anyObject];

    CGPoint currentTouchArea = [touchMyMinge locationInView:[touchMyminge view] ];
    CGPoint lastTouchArea = [touchMyMinge previousLocationInView:[touchMyMinge view]];

    // flip belly up. no one likes being entered from behind.
    currentTouchArea = [[CCDirector sharedDirector] convertToGL:currentTouchArea];
    lastTouchArea = [[CCDirector sharedDirector] convertToGL:lastTouchArea];

    // throw to console my inappropriate touches
    NSLog(@"current x=%2f,y=%2f",currentTouchArea.x, currentTouchArea.y);
    NSLog(@"last x=%2f,y=%2f",lastTouchArea.x, lastTouchArea.y);  

   // add my touches to the naughty touch array 
   naughtyTouchArray addObject:NSStringFromCGPoint(currentTouchArea)];
   naughtyTouchArray addObject:NSStringFromCGPoint(lastTouchArea)];
}

节点部分(即@interface DrawMyTouch:CCNode):

@implementation DrawMyTouch

-(id) init
{
    if( (self=[super init])) 
    { }
    return self;
}

-(void)draw
{
    glEnable(GL_LINE_SMOOTH);

    for(int i = 0; i < [naughtyTouchArray count]; i+=2)
    {
        start = CGPointFromString([naughtyTouchArray objectAtIndex:i]);
        end = CGPointFromString([naughtyTouchArray objectAtIndex:i+1]);

        ccDrawLine(start, end);
    }
}

@end

第二部分(即@interface GetMyTouches : CCLayer):

-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{   
    DrawMyTouch *line = [DrawMyTouch node];
    [self addChild: line];
}

记住触摸很容易。触摸时知道自己在做什么不是火箭科学。

最后,如果你不明白我发布的任何内容......拿起烘焙。世界需要更多的巧克力蛋糕生产商。

澄清:

  1. 没有人会从剪切和粘贴中学到东西~这段代码永远不会在没有爱抚的情况下工作
  2. 如果你看不到幽默,那你就错了职业

值得注意的是,我喜欢美味的巧克力蛋糕。世界确实需要更多出色的面包师。这不是侮辱,是鼓励。

“看看广场外,找到充满让生活值得生活的知识的圆圈”〜Aenesidemus。

于 2011-01-09T02:22:34.117 回答