问题描述
我正在使用Box2d和Cocos2d开发iPhone游戏。我在屏幕中央有一个球,它具有使用固定装置等创建的简单边界。我想要做的是添加功能,以便用户可以滑动球以施加力并基本上在屏幕上轻拂它。 / p>
我正在使用以下代码来实现此目的:
b2Vec2 force = b2Vec2(30,30);
_body-> ApplyLinearImpulse(force,_ballBodyDef-> position);
如果我在init函数中施加线性脉冲,则球会朝正确的方向发射并表现为它应该做。但是,如果我将其放入 handlePan
函数(当用户完成手势时调用该函数),它什么也不会做。以下是该功能的完整代码:(请注意, NSLog
会写出正确的信息。
-(void)handlePanFrom:(UIPanGestureRecognizer *)recognizer {
if(recognizer.state == UIGestureRecognizerStateEnded){
CGPoint velocity = [recognizer velocityInView:recognizer.view];
NSLog(@ Vel:%f,newPos:%f,velocity.x,velocity.y);
b2Vec2 force = b2Vec2(30,30);
_body-> ApplyLinearImpulse(force,_ballBodyDef-> position);
}
}
您在handlePanFrom中施加的力始终为b2Vec2(30,30)。它永远不会随着您当前的代码而改变,因此不会移动您的方向。
I am using Box2d and Cocos2d to develop an iPhone game. I have a ball in the center of the screen with a simple boundary created using fixtures etc. What I want to do is add the functionality so that a user can "swipe" the ball to apply force and basically flick it across the screen.
I am using the following code to achieve this:
b2Vec2 force = b2Vec2(30, 30);
_body->ApplyLinearImpulse(force, _ballBodyDef->position);
If I apply a linear impulse during the init function the ball fires off in the correct direction and behaves as it should do. It doesnt do anything, however, if I put it into the handlePan
function that is called when a gesture is done by the user. Here is the full code for the function: (Note that the NSLog
writes out the correct information.
- (void)handlePanFrom:(UIPanGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateEnded) {
CGPoint velocity = [recognizer velocityInView:recognizer.view];
NSLog(@"Vel: %f, newPos: %f",velocity.x,velocity.y);
b2Vec2 force = b2Vec2(30, 30);
_body->ApplyLinearImpulse(force, _ballBodyDef->position);
}
}
The force you are applying in handlePanFrom is always b2Vec2(30, 30). It will never change with your current code, and is therefore not going to move in your direction.
这篇关于在UIPanGestureRecognizer函数中调用ApplyLinearImpulse时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!