我试图在2D游戏中创建斜坡。这个坡度应该是X度。当现在一个球滚动时#34;超过"这个斜坡。这个斜率的影响应该改变球的行为,以便向后,向前或在其他地方初始化倾斜度。我认为我可以通过带有物理身体的SKSpritenode实现这一点,并且当球滚过时("碰撞")效果应该受到影响,但是我无法实现这一点。
我怎样才能做到这一点?
致以最诚挚的问候,
克里斯
答案 0 :(得分:0)
如果我正确地理解了你的问题,你正在寻找斜坡,当它在斜坡上下移动时对球产生物理效应。
查看下面的示例项目(项目处于横向模式,并且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)];
}