ComputerNode 不显示 Objective-C

Posted

技术标签:

【中文标题】ComputerNode 不显示 Objective-C【英文标题】:ComputerNode doesn't show up Objective-C 【发布时间】:2016-04-12 17:05:46 【问题描述】:

我尝试制作类似乒乓球的游戏,一侧是玩家,另一侧是计算机,因为我想在未来添加许多具有不同游戏场的游戏场景,我分别创建球节点、桨节点等文件,以实现更容易插入游戏区域,而且我没有从头开始在每个场景中创建它们。问题现在,当我尝试将所有东西放在一起时,玩家桨有效,球节点有效,但计算机桨显示上升第二然后消失。这是我得到的: 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

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

【问题讨论】:

【参考方案1】:

在这段代码中sn-p

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

您正在将 self 的孩子 paddle 定位在 self 的 parent's 父母(self's “祖父母”)。记住一个节点的框架已经引用了它的父坐标系。

这可能是您右边球员的球拍消失在您身上的原因。

【讨论】:

谢谢,是这个问题

以上是关于ComputerNode 不显示 Objective-C的主要内容,如果未能解决你的问题,请参考以下文章

如何禁用 UIPickerView (Swift)?

导入的框架在视图控制器中不可见

在 Swift 中为 NSOutputStream 设置 TCP_NODELAY

ARC 不允许将非 Objective-C 指针类型“const char *”隐式转换为“id”

cakephp saveAll 不适用于关联

在 Swift 项目中使用 Objective C 类中的 Swift 类