严重的多点触控问题

时间:2011-06-27 12:53:52

标签: cocos2d-iphone multi-touch

我几乎完成了我的游戏,但我无法让我的多点触控工作:

我进入了我的appdelegate

[glView setMultipleTouchEnabled:YES];
(also tried [window setMultiTouchEnabled:YES];)

在我的Bomb.h中

@interface Bomb : CCLayer (I also tried CCNode with <ccTargetTouchDelegate>)

Bomb.m

-(void)onEnter
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:kCCMenuTouchPriority swallowsTouches:YES];
[super onEnter];
}

-(void)onExit
{
[[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
[super onExit];
}

(Also tried registerWithTouchDispatcher withouth onEnter and onExit and also tried with [super registerWithTouchDispatcher])

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
if(isPaused || !canBeDragged || LOST)
return NO;

//take the coordinates of the touch and transform them into the OpenGL system.
CGPoint location = [touch locationInView: [touch view]];
location = [[CCDirector sharedDirector] convertToGL: location];

CGRect frame = CGRectMake(self.position.x, self.position.y, self.contentSize.width, self.contentSize.height);

if(!CGRectContainsPoint(frame, location))
return NO;

location.x *= factor;
location.y *= factor;

if(BombShape)
{
offsetX = BombShape->body->p.x – location.x;
offsetY = BombShape->body->p.y – location.y;

BombShape->body->v = cpvzero;
BombShape->collision_type++; //make it ‘Dragged’. For example RedNinja++ is DraggedRedNinja
}
return YES;
}
//****************************************************************************************
- (void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
if(isPaused || !canBeDragged)
return;

CGPoint location = [touch locationInView: [touch view]];
location = [[CCDirector sharedDirector] convertToGL: location];

CGPoint prevLocation = [touch previousLocationInView: [touch view]];
prevLocation = [[CCDirector sharedDirector] convertToGL: prevLocation];

location.x *= factor;
location.y *= factor;

prevLocation.x *= factor;
prevLocation.y *= factor;

if(BombBody)
{
//during the dragging, the bomb must not have velocity. Otherwise, it will “run” beneath your finger.
//so we are constantly calculating the velocity and ,when you end the draging, assign that value to the velocity of the bomb.
velocity = cpv((location.x – prevLocation.x) * 30 , (location.y – prevLocation.y)*30);

CGPoint newPosition = cpv(location.x + offsetX, location.y + offsetY);
//test per X

canMoveOnX = CheckOnX(BombBody->p , newPosition,radius);
canMoveOnY = CheckOnY(BombBody->p , newPosition,radius);

if(canMoveOnX)
BombBody->p.x = newPosition.x;

if(canMoveOnY)
BombBody->p.y = newPosition.y;
}
}

//****************************************************************************************
- (void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
if(!canBeDragged || isPaused)
return;

//set velocity back
BombShape->body->v = velocity;
BombShape->collision_type–;

offsetX = 0;
offsetY = 0;
}
//****************************************************************************************
-(void) ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event
{

}

不知怎的,我似乎无法发现我的第二次触摸:S。

我在一个没有接触的CCLayer的GameScene中加载我的Bomb.h。

请帮忙。感谢。

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。

如果您有其他视图,例如广告视图,您应该对此做同样的事情。

离。

[yourAnotherView setMultipleTouchEnabled:YES];

如果你不这样做,抱歉我再也不能回答了......