2
\$\begingroup\$

I am trying to make simple game, but I have a problem. The node is too small. On iPhones with smaller screen size, the finger can't move it. On iPad and on simulator, however, the node can be moved due to larger sizes of the screens. Due to that, I know it's not the code that causes the issue.

Due to the fact that I am using this paddle image on many levels, I create it as separate file so I can implement it anywhere I want within the app.

Here is what I got:

PaddleNode.m

- (id)init
{
    return [self initWithName:@"paddle"];
}

- (id)initWithName:(NSString *)name
{
    self = [super init];
    if (self) {

        self = [PaddleNode 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;
        self.currentTouchValue = nil;
    }

    return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.previousTouchesTimestamp = event.timestamp;
    for (UITouch *touch in touches) {
        self.currentTouchValue = [NSValue valueWithNonretainedObject:touch];
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.currentTouchValue = nil;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    for (UITouch *touch in touches) {
        NSValue *key = [NSValue valueWithNonretainedObject:touch];
        if ([key isEqualToValue:self.currentTouchValue]) {
           CGPoint touchPoint = [touch locationInNode:self.scene];
           CGPoint movePoint = CGPointMake(self.position.x, touchPoint.y);

           if ([self withinParentFrame:movePoint])
               self.position = movePoint;
       }
    }
 }

 - (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;
 }

P.S. I am learning Objective-C by myself and English is not my first language. So, if it's OK, explain me like you would to little child.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Given that PaddleNode is a subclass of SpriteKit Node, what would be an important thing to do is to first set the size property when using spriteNodeWithImageNamed::

self.size = self.view.scene.frame.size;

//Or, you could set it to somewhere else.
//Example: 
//self.size = (CGSize){100.0f, 100.0f};

self = [PaddleNode spriteNodeWithImageNamed: @"board.png"];

Another part we'd need to do is to set its position, after having set up the size. The following is an example for positioning the node relative to its parent:

self.position = (CGPoint){CGRectGetMidX(self.scene.view.frame), CGRectGetMidY(self.scene.view.frame)};
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .