gpt4 book ai didi

subclass - SpriteKit : How To Subclass SKNode to create a Vehicle class (complex object with joints)

转载 作者:行者123 更新时间:2023-12-02 22:02:46 28 4
gpt4 key购买 nike

有没有人(本地)弄清楚如何对 SKNode 进行子类化以包含通过关节连接的多个物体 - 例如汽车?

似乎没有办法将子类的关节添加到父场景的物理世界属性中。

此外,当尝试编译并运行下面的对象时,即使没有关节,我也会收到 BAD_EXC_ACCESS 错误。

感谢@Smick 提供此处发布的初始车辆代码:Sprite Kit pin joints appear to have an incorrect anchor

卡车类别:

#import "Truck.h"

@implementation Truck


-(id)initWithPosition:(CGPoint)pos {


SKSpriteNode *carBody = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:CGSizeMake(120, 8)];
carBody.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:carBody.size];
carBody.position = pos;
carBody.physicsBody.mass = 1.0;


SKSpriteNode *carTop = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:CGSizeMake(50, 8)];
carTop.position = CGPointMake(230, 708);
carTop.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:carTop.size];
carTop.physicsBody.mass = 0;

SKPhysicsJointFixed *carBodyJoint = [SKPhysicsJointFixed jointWithBodyA:carBody.physicsBody
bodyB:carTop.physicsBody
anchor:CGPointMake(0, 0)];



return self;
}

+(Truck*)initWithPosition:(CGPoint)pos {
return [[self alloc] initWithPosition:pos];
}


@end

我的场景: enter image description here

最佳答案

抱歉发帖晚了,但我自己就点击了这个。

除非附加的节点已添加到 SKScene 的场景图中,否则物理关节不起作用。

在上面的 initWithPosition 期间,情况并非如此。传递 SKScene 对我来说也不起作用,我怀疑是因为车辆节点仍未添加到场景图中。

您仍然可以将物理关节封装在类中,但必须在

之后调用另一个方法
[self addChild:car]

这是我对您已有的内容的改进:

车辆.h

@interface Vehicle : SKNode

@property (nonatomic) SKSpriteNode *leftWheel;
@property (nonatomic) SKSpriteNode *ctop;

-(id)initWithPosition:(CGPoint)pos;
-(void)initPhysics;

@end

车辆.m

//

#import "Vehicle.h"

@implementation Vehicle {
SKSpriteNode *chassis;
SKSpriteNode *rightWheel;
SKSpriteNode *leftShockPost;
SKSpriteNode *rightShockPost;
int wheelOffsetY;
CGFloat damping;
CGFloat frequency;
}

- (SKSpriteNode*) makeWheel
{
SKSpriteNode *wheel = [SKSpriteNode spriteNodeWithImageNamed:@"Wheel.png"];
// wheel.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:wheel.size.width/2];
return wheel;
}

-(id)initWithPosition:(CGPoint)pos {

if (self = [super init]) {

wheelOffsetY = 60;
damping = 1;
frequency = 4;

chassis = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:CGSizeMake(120, 8)];
chassis.position = pos;
[self addChild:chassis];

_ctop = [SKSpriteNode spriteNodeWithColor:[UIColor greenColor] size:CGSizeMake(70, 16)];
_ctop.position = CGPointMake(chassis.position.x+20, chassis.position.y+12);

[self addChild:_ctop];

_leftWheel = [self makeWheel];
_leftWheel.position = CGPointMake(chassis.position.x - chassis.size.width / 2, chassis.position.y - wheelOffsetY); //Always set position before physicsBody

[self addChild:_leftWheel];

rightWheel = [self makeWheel];
rightWheel.position = CGPointMake(chassis.position.x + chassis.size.width / 2, chassis.position.y - wheelOffsetY);
[self addChild:rightWheel];

//------------- LEFT SUSPENSION ----------------------------------------------------------------------------------------------- //

leftShockPost = [SKSpriteNode spriteNodeWithColor:[UIColor blueColor] size:CGSizeMake(7, wheelOffsetY)];
leftShockPost.position = CGPointMake(chassis.position.x - chassis.size.width / 2, chassis.position.y - leftShockPost.size.height/2);
[self addChild:leftShockPost];


//------------- RIGHT SUSPENSION ----------------------------------------------------------------------------------------------- //

rightShockPost = [SKSpriteNode spriteNodeWithColor:[UIColor blueColor] size:CGSizeMake(7, wheelOffsetY)];
rightShockPost.position = CGPointMake(chassis.position.x + chassis.size.width / 2, chassis.position.y - rightShockPost.size.height/2);
[self addChild:rightShockPost];

}

