gpt4 book ai didi

ios - 无法阻止 Sprite 移出屏幕

转载 作者:行者123 更新时间:2023-11-28 21:42:33 24 4
gpt4 key购买 nike

我想让我的 Sprite 在屏幕范围内移动。因此,我通过 bodyWithEdgeLoopF​​romRect 创建了一个边缘循环,同时设置了碰撞位掩码以使它们相互碰撞。

static const uint32_t kRocketCategory = 0x1 << 0;
static const uint32_t kEdgeCategory = 0x1 << 6;

我使用平移识别器来移动 Sprite ,这是设置所有事物属性的当前代码。

- (void)didMoveToView:(SKView *)view
{
// Pan gesture
self.panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanFrom:)];
[self.view addGestureRecognizer:self.panRecognizer];

// Edge
self.backgroundColor = [SKColor blackColor];
self.physicsWorld.gravity = CGVectorMake(0.0f, 0.0f);
self.physicsWorld.contactDelegate = self;
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
self.physicsBody.categoryBitMask = kEdgeCategory;
self.physicsBody.contactTestBitMask = kRocketCategory;
self.physicsBody.collisionBitMask = kRocketCategory;
self.physicsBody.usesPreciseCollisionDetection = YES;
self.physicsBody.restitution = 0;

// Rocket
SKTexture *texture = [SKTexture textureWithImageNamed:@"Rocketship-v2-1"];
texture.filteringMode = SKTextureFilteringNearest;
self.rocketSprite = [SKSpriteNode spriteNodeWithTexture:texture];

self.rocketSprite.position = CGPointMake(CGRectGetMidX(self.scene.frame), CGRectGetMaxY(self.scene.frame)*0.3); // 30% Y-axis
self.rocketSprite.name = @"rocketNode";
self.rocketSprite.xScale = 2;
self.rocketSprite.yScale = 2;

self.rocketSprite.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.rocketSprite.size];
self.rocketSprite.physicsBody.categoryBitMask = kRocketCategory;
self.rocketSprite.physicsBody.contactTestBitMask = kEdgeCategory;
self.rocketSprite.physicsBody.collisionBitMask = kEdgeCategory;
self.rocketSprite.physicsBody.usesPreciseCollisionDetection = YES;
self.rocketSprite.physicsBody.allowsRotation = NO;
self.rocketSprite.physicsBody.restitution = 0;

[self addChild:self.rocketSprite];
}

Pan 识别代码:

- (void)handlePanFrom:(UIPanGestureRecognizer *)recognizer
{
if (recognizer.state == UIGestureRecognizerStateBegan) {
self.selectedRocket = self.rocketSprite;
} else if (recognizer.state == UIGestureRecognizerStateChanged) {
CGPoint translation = [recognizer translationInView:recognizer.view];
translation = CGPointMake(translation.x, -translation.y);

[self panForTranslation:translation];
[recognizer setTranslation:CGPointZero inView:recognizer.view];
} else if (recognizer.state == UIGestureRecognizerStateEnded) {
}
}

- (void)panForTranslation:(CGPoint)translation
{
CGPoint position = self.selectedRocket.position;
if ([self.selectedRocket.name isEqualToString:@"rocketNode"]) {
self.selectedRocket.position = CGPointMake(position.x + translation.x, position.y);
}
}

现在的问题是,当我缓慢移动 Sprite (火箭)时,边缘会阻止 Sprite 离开屏幕。但是,当我快速移动 Sprite 时, Sprite 会冲出范围。请看下面的动画。我已经阅读了类似问题的一些解决方案,但我仍然不知道我的代码有什么问题。对于这里的情况,边循环和碰撞位掩码是否不够?

enter image description here

最佳答案

您的尝试与我在网上搜索时发现的类似,阅读您尝试过的内容后,您似乎已经涵盖了大多数更常见的问题(例如,不使用 SKAction)。

我目前不使用接触位掩码来处理场景一侧的碰撞,但我确实使用了一个主动检查位置的循环。

这是我用来重新定位对象的方法:(重新定位所有东西,你可以指定它只移动火箭。

-(void)repositionObjects
{
for (CXSpriteNode *i in self.sceneObjects) // CXSpriteNode is a certain subclass
{
CGPoint position = i.position;
if (position.x > self.background.size.width || position.x < 0)
{
CGPoint newPosition;
if (position.x > self.background.size.width)
{
newPosition = CGPointMake(self.background.size.width-1, position.y);
} else {
newPosition = CGPointMake(1, position.y);
}
[i runAction:[SKAction moveTo:newPosition duration:0.0f]];
}

if (position.y > self.background.size.height || position.y < 0)
{
CGPoint newPosition;
if (position.y > self.background.size.height)
{
newPosition = CGPointMake(self.background.size.height-1, 1);
} else {
newPosition = CGPointMake(position.x, 1);
}
[i runAction:[SKAction moveTo:newPosition duration:0.0f]];
}
}
}

这从 SKScene 循环中调用:

-(void)update:(NSTimeInterval)currentTime
{
[self repositionObjects];
}

现在,它显然没有您想要的结果那么优雅,我觉得它可能仍然会引起闪烁,但无论如何我都会试一试。

此外,一旦手势暂时超出范围以停止可能导致闪烁的重复滑动,可能值得尝试禁用/取消手势。

希望这对您有所帮助。

关于ios - 无法阻止 Sprite 移出屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31491900/

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