我有一些关于动画SKNodes和/或SKSpriteNodes的问题。第一个是如何在视图加载后让它们移动?如何使它们相互碰撞并相互反弹并向另一个方向移动。我正在使用SKScene。还有一种方法可以将SKSriteNodes与SKLabels分组吗?
我有:
#import <SpriteKit/SpriteKit.h>
//constants for the collision bitmap
static const uint32_t classicCategory = 1 << 0;
static const uint32_t arcadeCategory = 1 << 1;
static const uint32_t frenzieCategory = 1 << 2;
@interface HomeScene : SKScene <SKPhysicsContactDelegate> {
BOOL isSoundActionCompleted;
}
@end
这是我声明SKSpriteNodes
的地方 SKSpriteNode *classicMode = [SKSpriteNode spriteNodeWithImageNamed:@"bubble.png"];
classicMode.size = CGSizeMake(130, 130);
classicMode.position = CGPointMake(self.size.width/2, self.size.height/2+50);
classicMode.name = @"classicMode";
[self addChild:classicMode];
SKLabelNode *classicTitle = [SKLabelNode labelNodeWithFontNamed:@"Noteworthy"];
classicTitle.text = @"Classic";
classicTitle.fontSize = 25;
classicTitle.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeCenter;
classicTitle.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
classicTitle.fontColor = [SKColor darkGrayColor];
classicTitle.position = CGPointMake(self.size.width/2, self.size.height/2+50);
classicTitle.name = @"classicTitle";
[self addChild:classicTitle];
另一个
SKSpriteNode *arcadeMode = [SKSpriteNode spriteNodeWithImageNamed:@"bubble.png"];
arcadeMode.size = CGSizeMake(130, 130);
arcadeMode.position = CGPointMake(self.size.width/2+68, self.size.height/2-65);
arcadeMode.name = @"arcadeMode";
[self addChild:arcadeMode];
SKLabelNode *arcadeTitle = [SKLabelNode labelNodeWithFontNamed:@"Noteworthy"];
arcadeTitle.text = @"Arcade";
arcadeTitle.fontSize = 25;
arcadeTitle.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeCenter;
arcadeTitle.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
arcadeTitle.fontColor = [SKColor darkGrayColor];
arcadeTitle.position = CGPointMake(self.size.width/2+68, self.size.height/2-65);
arcadeTitle.name = @"arcadeTitle";
[self addChild:arcadeTitle];
碰撞:
//collision and contact detection
self.physicsBody.categoryBitMask = arcadeCategory;
//physics and collision detection
classicMode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:classicMode.size];
classicMode.physicsBody.dynamic = NO; //no gravity
classicMode.physicsBody.categoryBitMask = arcadeCategory;
arcadeMode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:arcadeMode.size];
arcadeMode.physicsBody.dynamic = NO; //no gravity
arcadeMode.physicsBody.categoryBitMask = classicCategory;
碰撞检测:
//collision detection
classicMode.physicsBody.categoryBitMask = classicCategory;
classicMode.physicsBody.collisionBitMask = arcadeCategory;
classicMode.physicsBody.contactTestBitMask = arcadeCategory;
arcadeMode.physicsBody.categoryBitMask = arcadeCategory;
arcadeMode.physicsBody.collisionBitMask = classicCategory;
arcadeMode.physicsBody.contactTestBitMask = classicCategory;
答案 0 :(得分:0)
1)代码中的第一个问题是两个静态实体从不碰撞使它们变得动态。所以至少要使一个或两个身体充满活力。
2)其次,对于线性运动,使用具有动态体体的body.physicsBody.velocity属性。
3)更改您的位掩码类别 classicMode.physicsBody.categoryBitMask = classicCategory; classicMode.physicsBody.collisionBitMask = arcadeCategory; classicMode.physicsBody.contactTestBitMask = arcadeCategory;
arcadeMode.physicsBody.categoryBitMask = arcadeCategory;
请记住,当两个实体相互接触或碰撞时,contactTestBitMask在spritekit中使用,而collisionBitMask用于将它们重叠或相互传递
我剪切了arcadeMode.physicsBody.collisionBitMask = classicCategory; arcadeMode.physicsBody.contactTestBitMask = classicCategory; 来自你的代码,因为你总是定义它 classicMode.physicsBody.categoryBitMask = classicCategory; classicMode.physicsBody.collisionBitMask = arcadeCategory; classicMode.physicsBody.contactTestBitMask = arcadeCategory;所以不需要重新定义它
当您希望两个类别互相交互时,只使用contactTestBitMask和collisionBitMask
4)处理didbegincontact和didEndContact中的所有碰撞和碰撞逻辑