ComputerNode 未显示 Objective-C

ComputerNode doesn't show up Objective-C

我试着把乒乓球打成这样,一边是玩家,另一边是电脑,因为我想在未来添加许多具有不同游戏场的游戏场景我分别创建了球节点、球拍节点 etc.files , 以便更容易地插入游戏场,我没有让它们从头开始在每个场景上创建。
问题 现在,当我尝试将所有东西放在一起时,玩家球拍工作正常,球节点工作,但电脑球拍显示第二个然后消失。这是我得到的: 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

为什么计算机节点消失了,我需要更改什么?

在此代码段中

- (void)positionOnRightSide
   {
     SKNode *paddle = [self childNodeWithName:@"rightPaddle"];
     paddle.position = CGPointMake(CGRectGetMaxX(self.parent.frame) -50, CGRectGetMidY(self.parent.frame));  
   }

你正在将self的childpaddle定位在自己[的坐标系中=17=]parent'sparent(自己的"grandparent")。请记住,节点的框架已经引用了其 parent 的坐标系。

这可能是您右侧玩家的球拍在您身上消失的原因。