Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios I';I’我的触摸有问题。我开始更新CGVector值_Ios_Objective C_Ios7_Sprite Kit - Fatal编程技术网

Ios I';I’我的触摸有问题。我开始更新CGVector值

Ios I';I’我的触摸有问题。我开始更新CGVector值,ios,objective-c,ios7,sprite-kit,Ios,Objective C,Ios7,Sprite Kit,我制作了一个CGVector,它使球员的球产生一个向上的冲动(模拟跳跃)。如果我用我现在拥有的(下图)来运行游戏,它会很好地开始,并且一旦游戏开始,跳跃就会完美地发生。每次记录触球时,我都试图让球跳跃,但我在调用正确的方法/使其工作时遇到问题 这是我的代码: #import "MyScene.h" @interface MyScene () @property (nonatomic) SKSpriteNode *paddle; @end int jump = 0; @impleme

我制作了一个
CGVector
,它使球员的球产生一个
向上的冲动
(模拟跳跃)。如果我用我现在拥有的(下图)来运行游戏,它会很好地开始,并且一旦游戏开始,跳跃就会完美地发生。每次记录触球时,我都试图让球跳跃,但我在调用正确的方法/使其工作时遇到问题

这是我的代码:

#import "MyScene.h"

@interface MyScene ()

@property (nonatomic) SKSpriteNode *paddle;


@end

int jump = 0;


@implementation MyScene

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{


NSLog(@"Touch Detected!");

// create the vector
//    myVector = CGVectorMake(0, 15);
//    [ball.physicsBody applyImpulse:myVector];




}

- (void)addBall:(CGSize)size {
SKSpriteNode *ball = [SKSpriteNode spriteNodeWithImageNamed:@"ball"];

// create a new sprite node from an image

// create a CGPoint for position
CGPoint myPoint = CGPointMake(size.width/2,0);
ball.position = myPoint;

// add a physics body
ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:ball.frame.size.width/2];

ball.physicsBody.restitution = 0;

// add the sprite node to the scene
[self addChild:ball];




// create the vector
CGVector myVector = CGVectorMake(0, 15);

// apply the vector
[ball.physicsBody applyImpulse:myVector];


}



-(id)initWithSize:(CGSize)size {    
if (self = [super initWithSize:size]) {
    /* Setup your scene here */
    self.backgroundColor = [SKColor whiteColor];

    // add a physics body to the scene
    self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];

    // change gravity settings of the physics world
    self.physicsWorld.gravity = CGVectorMake(0, -8);

    [self addBall:size];
//        [self addPlayer:size];


}
return self;
}


-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
}

@end

你可以看到我已经注释掉了
myVector=CGVectorMake(0,15)
[ball.physicsBody applyImpulse:myVector]在接触开始,因为它找不到球,我甚至不确定它是否正确。有人能帮我用冲动实现这个跳跃吗?谢谢

你在正确的轨道上,但是向量(0,15)的多个脉冲将导致球飞得很高。在应用任何后续脉冲之前,需要将球的速度重置为零

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

    NSLog(@"Touch Detected!");

    // create the vector
    ball.physicsBody.velocity = CGVectorMake(0,0);

    myVector = CGVectorMake(0, 15);
    [ball.physicsBody applyImpulse:myVector];

}
此外,还需要将ball声明为全局变量,如下所示:

@implementation MyScene
{
    SKSpriteNode *ball;
}
然后在addBall中:方法,而不是

SKSpriteNode *ball = [SKSpriteNode spriteNodeWithImageNamed:@"ball"];
只要写

ball = [SKSpriteNode spriteNodeWithImageNamed:@"ball"];

有没有理由我没有任何回应,但有很多观点?如果我做错了什么,请告诉我:)非常感谢你的回复!我得到一个“使用未声明的标识符“ball”作为所有3行的错误。我是否遗漏了什么?那是因为您没有将ball声明为全局变量。当然,这是一个多么愚蠢的错误!非常感谢您的帮助,它现在工作得很好:)