您好, 我是spriteKit的新手,我正在尝试制作游戏。在游戏中,我有一个从楼梯跳到楼梯的玩家,它无限地从屏幕顶部出来(就像在涂鸦跳跃中一样,只有跳跃由玩家触摸控制)。我试图通过对玩家施加冲动来跳跃,但我想通过玩家的触摸持续时间来控制跳跃强度。我怎么能这样做?当玩家 STARTS 触摸屏幕时执行跳转,这样我就无法测量跳跃强度(通过计算触摸持续时间)......任何想法? 提前致谢!!! (:
答案 0 :(得分:1)
这是一个简单的演示,可以在具有触摸持续时间的节点上应用脉冲。该方法很简单:触摸开始时设置BOOL变量YES
,触摸结束时设置NO
。触摸时,它将在update
方法中应用恒定的脉冲。
为了让游戏更自然,你可能想要优化冲动动作,或者在节点升序时向下滚动背景。
<强> GameScene.m:强>
#import "GameScene.h"
@interface GameScene ()
@property (nonatomic) SKSpriteNode *node;
@property BOOL touchingScreen;
@property CGFloat jumpHeightMax;
@end
@implementation GameScene
- (void)didMoveToView:(SKView *)view
{
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
// Generate a square node
self.node = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(50.0, 50.0)];
self.node.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
self.node.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.node.size];
self.node.physicsBody.allowsRotation = NO;
[self addChild:self.node];
}
const CGFloat kJumpHeight = 150.0;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
self.touchingScreen = YES;
self.jumpHeightMax = self.node.position.y + kJumpHeight;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
self.touchingScreen = NO;
self.jumpHeightMax = 0;
}
- (void)update:(CFTimeInterval)currentTime
{
if (self.touchingScreen && self.node.position.y <= self.jumpHeightMax) {
self.node.physicsBody.velocity = CGVectorMake(0, 0);
[self.node.physicsBody applyImpulse:CGVectorMake(0, 50)];
} else {
self.jumpHeightMax = 0;
}
}
@end