我遇到了我的英雄角色的问题,当他落在一个移动的平台上,而不是随着它一起移动时,他确实站在同一个X位置,直到平台离开屏幕。我搜索了许多答案但没有用。
这是我的英雄角色和移动平台的方法。
-(void)HeroAdd
{
_Hero = [SKSpriteNode spriteNodeWithImageNamed:@"Hero-1"];
_Hero.name = @"Daniel";
_Hero.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:_Hero.size];
_Hero.physicsBody.categoryBitMask = fPlayerCategory;
_Hero.physicsBody.contactTestBitMask = fPlatformCategory | fEnemyCategory;
_Hero.physicsBody.usesPreciseCollisionDetection = YES;
_Hero.physicsBody.affectedByGravity = YES;
_Hero.physicsBody.dynamic = YES;
_Hero.physicsBody.friction = .9;
_Hero.physicsBody.restitution = 0;
_Hero.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
_Hero.position = CGPointMake(_Hero.position.x - 252, _Hero.position.y + 50);
if (self.size.width == 480) {
_Hero.position = CGPointMake(_Hero.position.x + 44, _Hero.position.y);
}
[self addChild:_Hero];
}
我的移动平台代码
-(void)createPlatform {
SKTexture *objectTexture;
switch (arc4random_uniform(2)) {
case (0):
objectTexture = [SKTexture textureWithImageNamed:@"shortPlatform"];
break;
case (1):
objectTexture = [SKTexture textureWithImageNamed:@"highPlatform"];
default:
break;
}
SKSpriteNode *variaPlatform = [SKSpriteNode spriteNodeWithTexture:objectTexture];
variaPlatform.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
variaPlatform.position = CGPointMake(variaPlatform.position.x + 500, variaPlatform.position.y - 140);
variaPlatform.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:variaPlatform.size];
variaPlatform.physicsBody.usesPreciseCollisionDetection = YES;
variaPlatform.physicsBody.categoryBitMask = fPlatformCategory;
variaPlatform.physicsBody.contactTestBitMask = fPlatformCategory |fPlayerCategory | fEnemyCategory;
variaPlatform.physicsBody.dynamic = NO;
variaPlatform.physicsBody.affectedByGravity = NO;
SKAction *moveLeft = [SKAction moveTo:CGPointMake(180, variaPlatform.position.y) duration:3];
SKAction *moveDown = [SKAction moveTo:CGPointMake(180, -700) duration:4];
SKAction *removeFromParent = [SKAction removeFromParent];
SKAction *AllThree = [SKAction sequence:@[moveLeft, moveDown, removeFromParent]];
[self addChild:variaPlatform];
[variaPlatform runAction:AllThree];
}
任何类型的信息都将得到真正的赞赏。
答案 0 :(得分:5)
当我将传送带添加到游戏中时,我遇到了同样的问题。无论添加多少阻力,Sprite Kit都不会拖动对象。您目前有2个选项。
选项1 - 在平台的每一端添加一个壁架。这是最容易实现的,但不太优雅,如果他落在壁架上,仍然允许玩家滑落。
选项2 -
步骤1:添加使玩家与水平移动平台同步移动的代码。您可以使用self.physicsBody.velocity = CGVectorMake(-50, self.physicsBody.velocity.dy);
之类的内容或使用self.position = CGPointMake(self.position.x+10, self.position.y);
。您将不得不使用x值来将它们同步到平台的速度。
步骤2:每当玩家与平台联系时激活上述代码,并在联系人丢失时停用。
步骤3:如果平台切换方向,设置左右限制,当平台切换方向时通过联系通知您。根据平台的方向,您可以向播放器应用+ x或-x移动值。
我知道这个选项听起来很复杂,但事实并非如此。你只需要一步一步走。
*编辑以提供示例代码*
这是我在水平移动平台背后的逻辑:
<强> PLATFORMS 强> 如果您有多个水平移动平台,则需要将它们存储在GameScene(我的级别)中的数组中。我为他们创建了自己的课程,但你不必这样做。
您必须设置左右限制(带有联系人的不可见SKNode)来为平台设置BOOL属性,该属性告诉玩家类推送哪种方式,因为每个平台的长度可能不同。这就是为什么你需要保持对每个平台(因此数组)的引用。
<强> PLAYER 强> 当玩家跳过平台时,将Player类属性BOOL设置为TRUE,这会根据平台当前移动的方式激活左或右推的常量。另一方面,失去联系会取消推动。
// This code in my "Levels class" which is the default GameScene class.
- (void)didBeginContact:(SKPhysicsContact *)contact
{
uint32_t collision = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask);
if (collision == (CategoryPlatformHorizontal | CategoryPlayer))
{
[_player setPlatformHorizontalContact:true];
for(Platform *platformObject in platformArray)
{
if(([platformObject.name isEqualToString:contact.bodyB.node.name]) || ([platformObject.name isEqualToString:contact.bodyA.node.name]))
{
_player.currentPlatform = platformObject;
}
}
}
}
- (void)didEndContact:(SKPhysicsContact *)contact
{
uint32_t collision = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask);
if (collision == (CategoryPlatformHorizontal | CategoryPlayer))
{
[_player setPlatformHorizontalContact:false];
}
}
// This code is in Player.h
@property (strong) Platform *currentPlatform;
@property BOOL platformHorizontalContact;
// This code is in Player.m
- (void)update:(NSTimeInterval)currentTime
{
if(self.platformHorizontalContact == true)
{
if(self.currentPlatform.movingLeft == true)
{
self.physicsBody.velocity = CGVectorMake(-75, self.physicsBody.velocity.dy); // set your own value depending on your platform speed
} else {
self.physicsBody.velocity = CGVectorMake(75, self.physicsBody.velocity.dy); // set your own value depending on your platform speed
}
}
}