У меня есть стрела и шар. Якорная точка для стрелки находится внизу. Все, что мне нужно сделать, это сделать так, чтобы спрайт мог вращаться касанием, затем к мячу прикладывают линейный импульс и стреляют в направлении, указанном стрелкой.
Е.Г.
Скажем, вы поворачиваете стрелку на 70 градусов, затем нажимаете кнопку огня. Затем мяч отбрасывается под углом 70 градусов.
В настоящее время это то, что я использую, но мяч не стреляет в направлении, указанном стрелкой.
Вращает стрелку касанием.
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
//acquire the previous touch location
CGPoint firstLocation = [touch previousLocationInView:[touch view]];
CGPoint location = [touch locationInView:[touch view]];
//preform all the same basic rig on both the current touch and previous touch
CGPoint touchingPoint = [[CCDirector sharedDirector] convertToGL:location];
CGPoint firstTouchingPoint = [[CCDirector sharedDirector] convertToGL:firstLocation];
CGPoint firstVector = ccpSub(firstTouchingPoint, _arrow.position);
CGFloat firstRotateAngle = -ccpToAngle(firstVector);
CGFloat previousTouch = CC_RADIANS_TO_DEGREES(firstRotateAngle);
CGPoint vector = ccpSub(touchingPoint, _arrow.position);
CGFloat rotateAngle = -ccpToAngle(vector);
CGFloat currentTouch = CC_RADIANS_TO_DEGREES(rotateAngle);
//keep adding the difference of the two angles to the dial rotation
arrowRotation += currentTouch - previousTouch;
}
Кнопка огня, чтобы стрелять в направлении, указанном стрелкой.
-(void) fireBall: (id)sender
{
[self unscheduleUpdate];
direction = arrowRotation;
b2Vec2 force = b2Vec2(direction, 24.8);
_ballBody->ApplyLinearImpulse(force, _ballBody->GetWorldCenter());
}
Любая помощь очень ценится!
Заранее спасибо!