Reputation: 235
I try to make pong like game were on one side is player and on other side computer, because I want to in future add many game scenes with different gamefields I create ball node, paddle node etc.files separately, to make it easier to insert in gamefields and I don't have them create on every scenes from scratch.
The Problem Now when I try everything put together player paddle works, ball node works, but computer paddle shows up for second and then disappears. Here are what I got in:
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
Why Computer node disappears and what do I need to change that?
Upvotes: 0
Views: 46
Reputation: 1475
In this code snippet
- (void)positionOnRightSide
{
SKNode *paddle = [self childNodeWithName:@"rightPaddle"];
paddle.position = CGPointMake(CGRectGetMaxX(self.parent.frame) -50, CGRectGetMidY(self.parent.frame));
}
you are positioning self's child paddle in the coordinate system of the self's parent's parent (self's "grandparent"). Remember a node's frame already refers to its parent's coordinate system.
This might be the reason your right player's paddle is disappearing on you.
Upvotes: 2