我尝试制作像游戏一样的玩家是玩家和另一边的电脑,因为我希望将来添加许多游戏场景与不同的游戏场我分别创建球节点,桨节点等文件,使其成为更容易在游戏场中插入,我不会从头开始创建每个场景 问题现在,当我尝试所有放在一起的玩家桨工作,球节点工作,但计算机桨显示第二,然后消失。这是我得到的: 的 GameScene.m
@implementation GameScene
-(void)didMoveToView:(SKView *)view {
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
self.physicsBody.categoryBitMask = gamefieldCategory;
self.physicsBody.contactTestBitMask = emptyCategory;
self.physicsWorld.contactDelegate = self;
BallNode *ball = [[BallNode alloc] init];
[self addChild:ball];
ball = (BallNode *) [self childNodeWithName:@"ball"];
[ball resetPosition];
PlayerNode *leftPlayer = [[PlayerNode alloc] initOnLeftSide];
[self addChild:leftPlayer];
leftPlayer = (PlayerNode *) [self childNodeWithName:@"leftPlayer"];
[leftPlayer positionOnLeftSide];
ComputerNode *rightPlayer = [[ComputerNode alloc] initOnRightSide];
[self addChild:rightPlayer];
rightPlayer = (ComputerNode *) [self childNodeWithName:@"rightPlayer"];
[rightPlayer positionOnRightSide];
}
-(void)computer{
BallNode *ball = (BallNode *) [self childNodeWithName:@"ball"];
ComputerNode *rightPlayer = (ComputerNode *) [self childNodeWithName:@"rightPlayer"];
if (ball.position.x > CGRectGetMidX(self.frame)) {
if (rightPlayer.position.y > ball.position.y) {
rightPlayer.position = CGPointMake(self.frame.origin.x - 50 + self.frame.size.width, rightPlayer.position.y -1.5f);
//there is a very long text, thats why i cut it off
}
}
- (void)update:(CFTimeInterval)currentTime{
[self computer]; // calling computer paddle movement.
}
ComputerPaddleNode.m
@implementation ComputerPaddleNode
- (id)init
{
return [self initWithName:@"paddle"];
}
- (id)initWithName:(NSString *)name
{
self = [super init];
if (self) {
self = [ComputerPaddleNode spriteNodeWithImageNamed: @"board.png"];
self.userInteractionEnabled = YES;
self.name = name;
self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.frame.size];
self.physicsBody.dynamic = NO;
self.physicsBody.usesPreciseCollisionDetection = YES;
self.physicsBody.categoryBitMask = paddleCategory;
self.physicsBody.collisionBitMask = emptyCategory;
}
return self;
}
- (BOOL)withinParentFrame:(CGPoint)point
{
CGFloat offset = self.size.height / 2;
if (point.y >= offset && point.y <= self.scene.frame.size.height - offset)
return YES;
else
return NO;
}
- (CGPoint)normalisePoint:(CGPoint)point
{
CGFloat x = point.x / (self.size.width / 2);
if (x > 1.0)
x = 1.0;
else if (x < -1.0)
x = -1.0;
CGFloat y = point.y / (self.size.height / 2);
if (y > 1.0)
y = 1.0;
else if (y < -1.0)
y = -1.0;
return CGPointMake(x,y);
}
@end
ComputerNode.m
@implementation ComputerNode
-(id)init
{
return [self initOnRightSide];
}
- (id)initOnRightSide
{
self = [super init];
if (self) {
self.name = @"rightPlayer";
ComputerPaddleNode *rightPaddle = [[ComputerPaddleNode alloc] initWithName:@"rightPaddle"];
[self addChild:rightPaddle];
ScoreNode *score = [[ScoreNode alloc] initWithName:@"rightScore"];
[self addChild:score];
}
return self;
}
- (void)positionOnRightSide
{
SKNode *paddle = [self childNodeWithName:@"rightPaddle"];
paddle.position = CGPointMake(CGRectGetMaxX(self.parent.frame) -50, CGRectGetMidY(self.parent.frame));
}
@end
为什么计算机节点会消失,我需要更改它? (抱歉我的坏事)