gpt4 book ai didi

ios - 为什么 SKSpriteNode 不响应 applyForce 或 applyImpulse?

转载 作者:可可西里 更新时间:2023-11-01 05:34:08 24 4
gpt4 key购买 nike

我有一个 sprite 节点,它响应于施加在其连接的 physicsBody 上的重力而出现和下落。

很好。

现在,我想使用 applyForce 在它下落时向某个方向稍微插入它。

不幸的是,无论我将 physicsBody.massphysicsWorld.gravity 做得多小,或者我将 applyForce 向量做得多大 -- applyForce 向量似乎被忽略了。

当我注释掉 self.physicsWorld.gravity 行时,我看到 applyForce 正在工作正如预期的那样

如何在施加重力的同时使用 applyForce 实现“插入”物理体?


这是我的.h

// GameScene.h


#import <SpriteKit/SpriteKit.h>

@interface GameScene : SKScene

@end

还有.m

// GameScene.m

#import "GameScene.h"

@interface GameScene ()
@property NSMutableArray *bubblesToPop;

@end

@implementation GameScene

-(void)didMoveToView:(SKView *)view {
/* Setup your scene here */

self.size = view.bounds.size;
self.physicsWorld.gravity = CGVectorMake(0, 1.0);

SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"bubble"];

sprite.yScale = 0.5;
sprite.xScale = 0.5;
sprite.name = @"bubble";
sprite.position = CGPointMake(self.frame.size.width / 2.0, self.frame.size.height / 2.0);

SKPhysicsBody *physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:sprite.frame.size.width];
physicsBody.mass = 0.02;
[physicsBody applyForce:CGVectorMake(900, 900)];
sprite.physicsBody = physicsBody;

[self addChild:sprite];

}

@end

最佳答案

在 Sprite Kit 可以确定力如何影响它之前, body 需要在场景中。对物理体的 applyForce 调用需要在您 addChild 到场景后发生。

来自 Apple 的文档:

The physics simulation in Sprite Kit is performed by adding physics bodies to scenes. A physics body is a simulated physical object connected to a node in the scene’s node tree. It uses the node’s position and orientation to place itself in the simulation...Each time a scene computes a new frame of animation, it simulates the effects of forces and collisions on physics bodies connected to the node tree. It computes a final position, orientation, and velocity for each physics body. Then, the scene updates the position and rotation of each corresponding node [emphasis added].

此外,外部因素也会影响物理体如何受力影响。例如,在计算应用于物理体的合力时,必须考虑重力、碰撞和力场 (iOS 8)。因此,在 Sprite Kit 可以确定力如何影响它之前, body 需要在场景中。

关于ios - 为什么 SKSpriteNode 不响应 applyForce 或 applyImpulse?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26570233/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com