gpt4 book ai didi

ios - spriteKit 中的常规碰撞错误

转载 作者:行者123 更新时间:2023-11-29 02:55:06 25 4
gpt4 key购买 nike

我正在制作一个非常简单的跳跃游戏。当我的“英雄”与地面发生碰撞时,它会调用 didBeginContact 并进行跳跃。非常基本,但效果不佳,因为一段时间后(有时更快,有时更晚......)它停止工作,如您所见。我想知道我是否做错了什么,或者是否有更好的方法,或者 spriteKit 中是否存在我从未听说过的已知错误......图片是gif。如果看不到运动,请在新标签页中打开它 jumping secuence

@interface XYZWorld1()
@property (nonatomic) SKSpriteNode *hero;
@property (nonatomic) SKSpriteNode *groundPhysics;
@end


@implementation XYZWorld1
static const uint32_t ground = 0x2 << 2;
static const uint32_t bee = 0x3 << 3;

创建地面的方法:

    -(SKSpriteNode*)createGroundxPos:(float)x yPos:(float)y width:(CGFloat)width height:(CGFloat)height
{
SKSpriteNode *ground = [SKSpriteNode spriteNodeWithColor:[UIColor colorWithHue:1 saturation:1 brightness:1 alpha:1] size:CGSizeMake(width, height)];
SKPhysicsBody *groundFisica =[SKPhysicsBody bodyWithRectangleOfSize:ground.size];
[ground setName:@"ground"];
[groundFisica setDynamic:NO];
[groundFisica setAffectedByGravity:NO];
[groundFisica setAllowsRotation:NO];
[groundFisica setRestitution:0];
[groundFisica setUsesPreciseCollisionDetection:YES];
[groundFisica setCategoryBitMask:ground];
[groundFisica setContactTestBitMask:bee];
[groundFisica setCollisionBitMask:bee];
ground.physicsBody = groundFisica;
[ground setPosition:CGPointMake(x, y)];
return ground;

}

创建英雄的方法

-(SKSpriteNode*)crearHero
{
SKTexture *temp = _heroWalkingFrames[0];
SKSpriteNode *hero = [SKSpriteNode spriteNodeWithTexture:temp];

hero.name = @"hero";
hero.zPosition = 1;
if(IS_IPHONE_5){
[hero setPosition:CGPointMake(100, 280)];
}
else {
[hero setPosition:CGPointMake(100, 220)];
}

[hero setSize:CGSizeMake(25, 25)];
hero.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(23, 24)];

[hero.physicsBody setDensity:0.95];
[hero.physicsBody setDynamic:YES];
[hero.physicsBody setAffectedByGravity:YES];
[hero.physicsBody setAllowsRotation:NO];
[hero.physicsBody setUsesPreciseCollisionDetection:YES];
[hero.physicsBody setRestitution:0];
[hero.physicsBody setVelocity:CGVectorMake(0, 0)];
return hero;
}

游戏开始前预加载所有东西的方法:

-(void)preloadAssets
{
//hero
_heroWalkingFrames = [self cargarArrayAtlas:@"astroJump.atlas" imageName:@"astronautaJump_000"];
_hero = [self crearHero];

_hero.physicsBody.categoryBitMask = bee;
_hero.physicsBody.collisionBitMask = bee | cubico | ground | bicho ;
_hero.physicsBody.contactTestBitMask = bee | cubico | ground | bicho ;


//Ground
_groundPhysics = [self createGroundxPos:100 yPos:142 width:42 height:23.25];
}

两个物体第一次接触时调用的方法:

-(void) didBeginContact:(SKPhysicsContact *)contact 
{
//if hero collides with ground

if((contact.bodyA.categoryBitMask == bee && contact.bodyB.categoryBitMask == ground) ||
(contact.bodyA.categoryBitMask == ground && contact.bodyB.categoryBitMask == bee) )
{

if(_jumping == NO && _groundPhysics.position.y+_groundPhysics.size.height/2<=_hero.position.y-11.5)
{

[self jumping];
}
}
}

跳转调用的方法:

-(void)jumping{
//Play sound file
[self runAction:self.jumpSound withKey:@"jumpSound"];


if(IS_IPHONE_5){
_diferencia=(164 * 9)/_hero.position.y;

}else{
_diferencia=(164 * 7)/_hero.position.y;
}


[_hero.physicsBody applyImpulse:CGVectorMake(0,_diferencia)];
_jumping = YES;
[_hero removeActionForKey:@"astroJump"];
[self astroJump];
}

-(void)astroJump
{
//runAction animation method
[_hero runAction:[SKAction animateWithTextures:_heroWalkingFrames
timePerFrame:0.05f
resize:NO
restore:NO] withKey:@"astroJump"];
return;
}

if 语句中的 bool 值(self.jumping),在 updateinitGameContent 中:

-(void)update:(CFTimeInterval)currentTime
{
if(_hero.physicsBody.velocity.dy < 0 )_jumping = NO;
}

-(void)initGameContent
{

_jumping = NO;

最佳答案

当检测到与地面接触时,您可能需要确保将玩家位置重置为高于地面。否则,当调用 [self Jump]; 时,玩家仍可能与地面接触,这有时会导致玩家被“卡住”。在进行碰撞检测时,我在我的游戏中看到过类似的问题。下面的代码显示了您可以采取哪些措施来解决此问题。

if (_jumping == NO && _groundPhysics.position.y+_groundPhysics.size.height/2<=_hero.position.y-11.5)
{
_hero.position.y = _groundPhysics.position.y + _groundPhysics.size.height/2 + 1;
[self jumping];
}

关于ios - spriteKit 中的常规碰撞错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23998112/

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