只有当英雄高于平台时跳跃 - Spritekit游戏

时间:2014-08-29 07:37:37

标签: xcode sprite-kit 2d-games

大家好对于通行证两天,我试图解决我没有成功的问题..

当英雄跳上平台并上升时,我想制作游戏。它是Doodle Jump游戏类型。

这是代码

我制作世界节点

SKNode *world;

然后是英雄

SKSpriteNode *hero;
// setting the physicsBody
// add categoryBitMask = heroCat;
// add contactTestBitMask = platformCat;
// add collisionBitMask = 0;

平台

SKSpriteNode *platform;
// setting the physicsBody
// add categoryBitMask = platformCat;
// add collisionBitMask = 0;

在didBeginContact

// Only bounce the hero if he's falling
if (hero.physicsBody.velocity.dy < 0){
     hero.physicsBody.velocity = CGVectorMake(hero.physicsBody.velocity.dx, 500.0f);
}

在didSimulatePhysics中

// if the game is started and the hero position is > 0 and hero position > the new max hero y
if(self.isStarted && hero.position.y > 0 && hero.position.y > _maxHeroY){
    CGPoint positionInScene = [self convertPoint:hero.position fromNode:hero.parent];
    world.position = CGPointMake(world.position.x, world.position.y -positionInScene.y);
}   

到目前为止一直很好,但我注意到有时我可以跳到:             在didSimulatePhysics中,我还在随机Y上创建了新平台,其中包含来自preveos平台的固定x值             当英雄上升世界位置变化             英雄正在堕落             他不在平台之上但是能够接触到他的身体,这是错误的,因为只有当脚在平台上方时才必须跳起来

所以我改变了didBeginContact

// hero position in the world 
CGPoint positionInScene = [self convertPoint:hero.position fromNode:node.parent];

// platform position in the world
CGPoint positionInSceneP = [self convertPoint:node.position fromNode:node.parent];

// get the positon of the bottom edge of the hero (the feet)
int heroBottomEdge = positionInScene - hero.frame.size.height/2;

// get the positon of the top edge of the platfrom
int platformTopEdge = positionInScene - node.frame.size.height/2;


// Only bounce the hero if he's falling and the bottom edge of the hero node(the feet) is above the platform top edge or or equal to jump
if (hero.physicsBody.velocity.dy < 0 && heroBottomEdge >= platformTopEdge){
     hero.physicsBody.velocity = CGVectorMake(hero.physicsBody.velocity.dx, 500.0f);
}

问题在于,有时platformTopEdge更大,而且heroBottomEdge和其他英雄通过平台而不会跳跃                 我也尝试使用hero.position.y和node.position.y,但效果是相同的

0 个答案:

没有答案