我真的很想弄清楚为什么这段代码会导致对触摸输入的间歇性响应......即使将 NSLog 作为第一条指令......
我刚刚在 cocos2d 中使用 Box2d 制作了一个新项目,该项目在这个阶段只需要一些简单的东西......
出现在屏幕中央的篮子。它必须是一个b2Fixture
并且落在一个表面上。然后,如果用户触摸屏幕,我希望篮子缩放到触摸点,然后用户可以从那里在屏幕上拖动它。
当用户放手时,篮子会掉下来......我现在有这个工作......
然而BUG是触摸屏幕并不总是有效......它间歇性地响应触摸,因此间歇性地调用方法。
正如您将在下面看到的,我曾经NSLog
检查过每个方法何时被调用。结果是有时您必须将手指从屏幕上移开然后再打开几次,然后“看似随意”,它将决定运行代码....
这是我得到的...
我的触摸方法......
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"Multi Touch Moved...");
if (_mouseJoint == NULL) return;
UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:[myTouch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);
_mouseJoint->SetTarget(locationWorld);
}
-(void)ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event
{
NSLog(@"\nThe touch was CANCELED...");
}
-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
NSLog(@"Single Touch Moved...");
}
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:[myTouch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);
NSLog(@"\n\nTouch did begin...");
if (_mouseJoint != NULL)
{
_mouseJoint->SetTarget(locationWorld);
NSLog(@"The IF statment was met...");
return;
}
NSLog(@"The IF statment was NOT met...Running _mouseJoint setup...");
b2MouseJointDef md;
md.bodyA = _groundBody;
md.bodyB = _body;
md.target = _body->GetPosition();
md.collideConnected = true;
md.maxForce = 100000000.0f * _body->GetMass();
_mouseJoint = (b2MouseJoint *)_world->CreateJoint(&md);
_body->SetAwake(true);
_mouseJoint->SetTarget(locationWorld);
}
-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (_mouseJoint != nil) {
_world->DestroyJoint(_mouseJoint);
_mouseJoint = nil;
}
}
这是一些代码引用的接口......
@interface HelloWorldLayer : CCLayerColor
{
b2World *_world;
b2Body *_body;
b2Fixture *_bodyFix;
b2MouseJoint *_mouseJoint;
b2Body *_groundBody;
}
另一个成员帮助我确定的唯一想法是,因为我在一个场景中工作,并且我在屏幕上有/有一个 CCMenu 和一个 CCLabelTTF,CCMenu 是否可能仍在拦截触摸,如果是这样,动画完成后如何销毁 CCMenu?
按下唯一的按钮项只需 CC 将标题和标签(CCMenuItem)垂直移出屏幕......但该对象仍然存在......