我有SKSpriteNode因self.physicsWorld.gravity = CGVectorMake(0.0, -4.0);
而被迫倒地。当用户点击显示并按住它时,我希望SKSpriteNode向上飞,在用户停止触摸后,它再次落到地面。
我试图改变速度,但它只会产生小的弹跳,就像applyImpulse方法......:
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
flyingObject.physicsBody.velocity = CGVectorMake(0, 100)
}
}
编辑:
这是我的场景初始化代码
override func didMoveToView(view: SKView) {
/* Setup your scene here */
// Physics
self.physicsWorld.gravity = CGVectorMake(0.0, -4.0);
// flyingObject
var flyingObjectTexture = SKTexture(imageNamed:"flyingObject")
flyingObject.filteringMode = SKTextureFilteringMode.Nearest
flyingObject = SKSpriteNode(texture: flyingObjectTexture)
flyingObject.setScale(1)
flyingObject.position = CGPoint(x: self.frame.size.width * 0.35, y: self.frame.size.height * 0.6)
flyingObject.physicsBody = SKPhysicsBody(circleOfRadius:flyingObject.size.height/2.0)
flyingObject.physicsBody.dynamic = true
flyingObject.physicsBody.allowsRotation = false
self.addChild(flyingObject)
// Ground
var groundTexture = SKTexture(imageNamed:"Ground")
var sprite = SKSpriteNode(texture:groundTexture)
sprite.setScale(0.5)
sprite.position = CGPointMake(self.size.width/2 - 100, sprite.size.height)
self.addChild(sprite)
var ground = SKNode()
ground.position = CGPointMake(0, groundTexture.size().height / 2)
ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.width, groundTexture.size().height * 0.5))
ground.physicsBody.dynamic = false
self.addChild(ground)
}
答案 0 :(得分:2)
感谢Code Monkey,我能够提出解决方案,比我想象的更容易:
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
isTouching = true
}
override func touchesEnded(touches: NSSet!, withEvent event: UIEvent!) {
/* Called when a touch begins */
isTouching = false
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
if isTouching {
flyingObject.physicsBody.applyImpulse(CGVectorMake(0, 5))
}
}
答案 1 :(得分:1)
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch * touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
flyingObject.physicsBody.velocity = CGVectorMake(0, 100)
}
接触结束:
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
flyingObject.physicsBody.velocity = CGVectorMake(0, 0)
}