gpt4 book ai didi

swift & Spritekit : Creation of a slope

转载 作者:搜寻专家 更新时间:2023-11-01 05:41:27 29 4
gpt4 key购买 nike

我正在尝试在 2D 游戏中创建斜坡。这个斜坡应该有一个 X 度。当现在一个球“滚过”这个斜坡时。这个坡度的效果应该改变,然后球的行为因此向后滚动,向前滚动或在其他地方初始化倾斜度。我认为我可以使用具有物理 body 的 SKSpritenode 来实现这一点,并且当球滚动(“碰撞”)时效果应该受到球的影响,但我无法实现这一点。

我怎样才能做到这一点?

最好的问候,

克里斯

最佳答案

如果我对你的问题的理解正确,你正在寻找斜坡,以便在球向上或向下移动时对球产生物理影响。

请看下面的示例项目(该项目处于横向模式并且 show physics 设置为 YES)。

#import "GameScene.h"

typedef NS_OPTIONS(uint32_t, Level1PhysicsCategory) {
CategoryBall = 1 << 0,
CategoryFloor0 = 1 << 1,
};

@implementation GameScene {
SKSpriteNode *ballNode;
}

-(void)didMoveToView:(SKView *)view {
self.backgroundColor = [SKColor whiteColor];

// create slope floor
SKSpriteNode *myNode0 = [SKSpriteNode spriteNodeWithColor:[SKColor clearColor] size:CGSizeMake(200, 100)];
myNode0.position = CGPointMake(500,300);
myNode0.zPosition = 100;
CGMutablePathRef trianglePath = CGPathCreateMutable();
CGPathMoveToPoint(trianglePath, nil, -myNode0.size.width/2, myNode0.size.height/2);
CGPathAddLineToPoint(trianglePath, nil, myNode0.size.width/2, -myNode0.size.height/2);
CGPathAddLineToPoint(trianglePath, nil, -myNode0.size.width/2, -myNode0.size.height/2);
CGPathAddLineToPoint(trianglePath, nil, -myNode0.size.width/2, myNode0.size.height/2);
myNode0.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:trianglePath];
myNode0.physicsBody.dynamic = NO;
myNode0.physicsBody.restitution = 0;
myNode0.physicsBody.friction = 0.0;
myNode0.physicsBody.categoryBitMask = CategoryFloor0;
myNode0.physicsBody.collisionBitMask = CategoryBall;
CGPathRelease(trianglePath);
[self addChild:myNode0];

// create top floor
SKSpriteNode *myNode1 = [SKSpriteNode spriteNodeWithColor:[SKColor clearColor] size:CGSizeMake(300, 5)];
myNode1.position = CGPointMake(250,348);
myNode1.zPosition = 100;
myNode1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(300, 5)];
myNode1.physicsBody.dynamic = NO;
myNode1.physicsBody.restitution = 0;
myNode1.physicsBody.friction = 0.0;
myNode1.physicsBody.categoryBitMask = CategoryFloor0;
myNode1.physicsBody.collisionBitMask = CategoryBall;
[self addChild:myNode1];

// create bottom floor
SKSpriteNode *myNode2 = [SKSpriteNode spriteNodeWithColor:[SKColor clearColor] size:CGSizeMake(300, 5)];
myNode2.position = CGPointMake(750,248);
myNode2.zPosition = 100;
myNode2.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(300, 5)];
myNode2.physicsBody.dynamic = NO;
myNode2.physicsBody.restitution = 0;
myNode2.physicsBody.friction = 0.0;
myNode2.physicsBody.categoryBitMask = CategoryFloor0;
myNode2.physicsBody.collisionBitMask = CategoryBall;
[self addChild:myNode2];

// create ball
ballNode = [SKSpriteNode spriteNodeWithColor:[SKColor clearColor] size:CGSizeMake(20, 20)];
ballNode.position = CGPointMake(770,300);
ballNode.zPosition = 100;
ballNode.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:10];
ballNode.physicsBody.dynamic = YES;
ballNode.physicsBody.restitution = 0;
ballNode.physicsBody.friction = 0.0;
ballNode.physicsBody.categoryBitMask = CategoryBall;
ballNode.physicsBody.collisionBitMask = CategoryFloor0;
[self addChild:ballNode];
}

-(void)update:(CFTimeInterval)currentTime {

// check if ball has fallen off screen
// if yes, put ball back in place
if(ballNode.position.y < 0) {
ballNode.physicsBody.velocity = CGVectorMake(0, 0);
ballNode.position = CGPointMake(770, 300);
}
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// change the dx velocity to apply more or less force to the ball
[ballNode.physicsBody applyImpulse:CGVectorMake(-7, 0)];
}

关于 swift & Spritekit : Creation of a slope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29718733/

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