HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了iOS SpriteKit – 为bobblehead创建物理?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试创建一个逼真的bobblehead应用程序,并希望使用物理学来挖掘动画.我已经看到其他应用程序在那里做,并从我收集我需要使用SpriteKit.

我创建了一个SKScene,并使用SKPhysicsJointPin将头部固定在身体上,但它根本不会移动.任何想法或建议?

更新后的代码(5/28):

现在在头上使用2个弹簧接头,它从左向右移动,但不是向上和向下移动.此外,快速敲击会使头部最终足够远,直至“跌落”并且不在视线范围内.奇怪的.

对于适当的设置有什么想法,让它能够在它的起始位置远离中心并保持在指定区域内,以便它能够从身体上滑落并看起来很有趣?

BobbleheadView是一个子类SKView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.BACkgroundColor =[UIColor clearColor];
        self.showsFPS = YES;
        self.showsnodeCount = YES;

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                       initWithTarget:self
                                       action:@SELEctor(animateBobblE)];

        [self addGestureRecognizer:tap];

        SKScene *bobbleheadScene = [SKScene sceneWithSize:self.bounds.size];
        [self presentScene:bobbleheadScene];

        // 1. Body
        self.body = [SKSpriteNode spriteNodeWithImagenamed:@"Bobble-Body"];
        self.body.position = CGPointMake(self.frame.size.width/2,self.body.frame.size.height/2);
        self.body.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
        [bobbleheadScene addChild:self.body];

        // 2. Head
        self.head = [SKSpriteNode spriteNodeWithImagenamed:@"Bobble-Head"];
        self.head.position = CGPointMake(self.center.x,self.body.frame.size.height);
        //self.head.physicsBody.affectedByGravity = YES;
//        self.head.physicsBody.dynamic = NO;
        //This bobbles head great,but head falls off body and out of view
        self.head.physicsBody = [SKPhysicsBody bodyWithRectangLeofSize:self.head.size center:self.head.position];
        //End
        //self.head.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.head.frame];
        //self.head.physicsBody = [SKPhysicsBody bodyWithCircLeofRadius:self.head.size.height/2];
        [bobbleheadScene addChild:self.head];

        // 3. Ceiling
        self.ceiling = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:CGSizeMake(32,32)];
        self.ceiling.position = CGPointMake(self.frame.origin.x+self.frame.size.width/2,self.frame.size.height);
        self.ceiling.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
        [bobbleheadScene addChild:self.ceiling];



        //Spring Joint for Ceiling to Head
        SKPhysicsJointSpring *spring1 = [SKPhysicsJointSpring jointWithBodyA:self.ceiling.physicsBody bodyB:self.head.physicsBody anchorA:self.ceiling.position anchorB:CGPointMake(self.head.frame.origin.x+self.head.frame.size.width/2,0)];

        spring1.frequency = 20.0; //gives the spring some elasticity.
        spring1.damping = 5.0; //Will remove damping to create the 'pendulum'
        [bobbleheadScene.physicsWorld addJoint:spring1];


        //Spring Joint for Head to Body
        SKPhysicsJointSpring *spring = [SKPhysicsJointSpring jointWithBodyA:self.body.physicsBody bodyB:self.head.physicsBody anchorA:CGPointMake(self.body.position.x+self.body.size.width/2,self.body.position.y) anchorB:CGPointMake(self.head.position.x+self.head.size.width/2,self.body.position.y-self.body.size.height/2)];

        spring.frequency = 10.0; //gives the spring some elasticity.
        spring.damping = 1.0;
        [bobbleheadScene.physicsWorld addJoint:spring];

    }
    return self;
}

-(void)animateBobble{
    NSLog(@"Did Tap Bobblehead!");
    [self.head.physicsBody applyImpulse:CGVectorMake(100,-200)];
    //[self.body.physicsBody applyImpulse:CGVectorMake(20,10)];
}

解决方法

这个工作的唯一方法是制作一个“雪橇”.

所以:制作一个应用程序,在屏幕上有(比方说)六个滑块.

TBC我的意思是,当应用程序实际运行时,你会看到六个滑块.

TBC,这只是一个雪橇,它只适合你作为开发者,它不适合消费者应用程序.

(我认为“雪橇”一词来自汽车行业;当他们制造第一个版本的底盘/发动机以进行测试时.)

使它成为一些滑块控制弹簧功率/阻尼(或者你在cocos中可用的任何因素)以及其他滑块推动连接/弹簧长度的位置.

这是获得结果的唯一方法!务必在屏幕上显示值,或者至少在控制台上更改时将它们打印出来.

这是游戏开发中的日常事务.通常,雪橇比实际的消费者场景或特征更有效.没有“雪橇”,你将无法实现它.我希望它有所帮助!

大佬总结

以上是大佬教程为你收集整理的iOS SpriteKit – 为bobblehead创建物理?全部内容,希望文章能够帮你解决iOS SpriteKit – 为bobblehead创建物理?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。