Positioning of sprites

Discussion in 'iOS Programming' started by tattooedpierre, Jun 9, 2014.

  1. tattooedpierre macrumors newbie

    Joined:
    Aug 5, 2007
    Location:
    Glasgow, Scotland
    #1
    Hi,

    I appear to be having issues getting some sprites positioned correctly and wondered if someone could help?

    I'm following a tutorial written for Objective-C, but am changing it into Swift. Everything runs as it should, but I'm finding nodes being placed at different locations than they should be.

    I'm uncertain if I'm allowed to post Swift code as yet, but wondered if anyone could think off-hand what could be causing this? All I'm doing is setting a SpriteKit nodes 'position' property. As far as I understand it, screen size (which was my first thought as a problem) should be taken care of as CGSizeMake method is not an absolute pixel position but an anchor point. Or am I mis-undertanding?
     
  2. SilentPanda Moderator emeritus

    SilentPanda

    Joined:
    Oct 8, 2002
    Location:
    The Bamboo Forest
    #2
    You can post Swift code.
     
  3. tattooedpierre thread starter macrumors newbie

    Joined:
    Aug 5, 2007
    Location:
    Glasgow, Scotland
    #3
    Ok, thanks, so this is what I have:

    Code:
            let block = SKSpriteNode(color: UIColor.redColor(), size: CGSizeMake(100, 100));
            block.physicsBody = SKPhysicsBody(rectangleOfSize: block.size);
            block.position = CGPointMake(160, 480);
            block.physicsBody.restitution = 0;
    
    .. but when I run this, my block is not in the centre of the screen as it should be.
     
  4. waterskier2007 macrumors 68000

    waterskier2007

    Joined:
    Jun 19, 2007
    Location:
    White Lake, MI
    #4
    Where is your block positioned when you run the code? I can't remember back to when I tried out SpriteKit but normally position is based on the top left point, so you would have to have something like

    Code:
    block.position = CGPointMake(160.0 - (block.size.width / 2.0), 480 - (block.size.height/ 2.0));
    
     

Share This Page