【问题标题】:How can we detect a touch of a sprite?我们如何检测精灵的触摸?
【发布时间】:2010-03-20 06:16:13
【问题描述】:

我的应用中有两个精灵。两者都应该启用触摸,并且两个触摸彼此独立。如果我触摸屏幕(不是在精灵上),它应该有不同的触摸。我的问题是所有三个 sprite1,sprite2,剩余的屏幕应该有独立的触摸。但是我的程序将所有的接触都视为相同。我怎样才能使它们成为我需要的?

谢谢。

【问题讨论】:

  • 您在寻求多点触控方面的帮助吗?或者当它们中的一个被触摸时它们都记录了触摸?
  • 我问的是多点触控。每个人都应该在被触摸时调用自己的动作或方法。

标签: cocoa-touch iphone-sdk-3.0 cocos2d-iphone sprite


【解决方案1】:

为此,首先您需要为您的应用程序启用多点触控:

    [self setMultipleTouchEnabled:YES];

然后要识别触摸,你可以使用类似下面的代码:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
   for ( UITouch* Touch in touches ) 
   { 
      printf( "Touch began %p, tapcount %d\n", (void *) Touch, [Touch tapCount] ); 
      fflush( stdout ); 
   } 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{  
   for ( UITouch* Touch in touches ) 
   { 
      printf( "Touch moved %p, tapcount %d\n", (void*)Touch, [Touch tapCount] ); 
      fflush( stdout ); 
   } 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
   for ( UITouch* Touch in touches ) 
   { 
      printf( "Touch ended %p, tapcount %d\n", (void*)Touch, [Touch tapCount] ); 
      fflush( stdout ); 
   } 
}

因此,使用 (void*)Touch,您可以识别特定的触摸指针,该指针在您真正“结束”该触摸之前不会改变。

例如,如果您触摸屏幕,您将获得一个触摸实例,即使您移动该手指,该实例仍将保持相同的内存地址,直到您松开它。祝你好运,我将这段代码的基础完全用于多点触控精灵管理。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多