SpriteKit重力实现

时间:2014-02-12 05:38:18

标签: ios iphone ipad sprite-kit

这就是我的scene.m的样子:

#import "ERGMyScene.h"
#import "ERGBackground.h"
#import "ERGPlayer.h"

@implementation ERGMyScene

-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
    self.currentBackground = [ERGBackground generateNewBackground];
    self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
    [self addChild:self.currentBackground];}


ERGPlayer *player = [[ERGPlayer alloc] init];
player.position = CGPointMake(225, 100);
player.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:10];
[self addChild:player];

return self;
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
SKAction *moveUp = [SKAction moveBy:CGVectorMake(0, 200) duration:0.3];
ERGPlayer *player = (ERGPlayer*) [self childNodeWithName:playerName];
[player runAction:moveUp];
}

-(void)update:(CFTimeInterval)currentTime {
CFTimeInterval timeSinceLast = currentTime - self.lastUpdateTimeInterval;
self.lastUpdateTimeInterval = currentTime;
if (timeSinceLast > 1) { // more than a second since last update
    timeSinceLast = 1.0 / 60.0; }
}
@end

我在ViewController(0,-5)中启用了重力,当点击屏幕时,我的图像执行它想要做的事情,上升200点然后由于重力而下降。第一次点击使角色上升200,但是当图像仍在空中时,每次后续点击都不会使图像上升200,这是我想要它做的。我认为这是因为重力已经作用于图像,导致它不会上升200.当点击使图像上升了整整200时,有没有办法重置图像的重力?

1 个答案:

答案 0 :(得分:0)

使用applyImpulse代替SKAction

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    ERGPlayer *player = (ERGPlayer*) [self childNodeWithName:playerName];
    [player.physicsBody applyImpulse:CGVectorMake(0, ...)]
}