Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

tattooedpierre

macrumors newbie
Original poster
Aug 5, 2007
15
0
Glasgow, Scotland
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?
 
You can post Swift code.

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.
 
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));
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.