我正在使用spritekit来创建一个简单的运行游戏。我有一个火柴人和地面,我希望火柴人在落地时与地面相撞。然而,他只是穿过地面而不是框架。有什么想法吗?
//
// GameplayScene.h
// CodeRun
//
// Created by Sebastian Cain on 8/16/14.
// Copyright (c) 2014 ASX Software. All rights reserved.
//
#import <SpriteKit/SpriteKit.h>
@interface GameplayScene : SKScene
typedef NS_OPTIONS(uint32_t , ASXCollisionCategory){
ASXCollisionCategoryGround = 1 << 0,
ASXCollisionCategoryStickman = 1 << 1
};
@end
//
// Ground.m
// CodeRun
//
// Created by Sebastian Cain on 8/16/14.
// Copyright (c) 2014 ASX Software. All rights reserved.
//
#import "Ground.h"
#import "GameplayScene.h"
@implementation Ground
+ (instancetype)ground:(CGPoint)position{
Ground *groundNode = [Ground spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(960, 50)];
groundNode.anchorPoint = CGPointMake(0, 0);
groundNode.position = position;
groundNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(960, 50)];
groundNode.physicsBody.affectedByGravity = YES;
groundNode.physicsBody.dynamic = NO;
groundNode.physicsBody.mass = 1000;
groundNode.physicsBody.categoryBitMask = ASXCollisionCategoryGround;
groundNode.physicsBody.collisionBitMask = ASXCollisionCategoryStickman;
return groundNode;
}
@end
#import "Stickman.h"
#import "GameplayScene.h"
@implementation Stickman
+ (instancetype)stickman: (CGPoint)position{
Stickman *stickmanNode = [Stickman spriteNodeWithImageNamed:@"stick - Frame 1"];
stickmanNode.anchorPoint = CGPointMake(0.5, 0);
stickmanNode.position = position;
stickmanNode.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:25.0 center:CGPointMake(CGRectGetMidX(stickmanNode.frame), CGRectGetMidY(stickmanNode.frame))];
stickmanNode.physicsBody.mass = 50;
stickmanNode.physicsBody.affectedByGravity = YES;
stickmanNode.physicsBody.categoryBitMask = ASXCollisionCategoryStickman;
stickmanNode.physicsBody.collisionBitMask = ASXCollisionCategoryGround;
NSArray *textures = @[
[SKTexture textureWithImageNamed:@"stick - Frame 1"],
[SKTexture textureWithImageNamed:@"stick - Frame 2"],
[SKTexture textureWithImageNamed:@"stick - Frame 3"],
[SKTexture textureWithImageNamed:@"stick - Frame 4"],
[SKTexture textureWithImageNamed:@"stick - Frame 5"],
[SKTexture textureWithImageNamed:@"stick - Frame 6"],
[SKTexture textureWithImageNamed:@"stick - Frame 7"],
[SKTexture textureWithImageNamed:@"stick - Frame 8"],
[SKTexture textureWithImageNamed:@"stick - Frame 9"],
[SKTexture textureWithImageNamed:@"stick - Frame 10"]];
SKAction *stickmanAnimation = [SKAction animateWithTextures:textures timePerFrame:0.05];
SKAction *stickmanRepeat = [SKAction repeatActionForever:stickmanAnimation];
[stickmanNode runAction:stickmanRepeat];
return stickmanNode;
}
@end
//
// GameplayScene.m
// CodeRun
//
// Created by Sebastian Cain on 8/16/14.
// Copyright (c) 2014 ASX Software. All rights reserved.
//
#import "GameplayScene.h"
#import "Stickman.h"
#import "Ground.h"
@implementation GameplayScene
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
/* Setup your scene here */
SKSpriteNode *backgroundNode = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:self.frame.size];
backgroundNode.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
[self addChild:backgroundNode];
Stickman *stickmanInGameplay = [Stickman stickman:CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))];
[self addChild:stickmanInGameplay];
Ground *groundinGameplay = [Ground ground:CGPointMake(0, 0)];
[self addChild:groundinGameplay];
}
return self;
}
@end
答案 0 :(得分:-1)
您是否将Ground
节点添加到(0, 0)
位置(960, 50)
?在这种情况下,玩家将在该物理体内生成!
要实现基础,请删除Ground
节点并将以下内容放入GameplayScene
init方法中:
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
self.physicsBody.categoryBitMask = ASXCollisionCategoryGround;
就是这样。