return self;
}

-(void) initPhysics {

chassis.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:chassis.size];
_ctop.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:_ctop.size];

SKPhysicsJointFixed *cJoint = [SKPhysicsJointFixed jointWithBodyA:chassis.physicsBody
bodyB:_ctop.physicsBody
anchor:CGPointMake(_ctop.position.x, _ctop.position.y)];

_leftWheel.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:_leftWheel.size.width/2];
_leftWheel.physicsBody.allowsRotation = YES;

rightWheel.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:rightWheel.size.width/2];
rightWheel.physicsBody.allowsRotation = YES;

leftShockPost.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:leftShockPost.size];
SKPhysicsJointSliding *leftSlide = [SKPhysicsJointSliding jointWithBodyA:chassis.physicsBody
bodyB:leftShockPost.physicsBody
anchor:CGPointMake(leftShockPost.position.x, leftShockPost.position.y)
axis:CGVectorMake(0, 1)];

leftSlide.shouldEnableLimits = TRUE;
leftSlide.lowerDistanceLimit = 5;
leftSlide.upperDistanceLimit = wheelOffsetY;


SKPhysicsJointSpring *leftSpring = [SKPhysicsJointSpring jointWithBodyA:chassis.physicsBody bodyB:_leftWheel.physicsBody
anchorA:CGPointMake(chassis.position.x - chassis.size.width / 2, chassis.position.y)
anchorB:_leftWheel.position];
leftSpring.damping = damping;
leftSpring.frequency = frequency;

SKPhysicsJointPin *lPin = [SKPhysicsJointPin jointWithBodyA:leftShockPost.physicsBody bodyB:_leftWheel.physicsBody anchor:_leftWheel.position];

rightShockPost.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:rightShockPost.size];
SKPhysicsJointSliding *rightSlide = [SKPhysicsJointSliding jointWithBodyA:chassis.physicsBody
bodyB:rightShockPost.physicsBody
anchor:CGPointMake(rightShockPost.position.x, rightShockPost.position.y)
axis:CGVectorMake(0, 1)];

rightSlide.shouldEnableLimits = TRUE;
rightSlide.lowerDistanceLimit = 5;
rightSlide.upperDistanceLimit = wheelOffsetY;


SKPhysicsJointSpring *rightSpring = [SKPhysicsJointSpring jointWithBodyA:chassis.physicsBody bodyB:rightWheel.physicsBody
anchorA:CGPointMake(chassis.position.x + chassis.size.width / 2, chassis.position.y)
anchorB:rightWheel.position];
rightSpring.damping = damping;
rightSpring.frequency = frequency;

SKPhysicsJointPin *rPin = [SKPhysicsJointPin jointWithBodyA:rightShockPost.physicsBody bodyB:rightWheel.physicsBody anchor:rightWheel.position];

// Add all joints to the array.
// Add joints to scene's physics world

[self.scene.physicsWorld addJoint: cJoint];
[self.scene.physicsWorld addJoint: leftSlide];
[self.scene.physicsWorld addJoint: leftSpring];
[self.scene.physicsWorld addJoint: lPin];
[self.scene.physicsWorld addJoint: rightSlide];
[self.scene.physicsWorld addJoint: rightSpring];
[self.scene.physicsWorld addJoint: rPin];

}
@end

并从 MyScene.m 调用它

_car = [[DMVehicle alloc] initWithPosition:location];
[self addChild:_car];
[_car initPhysics];

希望有所帮助,我知道它对我的工作有所帮助

关于subclass - SpriteKit : How To Subclass SKNode to create a Vehicle class (complex object with joints),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20305724/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com