1

根据我上一个问题中guru的建议,我在我的 init 方法中添加了这段代码,以防止在精灵的透明区域发生触摸事件:

    path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, endTouch.x, endTouch.y);
    //250(y) is the height of the path, 50(x) is the width
    //set your width and height to be the same size as the non-transparent part of your sprite
    CGPathAddLineToPoint(path, NULL, 0, 250);
    CGPathAddLineToPoint(path, NULL, 50, 0);
    CGPathCloseSubpath(path);

然后我像这样修改了我的触摸事件:

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if(gameStat == TRUE)
{
    UITouch *touch=[touches anyObject];
    CGPoint loc=[touch locationInView:[touch view]];
    loc=[[CCDirector sharedDirector] convertToGL:loc];
    beginTouch = loc;

    for(int i = 0; i < [sprArray count]; i++)
    {
        CCSprite *sprite = (CCSprite *)[sprArray objectAtIndex:i];
        if(CGRectContainsPoint([sprite boundingBox], loc))
        {
            selectedSprite = sprite;
            //test the path
            loc = [selectedSprite convertToNodeSpace:loc];
            if (CGPathContainsPoint(path, NULL, loc, NO) ) {
                NSLog(@"inside");
            }
            else {
                NSLog(@"outside");
            }
            break;
        }
    }
}}

-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint loc1 = [touch previousLocationInView:[touch view]];
CGPoint loc2 = [touch locationInView:[touch view]];
loc2 = [[CCDirector sharedDirector]convertToGL:loc2];
endTouch = location;

int goUp = loc2.y - loc1.y;
if(goUp > 0){
CGPoint locationPath  = [selectedSprite convertToNodeSpace:location];
        if (CGPathContainsPoint(path, NULL, locationPath, NO) ) {
            [selectedSprite setPosition:location];
            _spriteTouch = TRUE;
        }
}}

现在,这很好用,但我的问题是精灵往往会以错误的方式移动。它应该跟随向上的触摸滑动,但无论我如何向上滑动,它都会继续向右移动。我在这里做错了什么?请帮我。

更新:我设法弄清楚如何修复我的代码,所以这已经解决了。我已经更新了我的代码,以防其他人遇到同样的问题。

4

2 回答 2

2

你试过改变这个

 loc = [selectedSprite convertToNodeSpace:loc];

到以下

 loc = [selectedSprite convertToWorldSpace:loc];

[编辑:更新示例]
因为调用 convertToGL:基于点的格式被格式化为基于 OpenGL,其中:

坐标

y+           (0,0)                  |y+
|            +------ y+      x-     |      x+
|            |               ---- (0,0) ----
|            |                      |
+ ---- x+    |                      |
(0,0)        x+                     y-
OpenGl       Cocoa Touch         Cartesian

来自屏幕的触摸(Cocoa Touch)不知道 OpenGL 坐标,因此需要使用Cocos2d CCDirector进行转换:

  • (CGPoint) convertToGL:(CGPoint)p
    将 UIKit 坐标转换为 OpenGL 坐标 用于将(多)触摸坐标转换为当前布局(纵向或横向)

反过来也一样

  • (CGPoint) convertToUI:(CGPoint)p
    将 OpenGL 坐标转换为 UIKit 坐标 用于将节点点转换为窗口点,以便调用如 glScissor

有时,我感到困惑并记录(而不是断点)该点确定了哪个坐标有效,因此我可以弄清楚逻辑。

NSLog(@"%@", NSStringFromCGPoint(touchPoint.position));
于 2013-02-19T03:13:54.467 回答
0

在 touchesMoved 方法中,您正在执行以下操作:-

loc2 = [[CCDirector sharedDirector]convertToGL:loc2];

但没有将 loc1 相应地转换为 GL 坐标。我认为你应该试试这个: -

loc1 = [[CCDirector sharedDirector]convertToGL:loc1];

它可能会有所帮助。

于 2013-02-19T04:49:45.153 回答