有人可以帮助我。我正在尝试使用CCSprite作为移动控制器,我可以拖动(左/右)来移动我的玩家角色。问题是当我在CCSprite之外触摸时它仍然检测为Active,这是我在ccTouchBegan上设置ACTIVE的枚举,以及ccTouchEnded方法中的IDLE。默认情况下,它处于IDLE状态。当我在CCSprite之外开始触摸时,不确定为什么它会激活。
请告知。
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
BOOL touched = NO;
CGPoint loc = [touch locationInView: touch.view];
loc = [[CCDirector sharedDirector] convertToGL: loc];
//avatar is a CCSprite...
//inside of IF statement should not execute if I touch anywhere on screen
//outside of self.avatar sprite, but it still executes....
if ( CGRectContainsPoint( [self.avatar boundingBox], loc) );
{
self.state = ACTIVE;
self.action = MOVE_STOPPED; //player decides which direction to move, based on action state
self.currPoint = self.prevPoint = self.avatar.position;
touched = YES;
}
return touched;
}
答案 0 :(得分:0)
不是100%,但尝试在转换为GL后添加此内容:
loc = [self.avatar convertToNodeSpace:loc];
答案 1 :(得分:0)
如果它帮助了我能够解决问题的任何人。我没有子类化CCLayer,而是继承了CCNode。在我的ccTouchBegan方法中,我做了一个显式绑定检查,以查看touchToNodeSpace中touch的位置是否在sprite的boundingBox内。我从SneakyInput按钮类借用了显式绑定检查。
这是ccTouchBegan方法。如果有人对如何在CCLayer的子类中有任何想法,请告诉我,所以我不必实现自己的CCTouchOneByOneDelegate。
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
BOOL touched = NO;
CGPoint loc = [touch locationInView: touch.view];
loc = [[CCDirector sharedDirector] convertToGL: loc];
loc = [self.avatar convertToNodeSpace: loc]; //YvesLeBorg's feedback
float width = [self.avatar boundingBox].size.width;
float height = [self.avatar boundingBox].size.height;
//if ( CGRectContainsPoint( [self.avatar boundingBox], loc) );
if ( loc.x < -width || loc.x > width || loc.y < -height || loc.y > height ) return touched;
else
{
self.state = ACTIVE;
self.action = MOVE_STOPPED;
self.currPoint = self.prevPoint = self.avatar.position;
touched = YES;
}
// CCLabelTTF *lbl = (CCLabelTTF *)[[[[CCDirector sharedDirector] runningScene] getChildByTag: -10] getChildByTag: 100];
// NSString *str = [NSString stringWithFormat: @"Touch Pos: (%.2f, %.2f)", [self.avatar boundingBox].size.width, [self.avatar boundingBox].size.height ];
// [lbl setString: str];
return touched;
}