我正在使用以下代码让球落下,每当我点击它时它应该跳回来,因此我的touchesBegan方法中的应用冲动。然而,当我运行这个代码时,球开始下降但是当我点击它时,它会继续下降。我做错了什么?
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
[ball.physicsBody applyImpulse:CGVectorMake(0, 8)];
}
-(void) addBall
{
SKShapeNode *ball;
ball = [SKShapeNode shapeNodeWithCircleOfRadius:30];
ball.strokeColor = [SKColor colorWithRed:114.0f/255.0f green:84.0f/255.0f blue:123.0f/255.0f alpha:1.0];
ball.fillColor = [SKColor colorWithRed:114.0f/255.0f green:84.0f/255.0f blue:123.0f/255.0f alpha:1.0];
ball.position = CGPointMake(self.size.width/2,self.size.height/2);
//add physics body
ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:30];
[self addChild:ball];
}
-(void)didMoveToView: (SKView *)view
{
self.backgroundColor = [UIColor colorWithRed:113.0f/255.0f green:255.0f/255.0f blue:248.0f/255.0f alpha:1.0];
//add phsyics body to the scene
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
//change gravity
self.physicsWorld.gravity = CGVectorMake(0, -6);
[self addBall];
}
-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
}
答案 0 :(得分:1)
你的冲动值非常低。增加它或减少重力,试试这个代码是有效的。
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
for (SKSpriteNode *node in self.children) {
if ([node.name isEqualToString:@"ball"])
[node.physicsBody applyImpulse:
CGVectorMake(0, 100)];
NSLog(@"tapped ball");
}
}
-(void)didMoveToView: (SKView *)view
{
self.backgroundColor = [UIColor colorWithRed:113.0f/255.0f green:255.0f/255.0f blue:248.0f/255.0f alpha:1.0];
//add phsyics body to the scene
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
//change gravity
self.physicsWorld.gravity = CGVectorMake(0, -6);
[self addBall];
}
-(void) addBall {
ball = [SKShapeNode shapeNodeWithCircleOfRadius:30];
ball.fillColor = [SKColor colorWithRed:225.0f/255.0f green:76.0f/255.0f blue:76.0f/255.0f alpha:1.0];
ball.strokeColor = [SKColor colorWithRed:225.0f/255.0f green:76.0f/255.0f blue:76.0f/255.0f alpha:1.0];
ball.position = CGPointMake(self.size.height/2, self.size.width/2);
ball.name = @"ball";
ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:30];
[self addChild:ball];
}
答案 1 :(得分:0)
以下是用于应用冲动的代码。
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
// if ball is tapped apply impulse
if ([node.name isEqualToString:@"ball"]) {
[node.physicsBody applyImpulse:
CGVectorMake(0, 80)];
NSLog(@"ball pressed");
}
}
-(void) addBall
{
SKShapeNode *ball;
ball = [SKShapeNode shapeNodeWithCircleOfRadius:30];
ball.strokeColor = [SKColor colorWithRed:114.0f/255.0f green:84.0f/255.0f blue:123.0f/255.0f alpha:1.0];
ball.fillColor = [SKColor colorWithRed:114.0f/255.0f green:84.0f/255.0f blue:123.0f/255.0f alpha:1.0];
ball.position = CGPointMake(self.size.width/2,self.size.height/2);
ball.name = @"ball";
//add physics body
ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:30];
[self addChild:ball];
}
答案 2 :(得分:-1)
您在ball
中创建并添加物理主体的addBall
与您在touchesBegan
中施加冲动的行为不同。在addBall
中,它是一个局部变量。也许你的意思是把它变成一个属性?然后在两个地方将其称为self.ball
。