简体   繁体   中英

Using anchorPoint in spritekit to each node

Im wondering at my setting ball.position function, the point is to set ball's point to other spritenode(in left/middle/right side), i tried to write my own function, but it was full of bugs, (it's commented), next after review developer lib's. I found anchorPoint to parent position, but it's not working, i dont know exactly how to set current parent for ball. I'll be really grateful for some advices.

    if (ball.position.y < block.position.y +10 && ball.position.y > block.position.y -10) {     
        midX = CGRectGetMidX(self.frame);
        side = block1.size.width/2;

        if (emitter.position.x < midX - side+5) {
            //  fixedEmitterPos = emitter.position.x+side;
            ball.anchorPoint = CGPointMake(0,0.5);
        }
        if (emitter.position.x > midX + side-5){
            //  fixedEmitterPos = emitter.position.x-side;
            ball.anchorPoint = CGPointMake(1,0.5);
        }
        if (emitter.position.x == 160) {
            //  fixedEmitterPos = emitter.position.x;
            ball.anchorPoint = CGPointMake(0.5,0.5);
        }
  }

I don't know what exactly is your question but to change your anchor point you don't change anything in parent node. For example if anchor point is

ball.anchorPoint = CGPointMake(0.5,0.5);

the anchor point in exactly in the middle and if you change position of the ball like:

ball.position == CGPointMake(50,50);

the ball center will be exactly in that point (50, 50).

but if the anchor point will be:

ball.anchorPoint = CGPointMake(1, 1);

and you change the position to :

ball.position == CGPointMake(50,50);

the ball centre will be in

X = 50 - (ball width / 2)
Y = 50 - (ball height / 2)

If you want to set up ball position base on other sprite node you can do something like that:

//Attach left centre side of ball to other sprite:
ball.anchorPoint = CGPointMake(0, 0.5);
ball.position.x == otherSprit.position.x + otherSprit.size.with;
ball.position.y == otherSprit.position.y;

//Attach right centre side of ball to other sprite:
ball.anchorPoint = CGPointMake(1, 0.5);
ball.position.x == otherSprit.position.x;
ball.position.y == otherSprit.position.y;

Hope this is what you are about.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM